Thursday, November 27, 2014

C program to display only the names which has vowels; accept the names through command line

/*................................................................................................................................................
File Name: rec30.c
Program : Write a program to display only the names which has vowels; accept the names through command line
Author : sujithbaby440@gmail.com
Date : 26-11-2014
...................................................................................................................................................*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int vowel(char str[10]);
main(int argc,char *argv[])
{
int i,n,j,res,a;
char str[10],arg[10];
for(i=1;i<argc;i++){
a=vowel(argv[i]);
if(a)
printf("%s\n",argv[i]);
}
}
int vowel(char str[10])
{
int i=0,flag=0;
while(str[i]!='\0'){
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'){
flag=1;
break;
}
else if(str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U'){
flag=1;
break;
}
i++;
}
return flag;
}


OUTPUT:-
1) /a.out hai friends how are you?
hai
friends
how
are
you?
2) ./a.out What is your age? Is it 23
What
is
your
age?
Is
It

C Program to count the numbers which are lies between 25 and 75 among the numbers passed through command line

/*…………………………………………………………………………………………..
File Name: rec29.c
Program :  C Program to count the numbers which are lies between 25 and 75 among the numbers passed through command line
Author : sujithbaby440@gmail.com
Date : 26-11-2014
..................................................................................................................................................*/
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
int i,num,count=0;
for(i=1;i<argc;i++){
num=atoi(argv[i]);
if((num>25)&&(num<75)){
count=count+1;
}
}
printf("Count of numbers between 25 and 75 is %d",count);
return 1;
}
OUTPUT 1:-
[141740@localhost ~]$ ./a.out 36 45 60
Count of numbers between 25 and 75 is 3[141740@localhost ~]$
OUTPUT 2:-
[141740@localhost ~]$ ./a.out 22 36 89 45 55
Count of numbers between 25 and 95 is 4[141740@localhost ~]$

C Program to count characters,words and lines in a text file,accept the file name through the command line

/*--------------------------------------------------------------------------------
File :rec28.c
Program :Program to count characters,words and lines in a text file,accept the file name through the command line.
Author :sujithbaby440@gmail.com
Date :25-11-2014
----------------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
FILE *fp;
int ch,c=0,l=0,w=0;
if(argc<2){
printf("Enter proper arguments\n");
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("Error:No Read Permission\n");
exit(0);
}
while((ch=getc(fp))!=EOF){
c++;
if((ch==' ')||(ch=='\n'))
w++;
if(ch=='\n')
l++;
}
printf("Number of characters:%d\n",c);
printf("Number of words:%d\n",w);
printf("Number of lines:%d\n",l);
fclose(fp);
}

Output 1:
[141740@localhost ~]$ ./a.out count.c
Number of characters:717
Number of words:314
Number of lines:27

Output 2:
[141740@localhost ~]$ ./a.out trace.c
Number of characters:520
Number of words:198
Number of lines:27

C programto display the content of the file .(accept filename through command line.)

/*...................................................................................................................
File :rec27.c
Program :To display the content of the file accept filename through command line.
Author :sujithbaby440@gmail.com
Date :26/11/2014
..................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int ch;
if(argc<2){
printf("you have not entered the command properly, check the syntax\n");
printf("SYNTAX:stypefile<filename>\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("file does not exist");
exit(1);
}
printf("the content of the file are\n");
while((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 1;
}
Output :
[141740@localhost ~]$ ./a.out c27.c
the content of the file are
/*........................................................................
cmd.c
To display the content of the file accept filename through command line.
Author:141740
26/11/2014
.........................................................................*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int ch;
if(argc<2){
printf("you have not entered the command properly, check the syntax\n");
printf("SYNTAX:stypefile<filename>\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("file does not exist");
exit(1);
}
printf("the content of the file are\n");
while((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 1;
}
[141740@localhost ~]$

C program to demonstrate the use of fread() and fwrite().

/*………………………………………………………………………………………...............
File Name: rec26.c
Program : File program to demonstrate the use of fread() and fwrite(). Define a structure to store the details of employees(Emp_ID,Emp_Name,Emp_Sal etc..)and store into some file using fwrite() and read the same using fread().
Author : sujithbaby440@gmail.com
Date : 26-11-2014
……………............................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
typedef struct employee{
int emp_id;
char emp_name[20];
float emp_sal;
}EMPLOYEE;
EMPLOYEE E[20],EMP[20];
main()
{
FILE *fp;
int i,j,n;
system("clear");
printf("Program to store the details of employee\n");
fp=fopen("employee.txt","w");
if(fp!=NULL){
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the employee id:\n");
scanf("%d",&E[i].emp_id);
printf("Enter employee name:\n");
scanf("%s",E[i].emp_name);
printf("Enter the salary:\n");
scanf("%f",&E[i].emp_sal);
fwrite(&E[i],sizeof(EMPLOYEE),1,fp);
}
fclose(fp);
fp=fopen("employee.txt","r");
rewind(fp);
printf("Emp_ID\tEmp_Name\tSalary\n");
for(i=1;i<=n;i++){
fread(&EMP[i],sizeof(EMPLOYEE),1,fp);
printf("%d\t%s\t\t%.2f\n",EMP[i].emp_id,EMP[i].emp_name,EMP[i].emp_sal);
}
fclose(fp);
}
else
printf("Error in opening file");
}

OUTPUT 1:-
Program to store the details of employee
Enter the value of N:3
Enter the employee id:
212
Enter employee name:
Sujith
Enter the salary:
25000
Enter the employee id:
345
Enter employee name:
Renjith
Enter the salary:
28000
Enter the employee id:
651
Enter employee name:
Alan
Enter the salary:
30000
Emp_ID Emp_Name Salary
212 Sujith 25000.00
345 Renjith 28000.00
651 Alan 30000.00

OUTPUT 2
Program to store the details of employee
Enter the value of N:2
Enter the employee id:
55
Enter employee name:
Koran
Enter the salary:
35000
Enter the employee id:
65
Enter employee name:
Thinnappan
Enter the salary:
40000
Emp_ID Emp_Name Salary
55 Koran 35000.00
65 Thinnapan 40000.00

C program to write 'N' numbers into a file “numbers.txt” and the display only the numbers which are divisible by 7 from the file

/*............................................................................................................................................
File Name: rec25.c
Program : Write a program to write 'N' numbers into a file “numbers.txt” and the display only the numbers which are divisible by 7 from the file(Use fscanf() and fprintf() functions for reading and writing)
Author : 141740
Date : 26-11-2014
..............................................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int n,i,a[10];
system("clear");
printf("Program to find the divisible of 7\n");
if((fp=fopen("numbers.txt","w"))==NULL){
printf("Cannot create file...!\n");
exit(1);
}
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter number into the file:\n");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
fprintf(fp,"%d\t",a[i]);
}
fclose(fp);
if((fp=fopen("numbers.txt","r"))==NULL){
printf("Cannot open file\n");
exit(1);
}
printf("The numbers which are divisible by 7 in the file is:\n");
for(i=1;i<=n;i++){
fscanf(fp,"%d",&a[i]);
if((a[i]%7)==0){
printf("%d\t",a[i]);
}
}
fclose(fp);
}


OUTPUT:-
Program to find the divisible of 7
Enter the value of n:
4
Enter number into the file:
7
14
23
66
The numbers which are divisible by 7 in the file is:
7 14 [141740@localhost ~]$

output2

Program to find the divisible of 7
Enter the value of n:
3
Enter number into the file:
7
77
99
The numbers which are divisible by 7 in the file is:
7 77 [141740@localhost ~]$

C program to write all the even elements into a file “Even.txt” and all Odd numbers into another file “Odd.txt” from an array

/*.......................................................................................................................................
File : rec24.c
Program : Write a program to write all the even elements into a file “Even.txt” and all Odd numbers into another file “Odd.txt” from an array. Display the same by reading from the file.
Author : sujithbaby440@gmail.com
Date : 22-11-2014
.............................................................................................................................................. */
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i,num,ecnt=0,ocnt=0;
system("clear");
FILE *fpo,*fpe;
if((fpe=fopen("even.txt","w"))==NULL){
printf("Cannot create file");
exit(1);
}
if((fpo=fopen("odd.txt","w"))==NULL){
printf("cannot create file");
exit(1);
}
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=1;i<=n;i++){
scanf("%d",&num);
if(num%2==0){
fprintf(fpe,"%d\t",num);
ecnt++;
}
else{
fprintf(fpo,"%d\t",num);
ocnt=ocnt+1;
}
}
fclose(fpe);
fclose(fpo);
if((fpe=fopen("even.txt","r"))==NULL){
printf("Not able to read file");
exit(1);
}
if((fpo=fopen("odd.txt","r"))==NULL){
printf("not able to read file");
exit(1);
}
printf("Even numbers in the array:\n");
for(i=1;i<=ecnt;i++){
fscanf(fpe,"%d",&num);
printf("%d\n",num);
}
printf("Odd numbers in the array:\n");
for(i=1;i<=ocnt;i++){
fscanf(fpo,"%d\t",&num);
printf("%d\t",num);
}
fclose(fpe);
fclose(fpo);
}

OUTPUT:-
Enter the value of n: 5
Enter 5 numbers
44
66
99
88
77
Even numbers in the array:
44
66
88
Odd numbers in the array:
99 77 [141740@localhost ~]$
Enter the value of n: 3
Enter 3 numbers
59
61
64
Even numbers in the array:
64
Odd numbers in the array:
59 61 [141740@localhost ~]$

C program using function subprogram to process the inventory information

/*………………………………………………………………………………………………
File Name: rec23.c
Program : A menu driven program using function subprogram to process the inventory information
(Like inserting, displaying, searching, list the item details whose prices lies between a ranges etc.) Of items
Author : sujithbaby440@gmail.com
Date : 23-11-2014
………………………………………………………………………………………………*/
#include<stdio.h>
#include<stdlib.h>
typedef struct InventType{
int item_id;
int item_price;
int item_qty;
char item_name[30];
}INVENTORY;
INVENTORY inv[20];
int n,i;
void FnMenu(int *choice);
void FnInsert();
void FnDisplay();
void FnSearch();
void FnListing();
main()
{
int choice;
system("clear");
printf("Menu Driven program to process Inventory Information");
FnMenu(&choice);
while(choice){
switch(choice){
case 1:FnInsert();
FnMenu(&choice);
break;
case 2:FnDisplay();
FnMenu(&choice);
FnMenu(&choice);
break;
case 4:FnListing();
FnMenu(&choice);
break;
case 5:exit(1);
break;
default:printf("Invalid Choice\n");
FnMenu(&choice);
break;
}
}
}
/*...................Function Definition....................*/
void FnMenu(int *choice)
{
printf("\n\t\tMENU\n");
printf("\n1.Inserting item details\n");
printf("2. Displaying the item details\n");
printf("3. Search for items\n");
printf("4. List the items within range\n");
printf("5. Exit\n\n");
printf("Enter your choice:");
scanf("%d",choice);
}
void FnInsert()
{
printf("Program to process inventory information\n");
printf("Enter the number of items:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the item id:");
scanf("%d",&inv[i].item_id);
printf("Enter the item name:");
scanf("%s",inv[i].item_name);
printf("Enter the item price:");
scanf("%d",&inv[i].item_price);
printf("Enter the item Quantity:");
scanf("%d",&inv[i].item_qty);
}
}
void FnDisplay()
{
printf("\t\tINVENTORY\n");
printf("Item Id\tItem Name\tItem Price\tQty\n");
for(i=1;i<=n;i++){
printf("%d\t%s\t\t%d\t\t%d\n",inv[i].item_id,inv[i].item_name,inv[i].item_price,inv[i].item_qty);
}
}
void FnSearch()
{
int new,flag=0;
printf("Enter the item id: ");
scanf("%d",&new);
for(i=1;i<=n;i++){
if(inv[i].item_id==new)
flag=1;
if(flag==1){
printf("\nItem Id:%d\n",inv[i].item_id);
printf("Item Name:%s\n",inv[i].item_name);
printf("Item Price:%d\n",inv[i].item_price);
printf("Item Quantity:%d\n",inv[i].item_qty);
break;
}
}
if(flag==0){
printf("Invalid item id\n");
}
}
void FnListing()
{
int r1,r2;
printf("Enter the price range:");
scanf("%d%d",&r1,&r2);
printf("\n****INVENTORY****\n");
printf("Item Id\tItem Name\tItem Price\tQty\n");
for(i=1;i<=n;i++){
if(inv[i].item_price>=r1&&inv[i].item_price<=r2){
printf("%d\t%s\t\t%d\t\t%d\n",inv[i].item_id,inv[i].item_name,inv[i].item_price,inv[i].item_qty);
}
}
}
MENU
1.Inserting item details
2. Displaying the item details
3. Search for items
4. List the items within range
5. Exit
Enter your choice:1
Program to process inventory information
Enter the number of items:2
Enter the item id:1
Enter the item name:book
Enter the item price:100
Enter the item Quantity:2
Enter the item id:2
Enter the item name:pen
Enter the item price:50
Enter the item Quantity:10
MENU
1.Inserting item details
2. Displaying the item details
3. Search for items
4. List the items within range
5. Exit
Enter your choice:2
INVENTORY
Item Id Item Name Item Price Qty
1 book 100 2
2 pen 50 10
MENU
1.Inserting item details
2. Displaying the item details
3. Search for items
4. List the items within range
5. Exit
Enter your choice: 4
MENU
1.Inserting item details
2. Displaying the item details
3. Search for items
4. List the items within range
5. Exit
Enter your choice:4
Enter the price range:60
120
****INVENTORY****
Item Id Item Name Item Price Qty
1 book 100 2
MENU
1.Inserting item details
2. Displaying the item details
3. Search for items
4. List the items within range
5. Exit
Enter your choice:5
[141740@localhost ~]$

C program to process the student‟s information (like inserting, displaying, searching etc.) Use the structures to store student's data

/*………………………………………………………………………………………………
File name: rec22.c
Program : A menu driven program using function of subprogram to process the student‟s information (like inserting, displaying, searching etc.) Use the structures to store student's
data
Author : sujithbaby440@gmail.com
Date : 22-11-2014
………………………………………………………………………………………………..*/
#include<stdio.h>
#include<stdlib.h>
typedef struct StudentType{
int SID;
char Name[30];
int Mrk1;
int Mrk2;
int Mrk3;
int Total;
float Avg;
int Result;
}Student;
Student s[20];
int N;
/*-------------------Prototype----------------*/
void fnMenu();
void fnEnterStudData();
void fnDispConsolR();
void fnDispIndRes();
main()
{
system("clear");
int ch;
printf("Program to create consolidated marks card\n");
fnMenu();
scanf("%d",&ch);
while(ch){
switch(ch){
case 1: fnEnterStudData();
fnMenu();
scanf("%d",&ch);
break;
case 2: fnDispConsolR();
fnMenu();
scanf("%d",&ch);
break;
case 3: fnDispIndRes();
fnMenu();
scanf("%d",&ch);
break;
case 4: exit(1);
break;
default:
fnMenu();
scanf("%d",&ch);
break;
}
}
}
/*----------------End of the main--------------------*/
/*-----------------Code subprogram--------------------*/
void fnMenu(){
printf("\t\t\t--------AIMIT----------\n");
printf("\t\t St. ALOYSIUS COLLEGE,MANGALORE\n");
printf("\t\t1:Enter Student Data\n");
printf("\t\t2:Display Consolidated Markshreet\n");
printf("\t\t3:Display Individual Student Marksheet\n");
printf("\t\t4:Exit\n");
printf("\nEnter Your Choice\n");
}
/*-----------------Code subprogram--------------------*/
void fnEnterStudData(){
int i;
printf("Enter Limit\n");
scanf("%d",&N);
for(i=1;i<=N;i++){
printf("Enter student registration number\n");
scanf("%d",&s[i].SID);
printf("Enter student name:\n");
scanf("%s",&s[i].Name);
printf("Enter marks in chemistry\n");
scanf("%d",&s[i].Mrk1);
printf("Enter marks in Physics\n");
scanf("%d",&s[i].Mrk2);
printf("Enter marks in Maths\n");
scanf("%d",&s[i].Mrk3);
s[i].Total=s[i].Mrk1+s[i].Mrk2+s[i].Mrk3;
s[i].Avg=s[i].Total/3;
if(s[i].Mrk1>=35&&s[i].Mrk2>=35&&s[i].Mrk3>=35&&s[i].Avg){
if(s[i].Avg>70){
s[i].Result=4;
}else if(s[i].Avg>60){
s[i].Result=1;
}else if(s[i].Avg>50){
s[i].Result=2;
}else if(s[i].Avg>40){
s[i].Result=3;
}else{
s[i].Result=0;
}
}
}
}
/*-----------------Code subprogram--------------------*/
void fnDispConsolR(){
printf("-----------------------------------------------\n");
printf("\n\t\t\tAimit Student Result\n");
printf("SID\tNAME\t\tCHEM PHYSIC MATH \tTOTAL\tAVG\t\tSTATUS\n");
int i;
for(i=1;i<=N;i++){
printf("\n%d\t",s[i].SID);
printf("%s\t",s[i].Name);
printf("\t%d\t",s[i].Mrk1);
printf("%d\t",s[i].Mrk2);
printf("%d\t",s[i].Mrk3);
printf("%d\t",s[i].Total);
printf("%f",s[i].Avg);
if(s[i].Result==4){
printf("Distinction\n");
}else if(s[i].Result==1){
printf("First class\n");
}else if(s[i].Result==2){
printf("Second class\n");
}else if(s[i].Result==3){
printf("Pass class\n");
}else{
printf("Fail\n");
}
}
}
void fnDispIndRes(){
int i,skey,flag=0,id;
printf("Enter the Student SID to display Individual Marksheet:\n");
scanf("%d",&skey);
printf("%d ",N);
for(i=1;i<=N;i++){
if(s[i].SID==skey){
flag=1;
id=i;
}
}
if(flag==1){
system("clear");
printf("\n\t\t\tAimit Student Result\n");
printf("----------------------------------\n");
printf("SID\tNAME\tCHEM PHYSIC MATH \tTOTAL\tAVG\t\tSTATUS\n");
printf("\n%d\t",s[id].SID);
printf("%s\t",s[id].Name);
printf("\t%d\t",s[id].Mrk1);
printf("%d\t",s[id].Mrk2);
printf("%d\t",s[id].Mrk3);
printf("%d\t",s[id].Total);
printf("%f",s[id].Avg);
if(s[i].Result==4){
printf("Distinction\n");
}else if(s[id].Result==1){
printf("First class\n");
}else if(s[id].Result==2){
printf("Second class\n");
}else if(s[id].Result==3){
printf("Pass class\n");
}else{
printf("Fail\n");
}
printf("------------------------------------------------\n");
}else{
printf("Invalid Result\n");
}
}
OUTPUT 1
Program to create consolidated marks card
--------AIMIT----------
St. ALOYSIUS COLLEGE,MANGALORE
1:Enter Student Data
2:Display Consolidated Mark sheet
3:Display Individual Student Mark sheet
4:Exit
Enter Your Choice
1
Enter Limit
2
Enter student registration number
141
Enter student name:
sooraj
Enter marks in chemistry
89
Enter marks in Physics
33
Enter marks in Maths
99
Enter student registration number
142
Enter student name:
ajay
Enter marks in chemistry
88
Enter marks in Physics
90
Enter marks in Maths
90
--------AIMIT----------
St. ALOYSIUS COLLEGE,MANGALORE
1:Enter Student Data
2:Display Consolidated Mark sheet
3:Display Individual Student Mark sheet
4:Exit
Enter Your Choice
2
-----------------------------------------------
Aimit Student Result
SID NAME CHEM PHYSIC MATH TOTAL AVG STATUS
141 sooraj 89 33 99 221 73.000000 Fail
142 ajay 88 90 90 268 89.000000 Distinction
--------AIMIT----------
St. ALOYSIUS COLLEGE,MANGALORE
1:Enter Student Data
2:Display Consolidated Mark sheet
3:Display Individual Student Mark sheet
4:Exit
Enter Your Choice
3
Enter the Student SID to display Individual Mark sheet:
142
Aimit Student Result
----------------------------------
SID NAME CHEM PHYSIC MATH TOTAL AVG STATUS
142 ajay 88 90 90 268 89.000000 Distinction
------------------------------------------------
--------AIMIT----------
St. ALOYSIUS COLLEGE,MANGALORE
1:Enter Student Data
2:Display Consolidated Mark sheet
3:Display Individual Student Mark sheet
4:Exit
Enter Your Choice
4
[141740@localhost ~]$

C program to sort elements in an array using bubble sort with pointers and use it in main program.

/*...................................................................................................................................................
File Name: rec21.c
Program :   program to sort elements in an array using bubble sort
with pointers and use it in main program.
Author : sujithbaby440@gmail.com
Date : 21-11-2014
...................................................................................................................................................*/
#include<stdio.h>
#include<malloc.h>
void fnInputArray(int N,int *s);
void fnDisplayArray(int N,int *s);
void fnBubbleSort(int N,int *s);
main()
{
int N,*s;
system("clear");
printf("Program to sort elements in an array using buuble sort\n");
printf("Enter the size of an array\n");
scanf("%d",&N);
s=(int*)malloc((N+1)*sizeof(int));
printf("Enter the %d elements in an array:\n",N);
fnInputArray(N,s);
printf("The elements in an array are:\n");
fnDisplayArray(N,s);
fnBubbleSort(N,s);
printf("\nThe elements in an after sorting are:\n");
fnDisplayArray(N,s);
}
void fnInputArray(int N,int *s)
{
int i;
for(i=1;i<=N;i++){
scanf("%d",&*(s+i));
}
}
void fnDisplayArray(int N,int *s)
{
int i;
for(i=1;i<=N;i++){
printf("%d\t",*(s+i));
}
}
void fnBubbleSort(int N,int *s)
{
int i,j,temp;
for(i=1;i<=(N-1);i++){
for(j=1;j<=(N-i);j++){
if(*(s+j)>*(s+j+1)){
temp=*(s+j);
*(s+j)=*(s+j+1);
*(s+j+1)=temp;
}
}
}
}


OUTPUT 1
Program to sort elements in an array using bubble sort
Enter the size of an array
5
Enter the 5 elements in an array
66
33
58
98
63
The elements in an array are:
66 33 58 98 63
The elements in an after sorting are:
33 58 63 66 98 [141740@localhost ~]$

OUTPUT 2
Program to sort elements in an array using bubble sort
Enter the size of an array
4
Enter the 4 elements in an array
33
55
11
2
The elements in an array are:
33 55 11 2
The elements in an after sorting are:
2 11 33 55 [141740@localhost ~]$

C program for string operations(reverse,compare,occurence)

/*..........................................................................................................................................
File Name : rec20.c
Program : Write the following function of subprogram using pointers and use it in main program with a menu option:
 Check whether a given character exists in a string or not
 Comparing two string lexicographically
 Count the occurrence of a given character in a string
 Reverse the given string.
Author : sujithbaby440@gmail.com
Date : 21-11-2014
………………………………………………………………………………………………...*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fnexist(char *s,char ch);
void fnlex(char *s, char *t);
void fncount(char *s,char ch);
void fnrev(char *s);
main()
{
int choice;
char *s,*t,ch;
s=(char *)malloc(25);
t=(char *)malloc(25);
system("clear");
printf("Program using pointer and use in main program\n");
printf("1.Check whether the given character exists in string\n");
printf("2.Compare two strings lexiographically\n");
printf("3.Count the occurrence of a given character in a string\n");
printf("4.Reverse the given string\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice){
case 1:printf("Enter string\n");
scanf("%s",s);
getchar();
printf("Enter character\n");
scanf("%c",&ch);
fnexist(s,ch);
break;
case 2:printf("Enter first string\n");
scanf("%s",s);
getchar();
printf("Enetr second string\n");
scanf("%s",t);
fnlex(s,t);
break;
case 3:printf("Enter string\n");
scanf("%s",s);
getchar();
printf("Enter character\n");
scanf("%c",&ch);
fncount(s,ch);
break;
case 4:printf("Enter string\n");
scanf("%s",s);
getchar();
fnrev(s);
break;
default:printf("Invalid choice\n");
break;
}
}
void fnexist(char *s, char ch)
{
int f=0,i=0;
while(*(s+i)!='\0'){
if(*(s+i)==ch){
f=1;
break;
}
i++;
}
if(f==1){
printf("Character found\n");
}else{
printf("Charcter not found\n");
}
}
void fnlex(char *s, char *t)
{
int f=1,i=0;
while(*(s+i)!='\0'){
if(*(s+i)!=*(t+i)){
f=0;
break;
}
i++;
}
if(f==1){
printf("Both strings are equal\n");
}else{
printf("Strings are not equal\n");
}
}
void fncount(char *s, char ch)
{
int f=0,i=0;
while(*(s+i)!='\0'){
if(*(s+i)==ch){
f++;
}
i++;
}
printf("Character occur %d times in string\n",f);
}
void fnrev(char *s)
{
int i=0,j=0,len;
char temp;
while(*(s+i)!='\0'){
i++;
}
len=i;
for(i=0;i<len/2;i++){
temp=*(s+i);
*(s+i)=*(s+len-i-1);
*(s+len-i-1)=temp;
}
printf("Reverse of string\n");
printf("%s",s);
printf("\n");
}

OUTPUT 1
Program using pointer and use in main program
1.Check whether the given character exists in string
2.Compare two strings lexiographically
3.Count the occurrence of a given character in a string
4.Reverse the given string
Enter your choice:
1
Enter string
tomorrow
Enter character
r
Character found
[141740@localhost ~]$


OUTPUT 2
Enter your choice:
4
Enter string
string
Reverse of string
gnirts
[141740@localhost ~]$
OUTPUT 3
Enter your choice:
2
Enter first string
triangle
Enter second string
triangle
Both strings are equal
[141740@localhost ~]$


OUTPUT 4
Enter your choice:
3
Enter string
occurrence
Enter character
c
Character occur 3 times in string
[141740@localhost ~]$

C program to Search for a string in other string

/*...............................................................................................................................
File : rec19.c
Program: Search for a string in other string
Author : sujithbaby440@gmail.com
Date : 15-11-14
.................................................................................................................................*/
#include<stdio.h>
#include<string.h>
int StrInStr(char s[], char t[]);
main ()
{
int num=1;
char s[20],t[20];
system("clear");
printf("Program to find a string inside another string\n");
printf("Enter the string\n");
scanf("%s",s);
printf("Enter the serch string\n");
scanf("%s",t);
num=StrInStr(s,t);
if(num==1){
printf("string is present\n");
}else{
printf("string is not present\n");
}
}
int StrInStr(char S[],char T[])
{
int i,j,k,n,temp=0;
i=0;
j=0;
k=0;
n=0;
while(S[n]!='\0'){
n++;
}
while(k<n){
j=k;
while(T[i]==S[j]){
i++;
j++;
temp=1;
}
k++;
}
if((T[i]=='\0')&&(temp=1)){
return 1;
}else{
return -1;
}
}

OUTPUT 1
Program to find a string inside another string
Enter the string
sundaymondaytuesday
Enter the search string
monday
string is present
[141740@localhost ~]$

OUTPUT 2
Program to find a string inside another string
Enter the string
searching
Enter the search string
search
string is present
[141740@localhost ~]$

C Program to sort the list of strings using built in string function

/*.....................................................................................................................
File : rec18.c
program : Program to sort the list of strings using built in string function
author : sujithbaby440@gmail.com
Date : 1-11-14
.........................................................................................................................*/
#include<stdio.h>
#include<string.h>
main()
{
int i,j,n;
char temp[30],names[20][30];
system("clear");
printf("Program to sort the list of name\n");
printf("Enter the number of names\n");
scanf("%d",&n);
printf("Enter names\n",n);
for(i=1;i<=n;i++){
scanf("%s",names[i]);
}
for(i=1;i<=n-1;i++){
for(j=1;j<=n-i;j++){
if(strcmp(names[j],names[j+1])>0){
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}
}
printf("\nSorted list:\n\n");
for(i=1;i<=n;i++){
printf(" %s\n",names[i]);
}
}

OUTPUT 1
Program to sort the list of name
Enter the number of names
6
Enter names
renjith
sujith
arunjith
abhijith
anand
amaljyothi
Sorted list:
abhijith
amaljyothi
anand
arunjith
renjith
sujith
[141740@localhost ~]$

OUTPUT 2
Program to sort the list of name
Enter the number of names
5
Enter names
abdul
shanoob
sujith
nihal
rishad
Sorted list:
abdul
nihal
rishad
shanoob
sujith
[141740@localhost ~]$
/*.............................................................................................................................

C Program to insert a string into another string

/*.................................................................................................................................
File : rec17.c
Program : Insert a string into another string
Author : 141740
Date : 15/11/2014
......................................................................................................................................*/
#include<stdio.h>
void StrInsert(int N,char S[],char T[]);
main()
{
int n;
char s[30],t[30];
system("clear");
printf("Program to insert a string into another string\n");
printf("Enter first string\n");
scanf("%s",t);
printf("Enter the second string\n");
scanf("%s",s);
printf("After how many letters \n");
scanf("%d",&n);
StrInsert(n,s,t);
}
void StrInsert(int N,char S[],char T[])
{
int i;
i=0;
while(T[i]!='\0'){
S[N]=T[i];
i++;
N++;
}
printf("%s\n",S);
}


OUTPUT 1
Program to insert a string into another string
Enter first string
kkpp
Enter the second string
tt
After how many letters
2
ttkkpp
[141740@localhost ~]$


OUTPUT 2
Program to insert a string into another string
Enter first string
night
Enter the second string
good
After how many letters
4
goodnight
[141740@localhost ~]$

C Program: Function sub program for the matrix operation

/*.........................................................................................................................................
File : rec16
Program: Function sub program for the matrix operation
Author : sujithbaby440@gmail.com
Date :15/11/2014
..........................................................................................................................................*/
#include<stdio.h>
void ReadMat(int M,int N,int x[5][5]);
void PrintMat(int M,int N,int x[5][5]);
void AddMat(int M,int n,int A[5][5],int B[5][5],int C[5][5]);
main()
{
int m,n,a[5][5],b[5][5],c[5][5];
system("clear");
printf("Enter the number of rows and columns for the matrix\n");
scanf("%d%d",&m,&n);
printf("\n1st matrix\n");
ReadMat(m,n,a);
PrintMat(m,n,a);
printf("\n2nd matrix\n");
ReadMat(m,n,b);
PrintMat(m,n,b);
AddMat(m,n,a,b,c);
printf("\nThe sum of the matrix\n");
PrintMat(m,n,c);
}
void ReadMat(int M,int N,int X[5][5])
{
int i,j;
printf("Enter values into matrix\n");
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
scanf("%d",&X[i][j]);
}
}
}
void PrintMat(int M,int N,int X[5][5])
{
int i,j;
printf("\nMatrix:\n");
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
printf("%d ",X[i][j]);
}
printf("\n");
}
}
void AddMat(int M,int N,int A[5][5],int B[5][5],int C[5][5])
{
int i,j;
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
C[i][j]=A[i][j]+B[i][j];
}
}
}


OUTPUT 1
Enter the number of rows and columns for the matrix
3 3
1st matrix
Enter values into matrix
1 2 8
4 5 6
7 9 9
Matrix:
1 2 8
4 5 6
7 9 9
2nd matrix
Enter values into matrix
2 1 2
4 5 6
1 1 1
Matrix:
2 1 2
4 5 6
1 1 1
The sum of the matrix
Matrix:
3 3 10
8 10 12
8 10 10
[141740@localhost ~]$


OUTPUT 2
Enter the number of rows and columns for the matrix
3 3
1st matrix
Enter values into matrix
1 1 1
1 1 1
2 2 2
Matrix:
1 1 1
1 1 1
2 2 2
2nd matrix
Enter values into matrix
4 4 4
5 5 5
1 1 1
Matrix:
4 4 4
5 5 5
1 1 1
The sum of the matrix
Matrix:
5 5 5
6 6 6
3 3 3
[141740@localhost ~]$

C Program to calculate sin or cos of an angle in degrees

/*................................................................................................................................................
File :rec14.c
Program :Menu driven function sub program for finding the
1. cosine of an angle in degrees
2.sine of an angle in degrees
Author : 141740
Date : 15-11-14
................................................................................................................................................. */
#include<stdio.h>
#include<math.h>
float CosX(float theta);
float SinX(float theta);
main()
{
int choice;
float cosx,sinx,th;
system("clear");
printf("Program to claulate sin or cos of an angle in degrees\n\n");
printf("Enter your choice\n");
printf("\t1: Cos of angel in degrees\n");
printf("\t2: Sin of angle in degrees\n");
scanf("%d",&choice);
printf("Enter the value of theta\n");
scanf("%f",&th);
if(choice==1){
cosx=CosX(th);
printf("Cos of %f = %f",th,cosx);
}else if(choice==2){
sinx=SinX(th);
printf("Sin of %f = %f",th,sinx);
}else{
printf("Enter proper choice\n");
}
}
float CosX(float theta)
{
int i;
float cosx,x,th;
x=(theta*3.14)/180.0;
th=1;
cosx=1;
i=1;
while(fabs(th)>0.0001){
th=(-1)*(th*x*x)/(((2*i)-1)*(2*i));
cosx=cosx+th;
i++;
}
return cosx;
}
float SinX(float theta)
{
int i;
float sinx,x,th;
x=(theta*3.14)/180.0;
th=x;
sinx=x;
i=1;
while(fabs(th)>0.0001){
th=(-1)*(th*x*x)/(((2*i)+1)*(2*i));
sinx=sinx+th;
i++;
}
return sinx;
}

OUTPUT 1
Program to calculate sin or cos of an angle in degrees
Enter your choice
1: Cos of angel in degrees
2: Sin of angle in degrees
1
Enter the value of theta
56
Cos of 56.000000 = 0.559604[141740@localhost ~]$

OUTPUT 2
Program to calculate sin or cos of an angle in degrees
Enter your choice
1: Cos of angel in degrees
2: Sin of angle in degrees
2
Enter the value of theta
60
Sin of 60.000000 = 0.865760[141740@localhost ~]$

c program to display all prime numbers in a given array

/*.................................................................................................................................................
File : rec14.c
program : Find the prime numbers in a given array
Author : sujithbaby440@gmail.com
Date :15-11-14
...................................................................................................................................................*/
#include<stdio.h>
int IsPrime(int N);
main()
{
int N,i,x[30],temp,num;
system("clear");
printf("The program to display all prime numbers in a given array\n\n");
printf("Enter the limit of the array\n");
scanf("%d",&N);
printf("Enter values in to array\n");
for(i=1;i<=N;i++){
scanf("%d",&x[i]);
}
printf("The prime numbers are:\n");
for(i=1;i<=N;i++){
temp=IsPrime(x[i]);
if(temp==1){
printf("%d\n",x[i]);
}
}
}
int IsPrime(int N)
{
int r,i,temp1;
i=2;
temp1=0;
while(i<N){
r=N%i;
if(r==0){
return 0;
}
i++;
}
return 1;
}


OUTPUT 1
The program to display all prime numbers in a given array
Enter the limit of the array
6
Enter values in to array
1 5 7 11 8 2
The prime numbers are:
1
5
7
11
2
[141740@localhost ~]$

OUTPUT 2
The program to display all prime numbers in a given array
Enter the limit of the array
5
Enter values in to array
7 9 6 1 2
The prime numbers are:
7
1
2
[141740@localhost ~]$

c progranm to find the factorial and ncr

/*...................................................................................................................................................
file name: rec13.c
program :Function sub program to find the factorial of a number and use that
function to find the nCr(ncr=n!/(n-r)|*r!)
Author : sujithbaby440@gmail.com
Date : 15-11-14
...................................................................................................................................................*/
#include<stdio.h>
long fnFact(int n);
main()
{
int n,r,i,a[10];
long ncr,factn,factr,factnr,nr;
system("clear");
printf("The progranm to find the factorial and ncr\n\n");
printf("Input the values of n and r\n");
scanf("%d\n%d",&n,&r);
nr=n-r;
factn=fnFact(n);
printf("factn= %d\n",factn);
factr=fnFact(r);
printf("factr= %d\n",factr);
factnr=fnFact(nr);
printf("factnr= %d\n\n",factnr);
ncr=factn/(factnr*factr);
printf("ncr= %ld ",ncr);
}
long fnFact(int n)
{
int i;
long fact=1;
for(i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}


OUTPUT 1
The progranm to find the factorial and ncr
Input the values of n and r
9 5
factn= 362880
factr= 120
factnr= 24
ncr= 126 [141740@localhost ~]$


OUTPUT 2
The progranm to find the factorial and ncr
Input the values of n and r
7 4
factn= 5040
factr= 24
factnr= 6
ncr= 35 [141740@localhost ~]$

C Program for sorting the rows and interchanging the columns

/*......................................................................................................................................
File : rec12.c
program : Menu driven program for
(1) Exchange the values of the two columns of a matrix m*n
(2) Sort the each rows of a matrix
Author : sujithbaby440@gmail.com
Date :13/11/2014
........................................................………………………………………………………….*/
#include<stdio.h>
main()
{
int i,j,m,n,a[30][30],choice,c1,c2,temp,x,y;
system("clear");
printf("Program for sorting the rows and interchanging the columns\n");
printf("\nEnter the number of rows and columns\n");
scanf("%d%d",&m,&n);
printf("Enter the elements into the %d*%d matrix\n",m,n);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Matrix:\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf(" Select your choice\n");
printf("\n (1) Exchange the values of the two columns of a matrix m*n");
printf("\n (2) Sort the each rows of a matrix\n");
scanf("%d",&choice);
if(choice==1){
printf("Enter the columns which are to be interchanged \n");
scanf("%d%d",&c1,&c2);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(j==c1){
temp=a[i][c1];
}if(j==c2){
a[i][c1]=a[i][c2];
a[i][c2]=temp;
}
}
}
printf("the interchanged matrix :\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}else if(choice==2){
printf("Sort all rows in the given matrix\n");
for(i=1;i<=m;i++){
for(x=1;x<=m-1;x++){
for(y=1;y<=m-x;y++){
if(a[i][y]>a[i][y+1]){
temp=a[i][y];
a[i][y]=a[i][y+1];
a[i][y+1]=temp;
}
}
}
}
printf("sorted Matrix:\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
}

OUTPUT 1
Program for sorting the rows and interchanging the columns
Enter the number of rows and columns
3 3
Enter the elements into the 3*3 matrix
1 2 3
4 5 6
7 8 9
Matrix:
1 2 3
4 5 6
7 8 9
Select your choice
(1) Exchange the values of the two columns of a matrix m*n
(2) Sort the each rows of a matrix
1
Enter the columns which are to be interchanged
1 3
the interchanged matrix :
3 2 1
6 5 4
9 8 7
[141740@localhost ~]$


OUTPUT 2
Program for sorting the rows and interchanging the columns
Enter the number of rows and columns
3 3
Enter the elements into the 3*3 matrix
4 5 8
1 1 3
7 9 6
Matrix:
4 5 8
1 1 3
7 9 6
Select your choice
(1) Exchange the values of the two columns of a matrix m*n
(2) Sort the each rows of a matrix
2
Sort all rows in the given matrix
sorted Matrix:
4 5 8
1 1 3
6 7 9
[141740@localhost ~]$

c program to input data into an m*n matrix and calculate the trace and normal of the matrix

/*...................................................................................................................................................
File :rec11.c
program: Write a Menu driven program to input data into an m*n matrix and calculate the
(1):Trace of the matrix
(2):norm of the matrix ie sqrt(sum of squares(a[i][j]))
author: sujithbaby440@gmail.com
.................................................................................................................................................. */
#include<stdio.h>
#include<math.h>
main()
{
int a[20][20],dsum=0,nsum=0,m,n,i,j,c;
float normal;
system("clear");
printf("Program to find the trace and norm of a matrix\n");
printf("Enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("The matrix\n");
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter your choice\n");
printf("(1):Trace of the matrix\n(2):norm of the matrix\n");
scanf("%d",&c);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(i==j){
dsum=dsum+a[i][j];
}
nsum=nsum+(a[i][j]*a[i][j]);
}
}
normal= sqrt(nsum);
if(c==1){
printf("The trace of the matrix is %d\n",dsum);
}
if(c==2){
printf("The norm of the matrix\t %f\n",normal);
}
}



OUTPUT 1
Program to find the trace and norm of a matrix
Enter the order of the matrix
3 3
Enter the elements of the matrix
1 5 6
7 8 9
1 2 3
The matrix
1 5 6
7 8 9
1 2 3
Enter your choice
(1):Trace of the matrix
(2):norm of the matrix
1
The trace of the matrix is 12
OUTPUT 2
Program to find the trace and norm of a matrix
Enter the order of the matrix
3 3
Enter the elements of the matrix
1 2 3
4 5 6
7 8 9
The matrix
1 2 3
4 5 6
7 8 9
Enter your choice
(1):Trace of the matrix
(2):norm of the matrix
2
The norm of the matrix 16.881943