C Programmes File
C Programmes File
1s
1. #include<stdio.h>
#include<conio.h>
main()
{
Int n,m,s;
Clrscr();
Printf(“\n enter 2 number”);
Scanf(“%d %d”,&n,&m);
s=n+m;
printf(“%d”,s);
getch();
}
2. main()
{
int n,sum,l,f;
clrscr();
printf("enter any no");
scanf("%d",&n);
l=n%10;
f=n/100;
sum=l+f;
printf("%d",sum);
getch();
}
3. #include<stdio.h>
#include<conio.h>
main()
{
int n1,n2,multi;
clrscr();
printf("Enter two no");
scanf("%d%d",&n1,&n2);
multi=n1*n2;
printf("%d",multi);
getch();
}
4. #include<stdio.h>
#include<conio.h>
main()
{
float f,c;
clrscr();
printf("Enter the celcius");
scanf("%f",&c);
f=(9*c/5)+32;
printf("In Fahrenheit=%f",f);
getch();
}
2
5. #include<conio.h>
main()
{
int n;
clrscr();
printf("enter the values");
scanf("%d%d",&a,&b);
a=b;
b=c;
c=a;
printf("%d",c);
getch()
}
6. #include<stdio.h>
#include<conio.h>
main()
{
int n,s=0,r;
clrscr();
printf("enter the value");
scanf("%d",&n);
r=n%10;
s=s+r;
n=n/10;
r=n%10;
s=s+r;
n=n/10;
s=s+n;
printf("%d",s);
getch();
}
7. main()
{
int r,a;
clrscr();
printf("enter the radius ");
scanf("%d",&r);
a=3.14*r*r;
printf("%d",a);
getch();
}
3
8. #include<stdio.h>
#include<conio.h>
main()
{
int p,c,h,e,m;
float per;
clrscr();
printf("five subjects marks");
scanf("%d%d%d%d%d",&p,&c,&h,&e,&m);
per=(p+c+h+e+m)/5;
printf("%f",per);
getch();
}
2. main()
{
int n1,n2,n3,n4,n5;
float p;
clrscr();
printf("enter 5 subject marks");
scanf("%d%d%d%d%d%",&n1,&n2,&n3,&n4,&n5);
p=(n1+n2+n3+n4+n5)/5;
if(p>=60>)
printf("1st div");
else if(p>=50)
printf("2nd div");
else if(p>=40)
printf("3rd div");
else
printf("fail");
getch();
}
4
3. #include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
5
4. main()
{
int n;
clrscr();
printf(“enter any number”);
scanf(“%d”,&n);
if(n>0)
printf(“negative”);
else
printf(“positive”);
getch();
}
5. main()
int age;
printf(“enter age”);
if(age>18)
printf(“Eligible for voating”);
else
printf(“Not eligible for voating”);
getch();
}
6. main()
{
int n,rev=0,temp,rem;
clrscr();
printf(“enter any 3 digit number”);
scanf(“%d”,&n);
temp=n;
rem=n%10;
rev=rev*10+rem;
n=n/10;
rem=n%10;
rev=rev*10+rem;
n=n/10;
rev=rev*10+n;
if(temp==n)
printf(“Palindrome”);
else
printf(“Not palindrome”);
getch();
}
6
7. main()
{
int n,rem,temp,arm=0;
printf(“enter any number”);
scanf(“%d”,&n);
temp=n;
rem=n%10;
arm=arm+(rem*rem*rem);
n=n/10;
rem=n%10;
arm=arm+(rem*rem*rem);
n=n/10;
arm=arm+(n*n*n);
if(temp==arm)
printf(“Armstrong”);
else
printf(“not armstrong”);
getch();
}
3 Class And 4 Class (Switch and GoTo statement)
1. main()
{
Char ch;
Printf(“enter any character=”);
Scanf(“%c”,&ch);
Switch(ch)
{
Case ‘a’: case ‘e’: case ‘i’: case ‘o’: case “u”:
Case ‘A’: case ‘E’: case ‘I’: case ‘O’: case “U”:
Printf(“\n vowel”);
Break;
Default:
Printf(“\n constant”);
}
Getch();
}
2. main()
{
Int d;
Printf(“enter any day no. (1-7)”);
Scanf(“%d”,&d);
Switch(d)
{
Case 1: printf(“sunday”);break;
Case 2: printf(“monday”);break;
Case 3: printf(“tuesday”);break;
Case 4: printf(“wednesday”);break;
Case 5: printf(“thursday”);break;
Case 6: printf(“friday”);break;
Case 7: printf(“saturday”);break;
default: printf(“wrong choice”);
}
Getch(); }
7
3. #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
float n1,n2,res;
char ch;
while(1)
{
clrscr();
printf("enter any two no\n");
fflush("stdin");
scanf("%f%f",&n1,&n2);
printf("\n\nselect desired operation");
printf("\nA->Addition");
printf("\nS->Substraction");
printf("\nD->Division");
printf("\nM->Multiplication");
printf("\nE->Exit");
printf("\n\nenter ur choice\n");
fflush("stdin");
scanf("%c",&ch);
switch(ch)
{
case 'A':
case 'a':
res=n1+n2;
printf("\nResult=%f",res);
break;
case 'S':
case 's':
res=n1-n2;
printf("\nResult=%f",res);
break;
case 'M':
case 'm':
res=n1*n2;
printf("\nResult=%f",res);
break;
case 'D':
case 'd':
res=n1/n2;
printf("\nResult=%d",res);
break;
case 'E':
case 'e':
exit(0);
break;
default:
printf("\nInvalid choice");
}
getch();
}
8
4. main()
{
Float n1,n2,res;
Int ch;
Printf(“enter any 2 number”);
Scanf(“%f%f”,&n1,&n2);
Printf(“\n\n select desired operation \n”);
Printf(“\n1->”Addition”);
Printf(“\n2->”Subtraction”);
Printf(“\n3->”Division”);
Printf(“\n4->”Multiplication”);
Printf(“\n5->”Exit”);
Printf(“\n\n enter ur choice=”);
Scanf(“%d,&ch”);
Switch(ch)
{
Case 1:
Res=n1+n2;
Printf(“\n Result=%f”,res);
Break;
Case 2:
Res=n1-n2;
Printf(“\n Result=%f”,res);
Break;
Case 3:
Res=n1/n2;
Printf(“\n Result=%f”,res);
Break;
Case 4:
Res=n1*n2;
Printf(“\n Result=%f”,res);
Break;
Case 5:
Exit(0);
Break;
Default:
Printf(“\n Invalid Choice”);
}
Getch();
}
9
GoTo label
1. main()
{
Int n1,t,i,rem,rev=0;
Printf(“\n plz enter any 3 digit number”);
Scanf(“%d”,&n1);
t=n1;
reverse:
rem=t%10;
rev=rev*10+rem;
t=t/10;
if(t>0)
goto reverse;
if(n1==rev)
printf(“\n palindrome”);
else
printf(“\n not palindrome”);
getch();
}
2. main()
{
Int n;
Printf(“\n plz input only positive no.”);
Scanf(“%d”,&n);
re_input:
if(n<0)
{
Printf(“\n plz input only positive number”);
goto re_input;
}
printf(“\n u enter positive no.”);
getch();
}
3. main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",i*i);
}
getch();
}
4. main()
{
int n,i,min;
clrscr();
printf("enter 10 no.\n");
for(i=1;i<=10;i++)
{
10
scanf("%d",&n);
if(i==1)
min=n;
if(min>n)
min=n;
}
printf("minimum no.=%d",min);
getch();
}
5. 1
00
111
0000
11111
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i%2);
}
printf("\n");
}
getch();
}
6. main()
{
Int n1,i=1;
Int p=0,n=0,e=0,o=0;
input:
printf(“\n plz input % number=”,i);
scanf(“%d”,&n1);
if(n1<0)
n++;
else
11
p++;
if(n1%2==0)
e++;
else
o++;
i++;
if(i<=10)
goto input;
printf(“\n number of positive number=%d”,p);
printf(“\n number of negative number=%d”,n);
printf(“\n number of even number=%d”,e);
printf(“\n number of odd number=%d”,o);
getch();
}
2. main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
3. main()
{int i;
for(i=10;i>=1;i--)
printf(“\n %d”,i);
getch();
}
4. main()
{
int I,n,sum=0;
for(i=1;i<=10;i++)
12
{
Scanf(“%d”,&n);
sum=sum+n;
}
Printf(“\n %d”, sum);
getch();
}
5. main()
{
int i;
for(i=100;i<=200;i++)
{
if(i%2==0)
printf(“%d”,i);
}getch();
}
6. main()
{
Int n,I,fact=1;
Printf(“enter any number=”);
Scanf(“%d”,&h);
For(i=1;i<=n;i++)
{
Fact=fact*I;
}
Printf(“Factorial=%d”,fact);
Getch();
}
7. main()
{
Int I;
For(i=’A’;i<=’Z’;i++)
{
Printf(“%c”,i);
}
Getch();
}
8. #include <stdio.h>
int main()
{
int x;
x = 0;
do {
/* "Hello, world!" is printed at least one time
even though the condition is false */
printf( "Hello, world!\n" );
13
} while ( x != 0 );
getchar();
}
9. main()
{
Int n,I,per=0;
Printf(“enter any number”);
Scanf(“%d”,&n);
For(i=1;i<=n/2;i++)
{
if(n%i==0)
per=per+I;
}
Printf(“perfect number”);
Else
printf(“not perfect number”);
getch();
}
10. main()
{
Int n1,n2,res,I;
N1=0;
N2=1;
Printf(“%d%d”,&n1,&n2);
For(i=1;i<=8;i++)
{
Res=n1+n2;
Printf(“%d”,res);
n1=n2;
n2=res;
}
Getch();
}
11. main()
{
Int I,j;
For(i=1;i<=5;i++)
{
For(j=1;j<=5;j++)
{
Printf(“%d”,i);
}
Printf(“\n”);
Getch();
}
14
12. *
**
***
****
*****
Main()
{
Int I;j;
For(i=1;i<=5;i++)
{
For(j=1;j<=I;j++)
Printf(“*”);
Printf(“\n”);
}
Getch();
}
13. 1
12
123
1234
12345
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
14. *
**
***
****
*****
main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
15
}
getch();
}
15. A
AB
ABC
ABCD
ABCDE
main()
{
int i,j;
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
printf("%c",j);
printf("\n");
}
getch();
}
int main()
{
int x;
/* The loop goes while x < 10, and x increases by one every loop*/
for ( x = 0; x < 10; x++ ) {
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */
printf( "%d\n", x );
}
getchar();
}
int main()
{
int x = 0; /* Don't forget to declare variables */
int main()
{
int x;
x = 0;
do {
/* "Hello, world!" is printed at least one time
even though the condition is false */
printf( "Hello, world!\n" );
} while ( x != 0 );
getchar();
}
int main()
{
int x;
x = 0;
do {
/* "Hello, world!" is printed at least one time
even though the condition is false */
printf( "Hello, world!\n" );
} while ( x != 0 );
getchar();
}
int main()
int i = 15, j;
while(i>0)
// display i==
17
printf("%d==", i);
j=i-1;
while(j>0)
// display #
printf("#");
j = j - 1;
printf("\n");
i = i - 1;
return 0;
21. array
#include <stdio.h>
#define SIZE 10
int main()
{
int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
int i, j;
printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
for(i = 0; i< SIZE; ++i)
{
printf( "%7d%13d ", i, n[ i ]) ;
printf( "\n" );
18
return 0;
}
22. #include <stdio.h>
#define SIZE 5
printf( "\n" );
getchar();
return 0;
}
void modifyElement(int i)
{
printf( "Value in modifyElement is %d\n", i *= 2 );
}
23. string
#include <stdio.h>
int main() {
char s1 [] = "My House is small";
19
1. main()
{
int arr[5],i;
printf("enter number");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
printf("\n%d",arr[i]);
}
getch();
}
2. main()
{
int no[10],i,n=0,p=0;
printf("enter 10 no.");
for(i=0;i<=9;i++)
scanf("%d",&no[i]);
for(i=0;i<=9;i++)
if(no[i]<0)
n++;
else
p++;
printf("negative no %d",n);
printf("positive no %d",p);
getch();
}
3. main()
{
int i,j;
int a[3][3],b[3][3],c[3][3];
clrscr();
printf("enter 9 value for 1st array \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("enter another 9 values for 2nd array\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
20
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("the original 1st array is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("d original 2nf array is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
printf("\n Addition of 2 array is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
getch();
}
4. main()
{
int m,n,c,d,first[4][4],second[4][4],sum[4][4];
clrscr();
printf("Rows and columns of matrix");
scanf("%d%",&m,&n);
printf("element of first matrix\n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&first[c][d]);
printf("elements of 2nd matrix\n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&second[c][d]);
for(c=0;c<m;c++)
for(d=0;d<n;d++)
sum[c][d]=first[c][d]+second[c][d];
printf("sum of entered mtrices-\n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
printf("%d\t",sum[c][d]);
21
printf("\n");
}
getch();
}
5. #include<stdio.h>
#include<conio.h>
main()
{
int avg,sum=0;
int i;
int marks[30];
for(i=0;i<=29;i++)
{
printf("\n enter marks");
scanf("%d",&marks[i]);
}
for(i=0;i<=29;i++)
sum=sum+marks[i];
avg=sum/30;
printf("\n average marks=%d",avg);
getch();
}
7.
main()
{
int m,n,p,q,c,d,k,sum=0;
int first[10][10],second[10][10],multiply[10][10];
printf("enter number of rows and columns of first matrix \n");
scanf("%d%d",&m,&n);
printf("enteer the element of first matrix \n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&first[c][d]);
printf("enter the number of rows and columns oof second matrix \n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("matrces with entere orders can't be multiplied with eachg other \n");
else
{
printf("enter the element of second matrix \n");
for(c=0;c<p;c++)
for(d=0;d<q;d++)
scanf("%d",&second[c][d]);
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
{
for(k=0;k<p;k++)
{
sum=sum+first[c][k]*second[k][d];
}
multiply[c][d]=sum;
sum=0;
}
}
printf("product of entered matrices \n");
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
printf("%d \t",multiply[c][d]);
printf("\n");
}
}
getch();
}
8. main()
{
int m,n,c,d,matrix[10][10],trans[10][10];
printf("enter d no. of rows n columns of matrix");
23
scanf("%d%d",&m,&n);
printf("enter d elements of matrix \n");
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
scanf("%d",&matrix[c][d]);
}
}
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
trans[d][c]=matrix[c][d];
}
}
printf("transpose of entered matrix:-\n");
for(c=0;c<m;c++)
{
for(d=0;d<m;d++)
{
printf("%d \t",trans[c][d]);
}
printf("\n");
}
getch();
}
3. main()
{
24
Class 15,16,17,1819,
2. int cube(int);
void main()
{
int i;
for(i=1;i<=10;i++)
printf("%d\n",cube(i));
}
int cube(int y)
{
int v;
v=y*y*y;
printf("%d",v);
getch();
}
25
3. main()
{
int a,b;
void swap(int a, int b);
a=10;
b=20;
printf("values of a before function call: %d \n",a);
printf("value of a, b after function call: %d \n",b);
getch();
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("value of a during function: %d \n",x);
printf("value of b during function: %d \n",y);
}
4. swapr(int*,int*);
main()
{
int a=10,b=20;
swapr(&a,&b);
printf("\na=%d b=%d",a,b);
}
swapr(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
getch();
}
3. #include
<stdio.h>
int main(void)
{
char ch = 'c';
char *chptr = &ch;
int i = 20;
int *intptr = &i;
float f = 1.20000;
float *fptr = &f;
char *ptr = "I am a string";
printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0;
}
26
4. #include <stdio.h>
int main()
{
int x, y;
int area, perim;
printf("Enter two values separated by space: " );
scanf("%d %d", &x, &y);
rectangle(x, y, &area, &perim);
printf("Area is %d Perimeter is %d\n", area, perim);
return 0;
}
5. #include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
27
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
6. #include<stdio.h>
#include<conio.h>
int mult(int x,int y);
int main()
{
int x;
int y;
printf("please input 2 no. to be multipied");
scanf("%d",&x);
scanf("%d",&y);
printf("the product of ur two number is %d\n", mult(x,y));
}
int mult(int x,int y)
28
{
int z;
z=x*y;
printf("%d",z);
getch();
}
7. main()
{
int balance;
int *address;
int value;
balance=5000;
address =& balance;
value=*address;
printf("balance is %d\n",value);
getch();
}
8. include<stdio.h>
void message();
void main()
{
message();
printf("\n hello,r,u,fine?");
}
void message()
{
printf("\n smile and the world smile with you");
getch();
}
9. #include <stdio.h>
int power(int n1,int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&exp);
29
10. #include<stdio.h>
int add(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Sum = %d",add(n));
return 0;
}
int add(int n)
{
if(n!=0)
return n+add(n-1); /* recursive call */
}
return(1);
rs=no*(fact(no-1));
return(rs);
}
int main(void)
{
struct tag *st_ptr; /* a pointer to a structure */
st_ptr = &my_struct; /* point the pointer to my_struct */
strcpy(my_struct.lname,"Jensen");
strcpy(my_struct.fname,"Ted");
printf("\n%s ",my_struct.fname);
printf("%s\n",my_struct.lname);
my_struct.age = 63;
show_name(st_ptr); /* pass the pointer */
return 0;
}
14. #include<stdio.h>
#include<conio.h>
void reverse();
int main()
{
printf("enter a sentence:");
reverse();
getch();
}
void reverse()
{
char c;
scanf("%c",&c);
if(c!='\n')
{
reverse();
printf("%c",c);
}
}
32
Class 22,23,24,25
1. struct book
{
char name[20];
int pages;
float price;
};
main()
{
struct book b1;
clrscr();
printf("enter book name");
gets(b1.name);
printf("\nenter pages and price");
scanf("%d%f",&b1.pages,&b1.price);
printf("book name %s",b1.name);
printf("\npages%d price%f",b1.pages,b1.price);
getch();
}
2. struct class
{
int number;
char name[20];
float marks;
};
main()
{
int x;
struct class student1={111,"Rao",72.50};
33
3. struct marks
{
int sub1;
int sub2;
int sub3;
int total;
};
main()
{
inti;
struct marks student[3]={{45,67,81,0},
{75,53,69,0},
{57,36,71,0}};
struct marks total;
for(i=0;i<=2;i++)
{
student[i].total=student[i].sub1+
student[i].sub2+
student[i].sub3;
total.sub1=total.sub1+student[i].sub1;
total.sub2=total.sub2+student[i].sub2;
total.sub3=total.sub3+student[i].sub3;
total.total=total.total+student[i].total;
}
printf("STUDENT TOTLA\n\n");
for(i=0;i<=2;i++)
printf("Studnt[%d] %d\n",i+1,student[i].total);
printf("\nSUBJECT TOTAL\n\n");
printf("%s %d\n%s %d\n%s %d\n",
"Subject 1 ",total.sub1,
"Subject 2 ",total.sub2,
"Subject 3",total.sub3);
printf("\nGrand Total=%d\n",total.total);
getch();
}
34
5. typedefstruct complex
{
float real;
floatimag;
}complex;
complex add(complex n1,complex n2);
int main()
{
complex n1,n2,temp;
printf("for 1st complex number\n");
printf("enter real and imaginaryrespectively:\n");
scanf("\n for 2nd complex number\n");
printf("enter real and imaginary respectively:\n");
scanf("%f%f",&n2.real,n2.imag);
temp=add(n1,n2);
printf("sum=%f+%fi",temp.real,temp.imag);
return 0;
}
complex add(complex n1,complex n2)
{
complex temp;
temp.real=n1.real+n2.real;
temp.imag=n1.imag+n2.imag;
return(temp);
#include <stdio.h>
struct student {
charfirstName[20];
charlastName[20];
char SSN[10];
floatgpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
4. #include <stdio.h>
#define N 4 struct student
{
35
char name[20];
inteng;
int math;
intphys;
double mean;
};
staticstruct student data[]={ {"Jack", 82, 72, 58, 0.0}, {"Young", 77, 82, 79, 0.0}, {"Steeve", 52, 62, 39, 0.0},
{"Mark", 61, 82, 88, 0.0} };
int main(void)
{
inti, j; // Calculation of mean of three subject
for(i=0; i<N; i++)
{
data[i].mean = (data[i].eng + data[i].math + data[i].phys)/3.0; }
for(i=0; i<N; i++)
{
printf("%7s: Eng = %3d Math = %3d Phys = %3d: Mean = %5.1f\n", data[i].name, data[i].eng, data[i].math,
data[i].phys, data[i].mean);
}
return (0);
}
3. #include
<stdio.h>
#define N 4
struct student
{ char name[20];
inteng;
int math;
intphys;
};
staticstruct student data[]={ {"Jack", 82, 72, 58}, {"Young", 77, 82, 79}, {"Steeve", 52, 62, 39}, {"Mark", 61,
82, 88} };
int main(void)
{
inti;
for(i=0; i<N; i++)
{
36
printf("%7s: Eng = %3d Math = %3d Phys = %3d\n", data[i].name, data[i].eng, data[i].math, data[i].phys);
}
return (0);
}
1. #include <stdio.h>
struct Distance
{
int feet; float inch;
}
d1,d2,sum;
int main()
{
printf("1st distance\n");
printf("Enter feet: ");
scanf("%d",&d1.feet); /* input of feet for structure variable d1 */
printf("Enter inch: ");
scanf("%f",&d1.inch); /* input of inch for structure variable d1 */
printf("2nd distance\n");
printf("Enter feet: ");
scanf("%d",&d2.feet); /* input of feet for structure variable d2 */
printf("Enter inch: ");
scanf("%f",&d2.inch); /* input of inch for structure variable d2 */
sum.feet=d1.feet+d2.feet;
sum.inch=d1.inch+d2.inch;
if (sum.inch>12)
{
//If inch is greater than 12, changing it to feet.
++sum.feet;
sum.inch=sum.inch-12; }
printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch); /* printing sum of distance d1 and d2 */ return
0;
}
37
2. void main()
{
inti;
struct student
{
longintrollno;
char sex;
float height;
float weight;
};
struct student data[3]={{121,'m',5.7,59.8},
{122,'f',6.0,65.2},
{123,'m',6.2,75.5}
};
clrscr();
printf("the initialized contents are:");
for(i=0;i<=2;i++)
{
printf("\n **RECORDS: \N");
printf("%c \n",data[i].sex );
printf("%f \n",data[i].height);
printf("%f \n",data[i].weight);
}
getch();
}
3.
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
4. #include <stdio.h>
#define N 4 struct student
{
char name[20];
inteng;
int math;
intphys;
38
};
staticstruct student data[]={ {"Jack", 82, 72, 58}, {"Young", 77, 82, 79}, {"Steeve", 52, 62, 39}, {"Mark", 61,
82, 88} };
int main(void)
{
inti;
for(i=0; i<N; i++)
{
printf("%7s: Eng = %3d Math = %3d Phys = %3d\n", data[i].name, data[i].eng, data[i].math, data[i].phys);
}
return (0);
}
5. struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
main()
{
struct personal person;
printf("input values \n");
scanf("%s%d%s%d%f",
person.name,
&person.day,
person.month,
&person.year,
&person.salary);
printf("%s%d%s%d%f
\n",person.name,person.day,person.month,person.year,person.salary);
getch();
}
linked list
1. #include <stdio.h>
#include <stdlib.h>
struct node
{
39
int item;
struct node *next;
};
int main()
{
struct node *root; /* This will be the unchanging first node */
root->next = 0;
root->item = 777;
conductor = root;
int x;
conductor = conductor->next;
conductor->item = x;
}
2. #include <stdio.h>
40
#include <stdlib.h>
struct NODE {
int number;
};
int main(void) {
intnum = 0;
int input = 1;
intretval = 0;
llist->number = 0;
llist->next = NULL;
while(input != 0) {
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);
switch(input) {
41
case 0:
default:
printf("Goodbye ...\n");
input = 0;
break;
case 1:
scanf("%d", &num);
append_node(llist, num);
break;
case 2:
scanf("%d", &num);
delete_node(llist, num);
break;
case 3:
scanf("%d", &num);
else
break;
case 4:
42
display_list(llist);
break;
} /* switch */
} /* while */
free(llist);
return(0);
while(llist->next != NULL) {
llist = llist->next;
printf("%d", llist->number);
while(llist->next != NULL)
llist = llist->next;
llist->next->number = num;
llist->next->next = NULL;
if(llist->number == num) {
temp = llist->next;
free(llist);
llist = temp;
} else {
while(llist->next->number != num)
llist = llist->next;
temp = llist->next->next;
free(llist->next);
llist->next = temp;
intretval = -1;
inti = 1;
while(llist->next != NULL) {
if(llist->next->number == num)
returni;
else
i++;
llist = llist->next;
44
returnretval;
9. #include <stdio.h>
#include <string.h>
#define MAX_BUF 256 long arr[10] = { 3,6,1,2,3,8,4,1,7,2};
char arr2[5][20] = { "Mickey Mouse", "Donald Duck", "Minnie Mouse", "Goofy", "Ted Jensen" };
void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *));
intcompare_string(const void *m, const void *n);
intcompare_long(const void *m, const void *n);
int main(void) {
inti;
puts("\nBefore Sorting:\n");
for (i = 0; i< 10; i++) /* show the long ints */
{
printf("%ld ",arr[i]);
}
puts("\n");
for (i = 0; i< 5; i++) /* show the strings */
{
printf("%s\n", arr2[i]);
}
bubble(arr, 4, 10, compare_long); /* sort the longs */
bubble(arr2, 20, 5, compare_string); /* sort the strings */
puts("\n\nAfter Sorting:\n");
for (i = 0; i< 10; i++) /* show the sorted longs */
{
printf("%d ",arr[i]);
}
puts("\n");
for (i = 0; i< 5; i++) /* show the sorted strings */
{
printf("%s\n", arr2[i]);
}
return 0;
}
45
void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *))
{
inti, j, k;
unsigned char buf[MAX_BUF];
unsigned char *bp = p;
for (i = N-1; i>= 0; i--)
{
for (j = 1; j <= i; j++)
{
k = fptr((void *)(bp + width*(j-1)), (void *)(bp + j*width));
if (k > 0)
{
memcpy(buf, bp + width*(j-1), width);
memcpy(bp + width*(j-1), bp + j*width , width);
memcpy(bp + j*width, buf, width);
}}} }
intcompare_string(const void *m, const void *n)
{
char *m1 = (char *)m;
char *n1 = (char *)n;
return (strcmp(m1,n1));
}
intcompare_long(const void *m, const void *n)
{
long *m1, *n1;
m1 = (long *)m;
n1 = (long *)n;
return (*m1 > *n1);
}
3. #include<stdio.h>
main()
{
FILE *f1,*f2,*f3;
intnumber,i;
printf("contents of DATA file\n\n");
f1=fopen("DATA","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
46
if(number==-1)break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f3);
else
putw(number,f2);
}
fclose9f10;
fclose9f20;
fclose9f30;
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\ncontents of ODD file\n\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\ncontents of EVEN file\n\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
}
47
1. #include<stdio.h>
main()
{
FILE*f1;
char c;
printf("data input \n\n");
/*open the file INPUT*/
f1=fopen("INPUT","w");
/*get a character from keyboard*/
while((c=getchar())!=EOF)
put(c,f1);
fclose (f1);
printf("\n data output \n\n");
/*reopen the file INPUT */
fclose(f1);
printf("\n data output\n\n");
/*reopen the file INPUT */
f1=fopen ("INPUT","r");
/*read a character from INPUT*/
while ((c=get char(f1))!=EOF)
printf("%c",c);
fclose(f1);
}
2. #include<stdio.h>
#include<stdlib.h>
#define NULL 0
structlinked_list {
int number;
structlinked_list *next;
};
typedefstructlinked_list node;
main(){
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head=(node *)malloc(sizeof(node));
create(head);
48
printf("\n");
printf("head");
printf("\n");
printf("\n Number of items=%d \n",count(head));
}
void create(node *list){
printf("Input a number\n");
printf("(type -999 at end):");
scanf("%d", &list->number);
if(list->number== -999){
list->next = NULL;
}
Else {
list->next=(node *)malloc(sizeof(node));
create(list->next);
}
return;
}
void print(node *list) {
if(list->next != NULL)
{
printf("%d-->",list->number);
if(list->next==NULL)
printf("%d",list->next->number);
printf(list->next);
}
return;
}
int count(node *list){
if(list->next==NULL)
return (0);
else
return(1+count(list->next));
}
3. #include<stdio.h>
main()
{
FILE *fp;
intnum,qty,I;
float price,value;
char item[10],filename[10];
printf(“Input filename”);
scanf(“%s”,filename);
fp=fopen(filename,”w”);
printf(“Input inventory datann”0;
printf(“Item namem number price quantityn”);
49
4. #include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int X_DIM=16;
int Y_DIM=5;
int Z_DIM=3;
int main(void) { char *space; char ***Arr3D;
int y, z; ptrdiff_t diff; /* first we set aside space for the array itself */
space = malloc(X_DIM * Y_DIM * Z_DIM * sizeof(char)); /* next we allocate space of an array of
pointers, each to eventually point to the first element of a 2 dimensional array of pointers to pointers */
Arr3D = malloc(Z_DIM * sizeof(char **)); /* and for each of these we assign a pointer to a newly allocated
array of pointers to a row */
for (z = 0; z < Z_DIM; z++) { Arr3D[z] = malloc(Y_DIM * sizeof(char *)); /* and for each space in this
array we put a pointer to the first element of each row in the array space originally allocated */
for (y = 0; y < Y_DIM; y++) { Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM); } } /* And,
now we check each address in our 3D array to see if the indexing of the Arr3d pointer leads through in a
continuous manner */ for (z = 0; z < Z_DIM; z++)
{
printf("Location of array %d is %p\n", z, *Arr3D[z]); for ( y = 0; y < Y_DIM; y++)
{
printf(" Array %d and Row %d starts at %p", z, y, Arr3D[z][y]); diff = Arr3D[z][y] - space; printf(" diff =
%d ",diff); printf(" z = %d y = %d\n", z, y);
50
}
}
return 0;
}
7. #include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
{
char first[100],second[100],*temp;
printf("enter 1st string");
gets(first);
printf("enter 2nd string");
gets(second);
printf("\n before swapping\n");
printf("first string:%s\n",first);
printf("first string:%s\n\n",second);
temp=(char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("after swapping\n");
printf("first string:%s\n",first);
printf("second string:%s\n",second);
getch();
}