Feb 27, 2012

c programs

PROBLEM 1: To find volume of a cylinder: l*b*h

PROCEDURE:-

1: input values for length,breadth,height
2: find the product (length*breadth*height)
3:print the above result

CODE:-

#include
#include
void main()
{
int l,b,h, vol;
printf(“enter values for length, breadth,height of cylinder”);
scanf(“%d %d %d”, &l,&b,&h);
vol=l*b*h;
printf(“\n volume of cylinder is %d”, vol);
}


Input:- enter values for length, breadth,height of cylinder
A = 5, B=5, C=5

Ouput:- volume of cylinder is 125

PROBLEM 2: To find area of triangle : sqrt(s*(s-a)*(s-b)*(s-c))

PROCEDURE:-

1.Input values for three sides of triangle
2.find s value with the formulae (a+b+c)/2
3.find area with the formulae sqrt(s(s-a)(s-b)(s-c))
4.print the above result


CODE:-

#include
#include
#include
void main()
{
int a,b,c;
float s, area;
printf(“enter values for three sides of triangle”);
scanf(“%d %d %d”, &a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“\n area of triangle with sides a=%d, b=%d, c=%d is %f”, a,b,c,area);
}


Input :- enter values for three sides of triangle
a=2, b=2, c=2

Output:- area of triangle with sides a=2, b=2, c=2 is 1.732

PROBLEM 3: Convert Celcious temperature to Farenheit.



PROCEDURE:-

1. input value for celcious
2. find farenheit value with the formulae (9/5)c+32
3. print the above result


CODE:-

#include
void main()
{
float c,f;
printf(“enter a value in celcious\n”);
scanf(“%f”,&c);
f=(9.0/5)*c+32;
printf(“temperature in farenheit is %f”,f);
}

Input:- enter a value in celcious
35

Output:- temperature in farenheit is 95.0000

PROBLEM 4: Calculate Simple Interest using (p*t*r)/100.


PROCEDURE:-

1. input values for principal amount, time,rate of interest
2. find simple interest with the formulae (p*t*r)/100
3. print the above result


CODE:-

#include
#include
void main()
{
int p,t,r;
float si;
printf(“enter values p,t,r”);
scanf(“%d %d %d”, &p,&t,&r);
si=(p*t*r)/100.0;
printf(“\n Simple interest is = Rs .%f”, si);
}



Input: - enter values p,t,r
P=10000, t=10, r=3

Output:- Simple interest is =Rs.3000

PROBLEM 5:-Find Value of S=ut+1/2*a*t**2.


PROCEDURE:-

1.enter values for u,a,t to find distance
2.find distance with the formulae ut+1/2at2
3.print the above result



CODE:-


#include
#include
void main()
{
float u,t,a,S;
clrscr();
printf(“enter values u,t,a”);
scanf(“%f %f %f”, &u,&t,&a);
S=(u*t)+(0.5*a*t*t);
printf(“\n S = %f”, S);
}



Input:- enter values u,t,a
U=10,t=4,a=4.9

Output:- S =79.200

PROBLEM 6:- Find Sum to first n numbers (using goto).


PROCEDURE:-


1. input value for n
2. set initial values for sum as 0 and I as 1
3. add I to sum and increment I by 1
4. if I is less than or equal to n goto step 3
5. print the sum


CODE:-


#include
void main()
{
int n, i=1, sum=0;
printf(“enter value for n”);
scanf(“%d”,&n);
abc:
sum=sum+i;
i=i+1;
if (i<=n) goto abc; printf(“sum upto %d numbers is %d”,n,sum); } Input:- enter value for n n=5 Output:- sum upto 5 numbers is 15 PROBLEM 7:- Swap two numbers.( using temporary variable). PROCEDURE:- 1.input values for a and b 2. store value of a in t 3. store value of b in a 4. store value of t in b 5.print value of a and b after swapping CODE:- #include
void main()
{
int a,b,t;
printf(“enter value for a,b”);
scanf(“%d %d”,&a,&b);
printf(“before swapping a=%d b=%d”,a,b);
t=a;
a=b;
b=t;
printf(“after swapping a=%d b=%d”,a,b);
}




Input:- enter value for a,b
a=5, b=6

Output:- after swapping a=6 b=5

PROBLEM 8:- Swap Two numbers (without using temporary variable)



PROCEDURE:-

1.input values for a and b
2. compute a= a+b
3. compute b =a-b
4. compute a=a-b
5.print value of a and b after swapping


CODE:-

#include
void main()
{
int a,b,t;
printf(“enter value for a,b”);
scanf(“%d %d”,&a,&b);
printf(“before swapping a=%d b=%d”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping a=%d b=%d”,a,b);
}


Input:- enter value for a,b
a=5, b=6

Output:- after swapping a=6 b=5

PROGRAMS USING STANDARD FUNCTIONS



PROBLEM 9:- To convert Upper case character to lower case and vice-versa.



PROCEDURE:-

1.input a character
2.if the given character is small case convert it to upper case
3.else if the given character is upper case convert it to lower case
4. after conversion print the character


CODE:-

#include
#include
void main()
{
char c;
printf(“enter a character”);
c=getchar();
if (islower(c))
putchar(toupper(c));
else
putchar(tolower(c));
}


Input:- enter a character
a
Output:- A

Input:-enter a character
A
Output:-a

PROGRAMS USING IF,IF-ELSE,CASE.
PROBLEM 10:- Greatest of 3 numbers.( Nested IF-ELSE)

PROCEDURE:-

1. input values for a,b,and c.
2. check whether a is greater than b and c
3. if so a will be greatest
4. if not c will be greatest
5. if a is not greater than b and c
6. then check whether b is greater than c
7. if yes b is greatest
8. if no c is greatest


CODE:-

#include
void main()
{
int a,b,c;
printf (“enter values for a,b,c”);
scanf(“%d %d %d”,&a,&b.&c);
if(a>b)
{
if(a>c)
printf(“ %d is the greatest “,a);
else
printf(“%d is the greatest”,c);
}
else {
if(b>c)
printf(“%d is the greatest”,b);
else
printf(“%d is the greatest”,c);
}
}

Input:- enter values for a,b,c
a=5, b=6, c=7

Output:- 7 is geatest
PROBLEM 11:- Find greatest of 3 numbers.(Using conditional operator)



PROCEDURE:-

1.input values for a,b,and c.
2.check whether a is greater than b and c
3.if so a will be greatest
4.if not c will be greatest
5.if a is not greater than b and c
6.then check whether b is greater than c
7.if yes b is greatest
8.if no c is greatest


CODE:-


#include
void main()
{
int a,b,c,y;
printf(“enter values for a,b,c”);
scanf(“%d %d %d “,&a,&b,&c);
y=(a>b)?(((a>c)?a:c) : ((b>c)?b:c))
printf(“%d is the greatest “,y);
}




Input:- enter values for a,b,c
a=6, b=8, c=7

Output:- b =8 is geatest










PROBLEM 12:- Write a C program to read a number and to print the number in words.(EX:=384=three eight four)in switch case.

PROCEDURE:-

1.input an integer
2.reverse the given integer
3. repeat steps 4 to 6 reversed integer is greater than 0
4. find modulo 10 value
5. print the result of step 4 in words
6. find new value of reversed number by dividing by 10.


CODE:-

#include
void main()
{
int a,rev=0,n;
printf(“enter a number”);
scanf(“%d”,&a);
while(a>0)
{
n=a%10;
rev=rev*10+n;
a=a/10;
}
while(rev>0)
{
n=rev%10;
switch(n)
{
case 1: printf(“one”); break; case 2: printf(“two”); break;
case 3: printf(“three”); break; case 4: printf(“four”); break;
case 5: printf(“five”); break; case 6: printf(“six”); break;
case 7: printf(“seven”);break; case 8: printf(“eight”); break;
case 9: printf(“nine”); break; case 0: printf(“zero”); break;
}
rev=rev/10;
}
}

Input:- Enter a number 123
Output:-one two three

PROBLEM 13:-Generate the electricity bill based on the specifications:
Domestic >100 units Rs 1.50
101 – 300 units Rs 2.00
301 – 500 units Rs 2.50
L.T.Consumer 501 – 1000 units Rs 4.00
H.T.Consumer 1001- 2000 units Rs 5.00

PROCEDURE:-

1.input no. of units consumed
2.according to the above specifications find the amount to be paid
3.print the above result


CODE:-

#include
void main()
{
int units;
float price;
printf(“enter the number of units “);
scanf(“%d”,&units);
if(units<100) price=units*1.50; else if(units>100&&units<=300) price=units*2.00; else if(units>300&&units<=500) price=units*2.50; else if (units>500&&units<=1000) price=units*4.00; else if(units>1000&&units<=2000) price=units*5.00; printf(“the total bill is %f”,price); } Input:- enter the number of units 320 Output:-the total bill is 800.00 PROBLEM 14:-(Switch Case) print result : grade on prercentage Percentage > 75 Distinction.
60 - 74 First Class.
50 - 59 Second Class.
40 - 49 Third Class.
Percentage< 40 Fail. PROCEDURE:- 1.input the total marks(for ten subjects) 2.find percentage(marks/10) 3. if above percentage is 10 or 9 or 8 or 7 grade is distinction 4.if percentage is 6 grade if first 5.if percentage is 5 grade is second 6.if percentage is 4 grade is third 7. if percentage is less than 4 grade is fail CODE:- #include
void main()
{
int x,marks;
printf(“enter the marks of the students”);
scanf(“%d”,&marks);
x=marks/10;
switch(x)
{
case 10:
case 9:
case 8:
case 7: printf(“Distinction”);
break;
case 6: printf(“first class”);
break;
case 5: printf(“second class”);
break;
case 4: printf(“third class”);
break;
case 3: case 2: case 1: printf(“fail”); break;
default: printf(“enter correct marks”);
}
}


Input:- enter the marks of the students 755
Output:- distinction

PROBLEM 15:- Generate the Net Salary with the given terms
SALARY < 22000 Nil 22001 - 30000 20 % of income >20000
30001 - 50000 1600 + 25 % of income >30000
50001 - 75000 6600 + 40 % of income >50000
> 75000 16600 + 50 % of income>75000


PROCEDURE:-
1. input salary of the employee
2. based on the above specifications calculate net salary
3. printf the net salary

CODE:-

#include
void main()
{
long double sal, netsal;
printf(“enter the salary of the employee”);
scanf(“%Lf”,&sal);
if(sal<22000) netsal=sal; else if(sal>22000&&sal<=30000) netsal=(sal-20000)*0.2; else if(sal>30000&&sal<=50000) netsal=1600+(sal-30000)*0.25; else if(sal>50000&&sal<=75000) netsal=6600+(sal-50000)*0.40; else netsal=16600+(sal-75000)*0.5; printf(“the net salary of the employee is :%Lf”,netsal); } Input:- enter the salary of the employee 60000 Output:- the net salary of the employee is 10600 PROGRAMS USING LOOPS (WHILE ,DO_WHILE,FOR) PROBLEM 16:- Sum and average of n different numbers. PROCEDURE:- 1. input an integer value n and set sum to 0 2. repeat step 3 until all the n different values are read 3. read an integer add this to previous sum 4. print the sum CODE:- #include
void main()
{
int n,x,i=1;sum=0;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter %d different values”,n);
while(i<=n) { scanf(“%d”,&x); sum=sum+x; i++; } printf(“sum of %d numbers is %d”, n, sum); } Input:- enter value for n 4 enter 4 different values 20 20 20 20 Output:- sum of 4 numbers is 80 PROBLEM 17:- Find Sum of even and odd in a series of numbers. PROCEDURE:- 1.enter an integer value n, set oddsum and evensum to 0 2. repeat step 3 and 4 until all the n different values are read 3.read an integer find modulo 2 of that if result is 0 add this to previous evensum 4.if result of modulo is 1 add the integer to previous oddsum 5.print the oddsum and evensum CODE:- #include
void main()
{
int n,x,i=1;sume=0,sumo=0;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter %d different values”,n);
while(i<=n) { scanf(“%d”,&x); if(x%2= =0) sume=sume+x; else sumo=sumo+x; i++ } printf(“Even sum =%d \n Odd sum = %d”, sume,sumo); } Input:- enter value for n 4 enter 4 different values 10 11 12 13 Output:- Even sum =22 Odd sum =24 PROBLEM 18:- Reversing a number. PROCEDURE:- 1.enter integer value n 2.repeat step 3 to 5 until n is greater than 0 3. find the modulo 10 for n and store result in r 4.print r 5. find new value of n by dividing it by 10 CODE:- #include
void main()
{
int n,r;
printf(“enter value for n”);
scanf(“%d”,&n);
while(n>0)
{
r=n%10;
printf(“%d”,r);
n=n/10;
}
}


Input:- enter value for n 124
Output:- 421

PROBLEM 19:- Sum of digits of a number down to a single digit.

PROCEDURE:-

1.enter integer value n
2.repeat step 3 to 8 until n is greater than 9
3.set sum to 0
4.repeat step 5 to 7 until n is greater than
5. find the modulo 10 for n and store result in r
6.add r to previous sum
7. find new value of n by dividing it by 10
8 set n to sum
9. print sum



CODE:-

#include
void main()
{
int n,m,sum,r;
printf(“enter value for n”);
scanf(“%d”,&n);
m=n;
while(n>9)
{
sum=0;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
n=sum;
}
printf(“sum of %d down to single digit is %d”,m,sum);
}


Input:- enter value for n 137
Output:- sum of 137 down to single digit is 2

PROBLEM 20. Number system conversion.(Decimal to Binary)

PROCEDURE:-

1.enter integer value n set sum to 0 and f to 1
2.repeat step 3 to 6 until n is greater than 0
3. find the modulo 2 for n and store result in r
4.set sum to sum+r*f
5.set f to f*10
6. find new value of n by dividing it by 2
7. print sum

CODE:-

#include
void main()
{
long int n,m,s=0,r,f=1;
printf(“enter number in decimal system \n”);
scanf(“%ld”,&n);
m=n;
While (n>0)
{
r=n%2;
s=s+r*f;
f=f*10;
n=n/2;
}
printf(“the binary value for the decimal number %ld is %ld”,m,s);
}


Input:- enter number in decimal system 7
Output:- the binary value for the decimal number 7 is 111











PROBLEM 21:- Find factorial of a number.



PROCEDURE:-


1.input an integer value n
2.set fact to 1 and i to 1
3.repeat step 4,5 until i less than or equal to n
4.set fact to fact*i
5.increment i by 1
6.print fact


CODE:-


#include
void main()
{
long int n, fact=1;
int i;
printf(“enter value for n”);
scanf(“%ld”,&n);
for(i=1;i<=n;i++) fact=fact *i ; printf(“ factorial for the number %ld is %ld”,n,fact); } Input:- enter value for n 5 Output:- factorial for the number 5 is 120 PROBLEM 22:- Find G.C.D.(Greatest Common Divisor) of two numbers. PROCEDURE:- 1.input two interger values a and b 2.if a is greater than b 3.set n=b 4.else set n=a 5.repeat steps 6 and 9 until n is greater than 0 6 find a modulo n and b modulo n, if modulo value is 0 7. print gcd value as n 8.stop 9else decrement n by 1 CODE:- #include
void main()
{
int a,b,n;
printf(“enter two numbers for a,b to find GCD”);
scanf(“%d %d “,&a,&b);
if(a>b)
n=b;
else
n=a;
do
{
if(a%n = = 0 && b%n = = 0)
{
printf(“ GCD for %d and %d is %d”,a,b,n);
break;
}
else
n=n-1;
}while(n>0);
}


Input:- enter two numbers for a ,b to find GCD 16 24
Output:- GCD for 16 and 24 is 8








PROBLEM :-23 Checking whether a number is Prime.


PROCEDURE:-

1.input an interger value n
2.start finding modulo value for n with i=2 to n-1 value incrementing i by 1 each time
3.if at any point remainder becomes 0
4.print that number is not prime
5.else print that the number is prime


CODE:-

#include
void main()
{
int n , i,flag =0;
printf(“enter a number “);
scanf(“%d”,&n);
for(i=2;i
void main()
{
int i,flag,n ;
printf(“primes below 100 are\n”);
for(n=1;n<=100;n++) { flag=0; for(i=2;i
void main()
{
int n ,i,s=0
printf(“enter a number “);
scanf(“%d”,&n);
for(i=1;i
void main()
{
int n ,i,s;
printf(“the perfect numbers below 100 are \n”);
for(n=1;n<=100;n++) { s=0; for(i=1;i
void main()
{
int n, a=0,b=1 ,c,flag=0;
printf(“enter a number”);
scanf(“%d”,&n);
do
{
c=a+b;
if(n= = c)
{
flag=1;
break;
}
a=b;
b=c;
}while(c<=n); if(flag = = 1) printf(“%d is Fibonacci number”,n); else printf(“%d is not Fibonacci number”,n); } Input:-enter a number 8 Output:-8 is Fibonacci number PROBLEM 28:- Generate Fibonacci series below 100( 0 ,1, 1, 2 ,3 ,5,8………..) PROCEDURE:- 1.set i to 3, set a=0,b=1, 2. repeat steps 3 to 5 until i is less than or equal 100 incrementing i by 1 each time 3. compute c=a+b 4.print value of c 5. set a=b,b=c CODE:- #include
void main()
{
int i=3, a=0,b=1 ,c;
printf(“Fibonacci series below 100 are :\n”);
printf(“%3d %3d”,a,b);
while(i<=100) { c=a+b; printf(“%3d”,c); a=b; b=c; i++; } } Output:- Fibonacci series below 100 are 0 1 1 2 3 5 8 13……………… PROBLEM 29:- Check whether a given number is Fibonacci prime or not. PROCEDURE:- 1.input an integer n 2. first check whether it is prime or not 3. if it prime check whether it is in the Fibonacci series 4.the above check can be made by generating series and comparing each number with n 5.if n is found in the series then n is Fibonacci prime 6.else it is not Fibonacci prime CODE:- #include
void main(){
int n,i=2,k=0,a,b,i,m;
printf(“enter the number”);
scanf(“%d”,&n);
while(i<=n/2) { if(n%i = = 0) { k=1; break; } i++; } if(k!=1) { a=0; b=1; i=1; m=n; while(i<=m) { c=a+b; if(m= =c) { printf(“ %d is Fibonacci prime”,n); break; } a=b; b=c; i++; } if(m!=c) printf(“%d Not Fibonacci prime”,n); } else printf(“not prime “); } Input:-enter the number 13 Output:- 13 is Fibonacci prime PROBLEM 30:- Generate twin primes below 100.( (3,5), (5,7), ……………). PROCEDURE:- 1.set n to 2 2.check the first prime 3.check the next prime by incrementing the n value 4. check the diff. of second prime to first prime (they are twin prime if the difference is 2) 6.repeat the process in steps 2 to 4 so as to generate all twin primes below 100 CODE:- #include
void main( )
{
/* n value is 2 because we are checking 1-100 twin primes, 1 is not prime,
b can have any value initially,here it is 2, later on b value will be a value of a*/

int i,b=2,n=2,a,ctr;
while(n<100) { ctr=0; i=2; while(i
void main()
{
int i;
float sum=0;
for(i=1;i<=25;i++) sum=sum+(1.0/(i * i)); printf(“ sum of the series is = %f”,sum); }| Output:- sum of the series is =1.605724 PROBLEM 32:-. Evaluate the cos(x) series(x-x3/3!+x5/5!-……..) PROCEDURE:- 1. input values for degrees x and the number of terms to be added n 2. convert the given degrees to radians 3. substitute in the series to find the sum 4. print the sum CODE:- #include
void main()
{
int i=1,n;
float sum,t,x,y;
printf(“enter the number of terms and degrees”)
scanf(“%d %f”,&n,&y);
x=y*3.142/180;
sum=x;
t=x;
while(i
void main()
{
int i=1,n,y;
float sum,t,x;
printf(“enter the number of terms and degrees”)
scanf(“%d %d”,&n,&y);
x=y*3.142/180;
sum=1;
t=1;
while(i
void main()
{
int n,r,s=0,m;
printf(“enter value for n”);
scanf(“%d”,&n);
m=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s= =m)
printf(“ %d is a palindrome”,m);
else
printf(“%d is not a palindrome”,m);
}
Input:- enter value for n 121

Output:- 121 is a palindrome



Generate the following Pyramids .


35) 1
2 2
3 3 3
4 4 4 4
………….
PROCEDURE:-
1.enter the number of rows n
2.set i to 1
3.repeat steps 4 to 8 until i<=n 4.set j to 1 5.repeat steps 6 and 7 until j<=i 6.print the value of j 7.increment j by 1 8. go to new line ,increment i by 1 CODE:- # include
void main()
{

int i,j,n;
printf(“enter value for n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) { for(j=1;j<=i; j++) { printf(“ %3d”,j); } printf(“\n”); } } Input:-enter value for n 3 Output:-1 2 2 3 3 3 36) 1 2 2 3 3 3 4 4 4 4 ……………………. PROCEDURE:- 1.enter the number of rows n 2.set i to 1 3.repeat steps 4 to 8 until i<=n 4.set j to 1, print (n-i) spaces 5.repeat steps 6 and 7 until j<=i 6.print the value of j 7.increment j by 1 8. go to new line, increment i by 1 CODE:- # include
void main()
{

int i,j,n,k;
printf(“enter value for n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) { for(k=1;k<=30-i; k++) printf(“ “); for(j=1;j<=i; j++) { printf(“ %3d”,j); } printf(“\n”); } } Input:-enter value for n 3 Output:- 1 2 2 3 3 3 37) 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 PROCEDURE:- 1.enter the number of rows n 2.print required spaces so as to get output in pyramid form 3.set i to 1 4.repeat steps 5 to 11 until i is less than or equal to n 5.set j to i 6.repeat step 7 until j is less than or equal to 2*i-1 7.print value of j, increment j by 1 8. set l to 2*i-2 9.repeat step 10 until l is greater than or equal to i 10. print value of l, decrement l by 1 11. goto new line , increment i by 1 CODE:- #include
void main()
{
int i,j,n=4,k;
for(i=1;i<=n;i++) { for(k=1;k<=30-i; k++) printf(“ “); for(j=i;j<=2*i-1; j++) printf(“ %3d”,j); for(l=2*i-2; l>=i; l--)
printf(“%3d”,l);
printf(“\n”);
}
}

Output:- 1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4




PROBLEM 38:- 1
2 3
4 5 6
7 8 9 10
……………..
PROCEDURE:-
1.enter the number of rows r
2.find the last no. to printed n=2+(r-1)
3.set i to 1,j=1,c=0
4.repeat steps 5 to 10 until j is less than or equal to n
5.set c to c+i
6.repeat step 7 to 8 until j is less than or equal to c and j less than or equal n
7.if i is less than or equal to n, then print value of j
8. increment j by 1
9.go to new line , set j to c+i
10 .increment i by 1

CODE:-

#include
void main()
{
int r,c,j,n,i;
printf(“enter the no. of rows”);
scanf(“%d”,&r);
printf(“the required output is :\n”);
n=((2+(r-1));c=0;i=1;j=1;
while(j<=n) { c=c+i; while(j<=c && j<=n) { if(i<=n) { printf(“%4d”,j); } j++; } printf(“\n”); j=c+i; i++; }} Input:- enter the no. of rows 4 Output:- 1 2 3 4 5 6 7 8 9 10 …………….. PROBLEM 39:- 1 0 1 1 0 1 0 1 0 1 PROCEDURE:- 1.enter no. of rows n, set k to 0 and l to 1 2. repeat steps 3 to 9 until i is less than or equal to n incrementing i by 1 each time 3. if modulo 2 of i is 0 then do step 4 and 5 4. set j to 1 and repeat step 5 until j is less than or equal to i/2 incrementing j by 1 each time 5.print k and l 6.else set m to 1 repeat step 7 ,8 until m is less than or equal i incrementing m by 1 7. if m modulo 2 is not equal to 0 print value of l 8. else print value of k 9.go to new line CODE:- #include
void main()
{
int i,j,k=0,l=1,n,m;
printf(“enter n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) { if(i%2 = =0) { for(j=1;j<=i/2;j++) printf(“%d %d”,k,l); } else { for(m=1;m<=i;m++) { if(m%2!=0) printf(“%d”,l); else printf(“%d”,k); } } printf(“\n”) }} Input:-enter n 4 Output:- 1 0 1 1 0 1 0 1 0 1 PROBLEM 40:- * * * * * * * * * * PROCEDURE:- 1.enter the number of rows n 2.set i to 1 3.repeat steps 4 to 8 until i<=n 4.set j to 1, print (n-i) spaces 5.repeat steps 6 and 7 until j<=i 6.print the value of ‘*’ 7.increment j by 1 8. go to new line, increment i by 1 CODE:- # include
void main()
{

int i,j,k;
for(i=1;i<=4;i++) { for(k=1;k<=30-i; k++) printf(“ “); for(j=1;j<=i; j++) printf(“*”); printf(“\n”); } } Output:- * * * * * * * * * * PROGRAMS USING ARRAYS. PROBLEM 41:- Sum & Average of a given n numbers. PROCEDURE:- 1.read the no. of values to be entered n 2.read the n different elements while reading add it sum 3.find the average and print sum and average CODE:- #include
void main()
{
int a[20],i,n,sum=0;
float avg;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter the %d element”,n);
for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); sum=sum+a[i]; } avg=sum/(float)n; printf(“ Sum = %d \n average= %f”,sum, avg); } Input:-enter value for n 4 Enter the 4 elements 10 20 30 40 Output:-sum=100 average=25.00 PROBLEM 42: Sort array of n numbers in ascending & descending order. PROCEDURE:- 1.read the no. of values to be entered n 2.read the n different elements 3.repeat steps 4 to 5 for i=1 ,2,….,n-1 4.repeat step 5 for j=1,2,….,n-i 5.if A[j]>A[j+1] swap both the elements
6.print the n elements
CODE:-
#include
void main()
{
int a[20],i,n,j,t;
float avg;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter the %d element”,n);
for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } for(i=1;ia[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
print(“the ascending order is :\n”);
for(i=1;i<=n;i++) printf(“%3d”,a[i]); print(“the descending order is :\n”); for(i=n;i>=1;i--)
printf(“%3d”,a[i]);
}
Input:-enter the no. of elements:4
Enter the 4 different numbers
2 8 4 1
Output:-the ascending order is :- 1 2 4 8
The descending order is : 8 4 2 1


PROBLEM 43:- Find the Maximum and Minimum in an array of n numbers.
PROCEDURE:-
1.read the no. of values to be entered n
2.read the n different elements
3.set max =a[1]
4.repeat step 5 for i=2 ,3,…..n
5.if a[i]>max, then set max=a[i]
6.print max
7.set min =a[1]
8.repeat step 9 for i=2 ,3,…..n
5.if a[i]
void main()
{
int a[20],i,n,max,min;
float avg;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter the %d element”,n);
for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } max=a[1]; for(i=2;i<=n;i++) if(a[i]>max)
max=a[i];
min=a[1];
for(i=2;i<=n;i++) if(a[i]0
3.increment k by 1, find n modulo 10 and assign to j
4.if k modulo 2 is 0 se=se+j
5.else so=so+j
6.new n is n/10
7.print even digit sum and odd digit sum

CODE:-

#include
void main()
{
int n, se=0,so=0,k=0,j;
printf(“enter a number”);
scanf(“%d”,&n);
while(n>0)
{
k++;
j=n%10;
if(k%2= =0)
se=se+j;
else
so=so+j;
n=n/10;
}
printf(“sum of digits in even places=%d”,se);
printf(“sum of digits in odd places=%d”,so);
}

Input:-enter a number 1234
Output:-sum of digits in even places=6
Sum of digits in odd places=4




PROBLEM 45: Sum of two matrices
PROCEDURE:-
1.enter value for m n
2.enter values for matrix A of m by n order
3.enter value for m n
4.enter values for matrix B of m by n order
5.add A and B matrix and store in C matrix
6.print C matrix

CODE:-
#include
void main()
{
int i,j, a[10][10],b[10][10],c[10][10],m.n;
printf(“enter value for m,n”);
scanf(“%d %d”,&m,&n);
printf(“enter the first matrix\n”);
for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(“%d”,&a[i][j]); printf(“enter the second matrix\n”); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(“%d”,&b[i][j]); for(i=1;i<=m;i++) for(j=1;j<=n;j++) c[i][j]=a[i][j]+b[i][j]; printf(“the resultant sum of two matrices is :\n”); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) printf(“%3d”,c[i][j]); printf(“\n”); } } Input:-enter value for m n 2 2 Enter the first matrix : 1 1 2 2 enter value for m n 2 2 enter the second matrix: 2 2 3 3 Output:-the resultant sum of two matrices: 3 3 5 5 PROBLEM 46:- Difference of two Matrices. PROCEDURE:- 1.enter value for m n 2.enter values for matrix A of m by n order 3.enter value for m n 4.enter values for matrix B of m by n order 5.Subtract A and B matrix and store in C matrix 6.print C matrix CODE:- #include
void main()
{
int i,j, a[10][10],b[10][10],c[10][10],m.n;
printf(“enter value for m,n”);
scanf(“%d %d”,&m,&n);
printf(“enter the first matrix\n”);
for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(“%d”,&a[i][j]); printf(“enter the second matrix\n”); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(“%d”,&b[i][j]); for(i=1;i<=m;i++) for(j=1;j<=n;j++) c[i][j]=a[i][j]-b[i][j]; printf(“the resultant difference of two matrices is :\n”); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) printf(“%3d”,c[i][j]); printf(“\n”); } } Input:-enter value for m n 2 2 Enter the first matrix : 3 3 3 3 enter value for m n 2 2 enter the second matrix: 2 2 2 2 Output:-the resultant difference of two matrices: 1 1 1 1 PROBLEM 47:-Multiplication of two Matrices. PROCEDURE:- 1.enter value for m n 2.enter values for matrix A of m by n order 3.enter value for p q 4.enter values for matrix B of m by n order 5.if n is equal to p then 5.find product A and B matrix and store in C matrix 6.print C matrix 7.if n is not equal to p matrix multiplication not possible CODE:- #include
void main()
{
int i,j, a[10][10],b[10][10],c[10][10],m.n,p,q,k;
printf(“enter value for m,n”); scanf(“%d %d”,&m,&n,&p,&q);
printf(“enter the first matrixof order % by %d\n”,m,n);
for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(“%d”,&a[i][j]); printf(“enter the second matrix of order %d by %d\n”,p,q); for(i=1;i<=p;i++) for(j=1;j<=q;j++) scanf(“%d”,&b[i][j]); if(n= =p) { for(i=1;i<=m;i++) for(j=1;j<=q;j++) { c[i][j]=0; for(k=1;k<=p;k++) c[i][j]=c[i][j] +a[i][k]*b[k][j]; } printf(“the resultant product of two matrices is :\n”); for(i=1;i<=m;i++) { for(j=1;j<=q;j++) printf(“%3d”,c[i][j]); printf(“\n”); } } else printf(“matrix multiplication not possible”); } Input:- enter value for m n 2 2 Enter the first matrix : 3 3 3 3 enter value for m n 2 2 enter the second matrix: 2 2 2 2 Output:- the resultant difference of two matrices: 12 12 12 12 PROBLEM 48:- Write a program to test whether a given Matrix is an upper triangular Matrix or not. An upper triangular Matrix is one in all which all these elements below its principal diagonal are 0. PROCEDURE:- 1.enter value for m n 2.enter values for matrix A of m by n order 3.if all the elements below the principal diagonal are 0’s then 4 print “Upper triangular matrix”. 5.else print “not an upper triangular matrix”. CODE:- #include
void main()
{
int a[10][10],m,n,i,j,t;
printf(“Enter the no.of rows & columns of the matrix:”);
scanf(“%d %d”, &m,&n);
if(m!=n)
printf(“Upper triangular rows must be equal to columns”);
else
{
printf(“Enter values\n”);
for(i=0;ij) {
if(a[i][j]!=0)
t=1;
}
else if(a[i][j]==0)
t=1;
if(t==1)
printf(“Not upper Triangular Matrix”);
else
printf(“Upper Triangular”);
}
Input:- enter value for m n 2 2 Enter the first matrix : 3 3
0 3

Output:-upper triangular

PROBLEM 49¬:- Write a program to test whether a given Matrix is an unit Matrix or not.
PROCEDURE:-
1.enter value for m n
2.enter values for matrix A of m by n order
3.if all the elements below the principal diagonal are 1’s and the remaining all elements must be 0’s then
4 print “unit matrix”.
5.else print “not a unit matrix”.
CODE:-
#include
void main()
{
int a[10][10],m,n,i,j,t=0;
printf(“Enter the no.of rows & columns of the matrix:”);
scanf(“%d %d”, &m,&n);
if(m!=n)
printf(“m, n must be equal”);
else
{
printf(“Enter values\n”);
for(i=0;i
void main()
{
int a[10][10],m,n,i,j,t=0;
printf(“Enter the no.of rows & columns of the matrix:”);
scanf(“%d %d”, &m,&n);
if(m!=n)
printf(“m,n must be equal );
else
{
printf(“Enter values\n”);
for(i=0;i
#include
void main()
{
char str[100];
int i=0,word=0,chr=0;
clrscr();
printf(“Enter any String : “);
gets(str);
while(str[i]!=’\0’)
{
if(str[i]= =” “)
word++;
else
chr++;
i++;
}
printf(“The Total No.of Words : %d \nThe Total No.of Characters %d”,word+1,chr);
}

Input:- Enter any String : I am fine
Output:- The Total No.of Words : 3
The Total No.of Characters 7
PROBLEM 52:- Compare two strings.

PROCEDURE:-
1.read two string
2.find the lengths of each string if they are equal then only proceed for comparision
3.start extracting character by character from the both the strings till the end of strings
4.compare the characters extracted
5.if at any point they are not equal stop further comparision and print that strings unequal
6.else print that strings are equal


CODE:-

#include
#include
void main()
{
char str[100],str1[100];
int k,n,m;
printf(“Enter First String : “); gets(str);
printf(“Enter Second String : “); gets(str1);
n=strlen(str); m=strlen(str1);
if(n= =m)
{
k=0; i=0;
while(str[i]!=’\0’)
{
if(str[i]!=str1[i])
{
k=1; break;
}
i++;
}
if(k= =0)
printf(“The strings are Equal”);
else
printf(“The strings are not Equal”);
}
else
printf(“strings are uncomparable”);
}

Input:- Enter first string : Hello
Enter second string: Hello
Output:-the strings are equal
PROBLEM 53:- Copy a String into another.

PROCEDURE:-

1.read a string
2.start extracting character by character till the end of string
3.store each character into second string
4.terminate the second string with null character
5.print the second string


CODE:-

#include
#include
void main()
{
char str[100],str1[100];
clrscr();
printf(“Enter First String : “);
gets(str);
i=0;
while(str[i]!=’\0’)
{
str1[i]=str[i];
i++;
}
str1[i]=’\0’;
printf(“The Original string : %s”,str);
printf(“The Copied string : %s”,str1);
}

Input:- Enter first string : Hello

Output:- The original string: Hello
The copied string : Hello



PROBLEM 54:- Concatenate Two Strings.

PROCEDURE:-

1.read two strings
2.traverse till the end of string
3.add second string at the end of first string
4.terminate the string with null character
5.print the first string


CODE:-

#include
void main()
{
char str[100],str1[25];
int i,j,k;
clrscr();
printf(“Enter First String : “);
gets(str);
printf(“Enter Second String : “);
gets(str1);
for(j=0;str[j]!=’\0’;j++);
for(i=0;str1[i]!=’\0’;i++)
str[i+j+1]=str1[i];
str[i+j+1]=’\0’
printf(“After ConcatenatingTwo strings :”);
puts(str);
}

Input:- Enter first string : Hello
Enter second string: hai

Output:- After concatenating two strings : Hellohai

PROBLEM 55. Insert a substring into a String from a particular position.

PROCEDURE:-

1.read a string
2.traverse till the position for inserting the substring
3.move the remaining characters of the original string ahead by the no. of characters in substring
4.insert the substring
5.print the original string


CODE:-

#include
#include
void main()
{
char s[100],s1[50];
int i,j,k,pos,m;
printf("enter Two Strings : ");
gets(s); gets(s1);
k=strlen(s); j=strlen(s1);
printf("enter Position to Insert string : ");
scanf("%d",&pos);
m=j; i=k+j; s[i+1]='\0';
while(m>=0)
{
s[i]=s[k-1];
i--; k--; m--;
}
i=0;
while(s1[i]!='\0')
{
s[pos]=s1[i];
pos++; i++;
}
printf("The result string is : %s",s);
}

Input:- Enter first string : Hello
Enter second string: hai
Enter position to insert string : 2

Output:- The result string is : Hhaiello
PROBLEM 56:- Delete n characters in the string from a particular position.
PROCEDURE:-

1.read a string st1
2.read the no. of characters to be deleted n and form which position m
3. set j to n+m so as to move characters positioned at j a head till null character is encountered

CODE:-

#include
void main()
{
char str[20];
int i,j,m,n;
printf(“enter the string”);
gets(str);
printf(“enter the position and the number of characters to be deleted”);
scanf(“%d %d”, &n,&m);
j=n+m;
i=n;
while(str[j]!=’\0’)
{
str[i]=str[j];
i++;
j++;
}
str[i]=’\0’;
printf(“the new string is “);
puts(str);
}

Input:-enter a string : HELLO
Enter the position and the number of characters to be deleted 1 3

Output:-the new string is :- HO

PROBLEM 57:- Reverse a given String.


PROCEDURE:-

1.read a string S
2. move till the end of string
3.start printing character by character from end till the beginning

CODE:-

#include
main()
{
int i,j;
char s[100];
printf(“Enter the string”);
scanf(“%s”,s);
for(i=0;s[i]!=’\0’;i++);
printf(“The reversed string is:”);
for(j=i-1;j>=0;j--)
putchar(s[j]);
}

Input:-enter the string: HELLO

Output:-the reversed string is: OLLEH


PROBLEM 58:- Check whether a given string is palindrome or not.
PROCEDURE:-

1.read a string S
2.move till the end of S
3.start copying from the last character till the first character into another string S1
4.if S and S1 are the same then print “palindrome” else print “not palindrome”

CODE:-

#include
main()
{
int i,j,k;
char s[100],s1[100];
printf(“Enter the string”);
gets(s);
for(i=0;s[i]!=’\0’;i++);
printf(“The reversed string is:”);
k=0;
for(j=i-1;j>=0;j--)
{
s1[k]=s[j];
k++;
}
s1[k]=’\0’;
if(strcmp(s,s1)= =0)
printf(“the string is palindrome”);
else
printf(“string is not palindrome);
}

Input:-enter the string: MADAM

Output:-the string is palindrome

PROBLEM 59:- Write a program to compare two given strings .the function should return 1 if the strings are equal and 0 otherwise.
PROCEDURE:-
1. read two strings S1,S2
2. find the lengths of both the strings
3. if they are equal the proceed with comparision else print “uncomparable”
4. start comparing from first characters of either of the string till the end of string
5. if at any position a mismatch occurs print”unequal “ return
6. else print “equal”
CODE:-
#include
void main()
{
char str[20],str1[20];
int flag=0,i=0;
int strcp(char [ ], char [ ]), x;
printf(“enter the first string”);
gets(str);
printf(“enter the second string”);
gets(str1);
n=strlen(str); m=strlen(str1);
if(m= =n)
{x=strcp(str,str1);
if(x= =0)
printf(“strings are equal”);
else
printf(“strings are unequal”);
}
else printf(“uncomparable”);
}
int strcp(char *s1, char *s2)
{ int i=0,flag=0;
while(*s1!=’\0’)
{
if(*s1!=*s2)
{
flag=1; break;
}
s1++; s2++;
}
if(flag= =0) return(0);else return(1);
}
Input:- enter first string: HELLO
enter second string:HELLO
Output:-strings are equal


PROBLEM 60:- Program to find the number of occurences of each alphabet in a given string .Assume that the string contain only alphabets.
PROCEDURE:-
1.read a string S
2.repeat steps 3 to 7till the end of the string S, set i to 0 and character count by 0
3. strart scanning the the character i th character
4.repeat step 5 till the end of the string S
5. if the i th character matches with any other characters scanned increase character count by 1
6. print the character count
7.increase i by 1

CODE:-

#include
void main()
{
char str[20];
printf(“enter the string”);
gets(str);
i=0;
while(str[i]!=’\0’)
{
c=0;
ch=str[i];
j=i;
while(str[j]!=’\0’)
{
if(ch= =str[j])
c++;
j++;
}
printf(“%c ‘s no. of occurrences is %d”,ch,c);
i++;
}
}
Input:-enter the string: I am fine
Output:- i ‘s no. of occurrences is 2
a’s no. of occurrences is 1
m’ss no. of occurrences is 1
f’s no. of occurrences is 1
n’s no. of occurrences is 1
e’s no. of occurrences is 1

PROBLEM 61:- Maximum of the given numbers in an array.

PROCEDURE:-

1. Read the numbers in to the array.
2. Imagine that the first no.in the array is the maximum no.
3. compare other no with max.
4. if no. is greater than max interchange the values.
5. print the max value.

CODE:-
#include
void main()
{
void maxi(int [],int);
int a[20],i,n,max;
float avg;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter the %d element”,n);
for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } maxi(a,n); } void maxi(int a[],int n) { int i,max; max=a[1]; for(i=2;i<=n;i++) if(a[i]>max)
max=a[i];
printf(“maximum value is %d”,max);
}
Input:- enter value for n 4
Enter 4 elements 10 80 9 4
Outout:-maximum value is 80

PROBLEM 62:- Factorial of given number.

PROCEDURE:-

1. Read the number n., set f=1
2. starting i=1,2….n repeat step 3
3. set f=f*i
4. print f

CODE:-

#include
void main()
{
long int fact(long int);
long int n,f;
printf(“enter value for n”);
scanf(“%ld”,&n);
f=fact(n);
printf(“factorial of %ld is %ld”,n,f);
}
long int fact(long int n)
{
long int i,f=1;
for(i=1;i<=n;i++) f=f*i; return(f); } Input:-enter value for n 3 Output:-factorial of 3 is 6 PROBLEM 63:- Reverse a string. PROCEDURE:- 1.read a string S 2. move till the end of string 3.start printing character by character from end till the beginning CODE:- #include
void main()
{
char str[20];
void rev(char [ ]);
printf(“enter the string”);
gets(str);
rev(str);
}

void rev(char *s)
{
char *t;
t=s;
while(*s!=’\0’)
s++;
printf(“the reversed string is \n”);
while((s-t)>=0)
{
putchar(*s) ;
s--;
}
}

Input:-enter the string : HELLO
Output:-the reversed string is :OLLEH

PROBLEM 64:- Swap two numbers (Call by value).

PROCEDURE:-

1. Read two numbers a & b.
2. using a temporary variable interchange the values of a & b.
3. print the values of a & b.

CODE:-

#include
void main()
{
void swap(int,int);
int a,b;
printf(“enter values for a,b”);
scanf(“%d %d”,&a,&b);
printf(“before swapping a=%d b=%d”,a,b);
swap(a,b);
printf(“after swapping a=%d b=%d”,a,b);
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf(“in function after swapping a=%d b=%d”,a,b);
}

Input:-enter values for a and b 5 4

Output:-before swapping a=5 b=4
After swapping a=5 b=4

PROBLEM 65:- Swap two numbers (Call by reference).

PROCEDURE:-

1 Read two numbers a & b.
2 using a temporary variable interchange the address locations of a & b.
3 print the values of a & b.

CODE:-

#include
void main()
{
void swap(int *, int *);
int a,b;
printf(“enter values for a,b”);
scanf(“%d %d”,&a,&b);
printf(“before swapping a=%d b=%d”,a,b);
swap(&a,&b);
printf(“after swapping a=%d b=%d”,a,b);
}
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}

Input:-enter values for a and b 5 4

Output:-before swapping a=5 b=4
After swapping a=4 b=5

PROBLEM 66:- A recursive function in C for finding the factorial of a given number n.

PROCEDURE:-

1 Read the number n.
2 recursive call of n*fact(n-1) until n=0
3 print value

CODE:-

#include
void main()
{
long int n,f;
long int fact(long int);
printf(“enter value for n”);
scanf(“%ld”,&n);
f=fact(n);
printf(“factorial of %ld is %ld “,n,f);
}
long int fact(long int n)
{
if(n= =0)
return(1);
else
return(n*fact(n-1));
}

Input:-enter value for n 3
Output:-factorial of 3 is 6

PROBLEM 67:- Compare two strings.
PROCEDURE:-
1.read two string
2.find the lengths of each string if they are equal then only proceed for comparision
3.start extracting character by character from the both the strings till the end of strings
4.compare the characters extracted
5.if at any point they are not equal stop further comparision and print that strings unequal
6.else print that strings are equal
CODE:-
#include
void main()
{
char str[20],str1[20];
int flag=0,i=0;
void strcp(char [ ], char [ ]);
int x;
printf(“enter the first string”); gets(str);
printf(“enter the second string”); gets(str1);
n=strlen(str); m=strlen(str1);
if(m= =n)
{
strcp(str,str1);
}
else
printf(“uncomparable”);
}
void strcp(char *s1, char *s2)
{
int i=0,flag=0;
while(*s1!=’\0’)
{
if(*s1!=*s2)
{
flag=1; break;
}
s1++; s2++;
}
if(flag= =0)
printf(“strings are equal”);
else
printf(“strings are unequal”);
}
Input:-enter first string: HELLO
Enter second string: HELLO
Output:-strings are equal

PROBLEM 68:- Sort table of strings .

PROCEDURE:-

1. Enter n strings
2. compare string with its successor string using strcmp() function
3. if its value is greater than 0 then swap both the strings
4. repeat the process until all the strings are sorted
5. print the sorted strings

CODE:-

#include
#include
void main()
{
int n,i,j;
char s[100][100],t[100];
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter %d strings “,n);
for(i=0;i0)
{
strcpy(t,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],t);
}
printf(“the sorted strings are\n”);
for(i=0;i
void main()
{
int n,x,sum(int);
printf(“enter value for n”);
scanf(“%d”,&n);
x=sum(n);
printf(“sum upto %d numbers is %d”,n,x);
}
int sum(int n)
{
if (n= = 0)
return(0);
else
return(n+sum(n-1));
}

Input:-enter value for n 4
Output:-sum upto 4 numbers is 10

PROBLEM 70:- Find Standard Deviation for a set of values .

PROCEDURE:-

1. Read value for n
2. read n different values find the total sum of them
3. find average of n nos.
4. for the n values find sd

CODE:-

#include
void main()
{
int n ,x[100],i;
float avg,sum=0,sn,sd;
printf(“enter value for n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) { scanf(“%d”,&x[i]); sum=sum+x[i]; } avg=sum/n; sn=0; for(i=1;i<=n;i++) sn = sn + (x[i]-avg)*(x[i]-avg); sd= (1.0/n)*sqrt(sn); printf(“the standard deviation is %f “, sd); } Input:-enter value for n 4 Enter 4 values 4 5 6 7 Output:-sd=0.25 PROGRAMS ON STRUCTURES: PROBLEM 71: Program to calculate the subject-wise and student-wise totals and store them as a part of the structure and arrays within a structure. PROCEDURE:- 1.read the number of students and no. of subjects n m 2. read the m marks obtained by each of n students 3. put the data in tabular form find total sum of marks obtained by each student S 4. find marks total obtained in each subject by all the students T 5. find the grand total (S+T) 6.print all the details in tabular form CODE:- #include
#define max 10
struct student
{
int s[max];
int ts[max];
int tst[max];
}st[max];
int i,j,s,gt,m,n;
void main()
{
printf(“enter no. of students”);
scanf(“%d”,&n);
printf(“enter no. of subjects”);
scanf(“%d”,&m);
printf(“enter %d students %d subjects marks\n”,n,m);
for(i=0;i
struct abc
{
int a;
float b;
};
void main()
{
struct abc s1={10,10.5};
struct abc s2;
struct abc modify(struct abc );
printf(“ the structure members before modification are :”);
printf(“%5d %8.3f”,s1.a,s1.b);
s2=modify(s1);
printf(“ the structure members after modification are :”);
printf(“%5d %8.3f”,s2.a,s2.b);
}

struct abc modify(struct abc s1)
{
s1.a=s1.a+10;
s1.b=s1.b+10;
return(s1);
}
Input:- the structure members before modification are : 10 10.5
Output:- the structure members after modification are : 20 20.5


PROBLEM 73:- Write a suitable program to explain the concept of UNIONS.

PROCEDURE:-

1.read values for a union members through union variable
2 assign values to each member and print it immediately.

CODE:-

#include
union abc
{
int a;
float b;
};
void main()
{
union abc s1;
printf(“ the union members values are:”);
scanf(“\n%d”,&s1.a);
printf(“%d”,s1.a);
scanf(“%f”,&s1.b);
printf(“\n%f”,s1.b);
}

Input:- the union members values are:
Output:-10
10.50000
PROGRAMS USING POINTERS
PROBLEM 74:- Perform all arithmetic operations using pointer variables.
PROCEDURE:-
1. read two values a and b store addresses of a and b in p and q
2. perform arithmetic manipulations using pointers p and q

CODE:-

#include
void main()
{
int a,b,ch;
int *p=&a,*q=&b;
printf(“enter value for a and b”);
scanf(“%d %d”,&a,&b);
printf(“enter ur choice 1:add 2:subtract 3: multiply 4: divide 5:exit”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“the sum = %d “,(*p+*q));
break;
case 2: printf(“the difference = %d “,(*p-*q));
break;
case 3: printf(“the product = %d “,(*p * (*q)));
break;
case 4: printf(“the quotient = %d “,(*p/(*q)));
break;
case 5: exit();
}
}
Input:-enter values for a and b 5 2
Output:-the sum= 7
The difference =3
The product = 10
The quotient =2

PROBLEM 75:- Write a program to compute the sum of all elements stored in an array.

PROCEDURE:-
1.read value for n
2.read n different values while reading add it to sum
3.print the sum
CODE:-

#include
void main()
{
int a[20],i,n,*x,sum=0;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter %d elements “,n);
for(i=1;i<=n;i++) scanf(“%d”,&a[i]); x=&a[0]; sum=0; for(i=1;i<=n;i++) { sum=sum+*x; x++; } printf(“the sum=%d”,sum); } Input:-enter value for n 3 Enter the 3 elements 10 20 30 Output:-the sum=60 PROBLEM 76:- Write a program to find the length of string. PROCEDURE:- 1. read string 2. while moving till the end of string increment a counter value C 3. print C value CODE:- #include
void main()
{
char str[20];
char s=&str[0];
int i=0;
printf(“enter a string”);
gets(str);
while(*s!=’\0’)
{
i++;
s++;
}
printf(“the length of the string is %d”,i);
}

Input:-enter string: HELLO
Output:-the length of the string is 5

PROBLEM 77:- Write a program to sort a given set of numbers.

PROCEDURE:-

1.read the no. of values to be entered n
2.read the n different elements
3.repeat steps 4 to 5 for i=1 ,2,….,n-1
4.repeat step 5 for j=1,2,….,n-i
5.if A[j]>A[j+1] swap both the elements
6.print the n elements


CODE:-

#include
void main()
{
int n, *a,i,j,t;
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter the array elements”);
for(i=1;i<=n;i++) scanf(“%d”,(a+i)); for(i=1;i*(a+j+1))
{
t=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=t;
}
}
printf(“the sorted data is”);
for(i=1;i<=n;i++) printf(“%4d”,*(a+i)); } Input:-enter the no. of elements:4 Enter the 4 different numbers 2 8 4 1 Output:-the ascending order is :- 1 2 4 8 The descending order is : 8 4 2 1 PROBLEM 78: Write a program to find matrix multiplication. PROCEDURE:- 1.enter value for m n 2.enter values for matrix A of m by n order 3.enter value for p q 4.enter values for matrix B of m by n order 5.if n is equal to p then 5.find product A and B matrix and store in C matrix 6.print C matrix 7.if n is not equal to p matrix multiplication not possible CODE:- #include
#include
void main()
{
int i,j,k,a[10][10],b[10][10],c[10][10],*p,*q,*r,l,m,n,o;
clrscr();
printf("Enter Dimentions for First matrix :");
scanf("%d%d",&l,&m);
printf("Enter Dimentions for Second matrix :");
scanf("%d%d",&n,&o);
if(m==n)
{
printf("\nEnter First Matrix : ");
for(i=0;i
void main()
{
char str[20],str1[20];
char *s1=&str[0];
char *s2=&str1[0];
int n,m,flag;
printf(“enter first string”);
gets(str);
printf(“enter second string”);
gets(str1);
n=strlen(str);
m=strlen(str1);
if(n= =m)
{
flag=0;
while(*s1!=’\0’)
{
if(*s1!=*s2)
{
flag=1;
break;
}
s1++;
s2++;
}
if(flag= =0)
printf(“strings are equal”);
else
printf(“strings are not equal”);
}
else
printf(“strings are uncomparable”);
}
Input:-enter first string: HELLO
Enter second string: HELLO
Output:-strings are equal
PROBLEM 80:- Copy contents of one string to another.

PROCEDURE:-

1.Read a string S
2.extract character by character from S and copy it into S1 till end of S
3.print S1

CODE:-

#include
void main()
{
char str[20],str1[20];
char *s1=&str[0];
char *s2=&str1[0];
int n,m,flag;
printf(“enter the string”);
gets(str);
while(*s1!=’\0’)
{
*s2=*s1;
s1++
s2++;
}
*s2=’\0’;
printf(“the copied string is “);
puts(str1);
}

Input:- enter the string : HAI
Output:-the copied string is : HAI

PROBLEM 81:- Sort table of string .

PROCEDURE:-

1. Enter n strings
2. compare string with its successor string using strcmp() function
3. if its value is greater than 0 then swap both the strings
4. repeat the process until all the strings are sorted
5. print the sorted strings

CODE:-

#include
#include
void main()
{
int n,i,j;
char s[100][100],t[100];
printf(“enter value for n”);
scanf(“%d”,&n);
printf(“enter %d strings “,n);
for(i=0;i0)
{
strcpy(t,*(s+i));
strcpy(*(s+i),*(s+j));
strcpy(*(s+j),t);
}
printf(“the sorted strings are\n”);
for(i=0;i
struct abc
{
int a;
float b;
};
void main()
{
struct abc s1={10,10.5};
void modify(struct abc *);
printf(“ the structure members before modification are :”);
printf(“%5d %8.3f”,s1.a,s1.b);
modify(&s1);
printf(“ the structure members after modification are :”);
printf(“%5d %8.3f”,s1.a,s2.b);
}

void modify(struct abc *s1)
{
s1->a=s1->a+10;
s1->b=s1->b+10;
}

Input:- the structure members before modification are : 10 10.5
Output:- the structure members after modification are : 20 20.5


PROBLEM 83:- Write an example program which uses pointer to a structure.

PROCEDURE:-

1.read values for structure members
2.store the address of structure variable in a structure pointer
3.now members are accessed by -> operator

CODE:-

#include
struct abc
{
int a;
float b;
};
void main()
{
struct abc s1={10,10.5};
struct abc *p=&s1;
printf(“the contents of the structure”);
printf(“%d”,p->a);
printf(“\n%f “,p->b);
}

Output:- the contents of the structure are 10 10.5

PROBLEM 84:- Write a program that uses a function returning a pointer.
PROCEDURE:-

1.this function returns the address
2.so values in function are available in main

CODE:-

#include
void main()
{
int *b;
int *func();
b=func();
printf(“value =%d”,*b);
}
int *func()
{
int j=50;
return(&j);
} Output:-value =50


PROGRAMS USING FILES:
PROBLEM 85:- Write a program with a file named DATA contains a series of integer numbers.Code a program to read these numbers and then write all odd numbers to a file called ODD and all even numbers to a file called EVEN.

PROCEDURE:-

1. open a DATA file to write integer data
2. open the same file for reading, and open ODD,EVEN files to write
3. read contents of DATA file and check whether it is even or odd number
4. if it is odd number write to ODD file else write to EVEN file
5. close all the files

CODE:-

#include
void main()
{
FILE *f1, *f2,*f3;
int i,n;
f1=fopen(“intfile”,”w”);
f2=fopen(“odd”,”w”);
f3=fopen(“even”,”w”);
printf(“enter 10 numbers into a file”);
for(i=1;i<=10;i++) { scanf(“%d”,&n); putw(n,f1); } fclose(f1); f1=fopen(“intfile”,”r”); while((n=getw(f1))!=EOF) if(n%2= =0) putw(n,f3); else putw(n,f2); } fclose(f2); fclose(f3); f2=fopen(“odd”,”r”); f3=fopen(“even”,”r”); printf(“the contents of odd file are\n”); while((n=getw(f2))!=EOF) printf(“%3d”,n); printf(“the contents of even file are\n”); while((n=getw(f3))!=EOF) printf(“%3d”,n); fclose(f1); fclose(f2); fclose(f3); } PROBLEM 86:- Write a program with a file named TEXT (with a paragraph in it).Code a program to read this paragraph and then write all vowels to a file called VOWEL and consonants to a file called CONSONANT. PROCEDURE:- 1. open a TEXT file to write character data 2. open the same file for reading, and open VOWEL,CONSONANT files to write 3. read contents of TEXT file and check whether it is vowel or not 4. if it is vowel character write to VOWEL file else write to CONSONANT file 5. close all the files CODE:- #include
void main()
{
FILE *f1, *f2,*f3;
Char c;
f1=fopen(“text”,”w”);
f2=fopen(“vowel”,”w”);
f3=fopen(“consonant”,”w”);
printf(“enter a paragraph”);
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“text”,”r”);
while((c=getc(f1))!=EOF)
{
if(c= =’a’ || c= =’e’ || c= =’i’ || c= =’o’ || c= =’u’)
putc(c,f2);
else
putc(c,f3);
}

fclose(f2);
fclose(f3);
f2=fopen(“vowel”,”r”);
f3=fopen(“consonant”,”r”);
printf(“the contents of VOWEL file are\n”);
while((c=getc(f2))!=EOF)
putchar(c);
printf(“the contents of CONSONANT file are\n”);
while((c=getc(f3))!=EOF)
putchar(c);
fclose(f1);
fclose(f2);
fclose(f3);
}


PROBLEM 87:- Write a program to store text into a file , modify the same file by replacing all the ‘a’s with ‘#’.

PROCEDURE:-

1.open a file to store text
2. open the same file for modification
3. repeat step 4 until EOF is encountered
4. extract character by character , if character is ‘a’ replace the same with ‘#’
5. close the file

CODE:-

#include
void main()
{
FILE *f1;
char c;
printf(“enter data to a file”);
f1=fopen(“data”,”w”);
While((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);
f1=fopen(“data”,”w+”);
while((c=getc(f1))!=EOF)
{
if(c= =’a’);
putc(‘#’,f1);
}
fclose(f1);
printf(“the modified data file is \n”);
f1=fopen(“data”,”r”);
while((c=getc(f1))!=EOF)
putchar(c );
fclose(f1);
}

PROBLEM 88:- Write a program to create two text files , append the second text file at the end of first file.

PROCEDURE:-

1.open a two files to store text F1,F2
2. open the F1 file for appending
3. repeat step 4 until EOF of F2 is encountered
4. extract character by character , and place it in F1
5. close the files

CODE:-

#include
void main()
{
FILE *f1,*f2;
char c;
printf(“enter data to a file”);
f1=fopen(“data”,”w”);
While((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);
printf(“enter data to a file”);
f2=fopen(“data1”,”w”);
While((c=getchar())!=EOF)
{
putc(c,f2);
}
fclose(f2);

f1=fopen(“data”,”a+”);
f2=fopen(“data1”,”r”);
while((c=getc(f2))!=EOF)
{

putc(c,f1);
}
fclose(f1);
fclose(f2);
printf(“the modified data file is \n”);
f1=fopen(“data”,”r”);
while((c=getc(f1))!=EOF)
putchar(c );
fclose(f1);
}


PROBLEM 89:- Write a program to copy contents of one file to another.

PROCEDURE:-

1.open a file to store text F1
2. open the F2 file in writing mode
3. repeat step 4 until EOF of F1 is encountered
4. extract character by character , and place it in F2
5. close the files

CODE:-

#include
void main()
{
FILE f1,f2;
char c;
f1=fopen(“data1”,”w”);
f2=fopen(“data2”,”w”);
printf(“enter data to a file”);
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“data1”,”r”);
while((c=getc(f1))!=EOF)
putc(c,f2);
fclose(f2);
f2=fopen(“data2”,”r”);
while((c=getc(f2))!=EOF)
putchar(c);
fclose(f1);
fclose(f2);
}

PROBLEM 90:- Write a program to see contents of file.

PROCEDURE:-

1.open a file to store text F1
2. open the F1 file in reading mode
3. repeat step 4 until EOF of F1 is encountered
4. extract character by character , and place it on screen
5. close the files

CODE:-

#include
void main()
{
FILE f1;
char c;
f1=fopen(“data1”,”w”);
printf(“enter data to a file”);
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“data1”,”r”);
while((c=getc(f1))!=EOF)
putc(c,f2);
fclose(f2);
f2=fopen(“data2”,”r”);
while((c=getc(f2))!=EOF)
putchar(c);
fclose(f1);
fclose(f2);
}

PROBLEM 91:- Write a program to error handling in file operations.

PROCEDURE:-

1. special functions feof() and fopen() are used to check
2. whether the opening file is available or not and to see that
3. reading of data beyond EOF mark is avoided

CODE:-

#include
void main()
{
char *filename;
FILE *fp1,*fp2;
int i,num;
for(i=10;i<=100;i+=10) putw(i,fp1); printf(“input filename”); open-file: scanf(“%s”,filename); if((fp2= fopen(filename,”r”))= =NULL) { printf(“cannot open file”); printf(“type filename once again”); goto open-file; } else for(i=1;i<=20;i++) { num=getw(fp2); if(feof(fp2)) { printf(“ran out of data”); break; } else printf(“%d”,num); } fclose(fp2); } PROBLEM 92:- Write a formatted data to a file and extract the same(fscanf,fprintf) PROCEDURE:- 1. fscanf () and fprintf () are used to store data in formatted form and to extract the same in formatted form CODE:- #include
void main()
{
FILE *fp;
int num,qty,i;
float price,value;
char item[10],filename[10];
fp=fopen(filename,”w”);
printf(“ itemname number price qty\n”);
for(i=1;i<=3;i++) { scanf(“%s%d%f%d”,item,&num,&price,&qty); fprintf(fp,”%s %d %.2f %d”,item,num,price,qty); } fclose(fp); fp=fopen(filename,”r”); for(i=1;i<=3;i++) { fscanf(fp,”%s %d %f %d”,item,&num,&price,&qty); value=price*qty; printf(“%-8s %7d %8.2f %11.2f\n”,item,num,price,qty,value); } fclose(fp); } PROBLEM 93:- Write a program that uses the functions ftell and fseek. PROCEDURE:- 1. file pointer can be move anywhere in the file randomly using fseek() 2. ftell() is used to find the number of bytes the current pointer is existing from beginning of the file. CODE:- #include
void main()
{
FILE *fp;
long n;
char c;
fp=fopen(“random”,”w”);
while((c=getchar())!=EOF)
putc(c,fp);
fclose(fp);
fp=fopen(“random”,”r”);
n=0;
while(feof(fp)= =0)
{
fseek(fp,n,0);
printf(“position of %c is %ld”,getc(fp),ftell(fp));
n=n+5;
}
fclose(fp);
}


PROBLEM 94:- Write a program that will receive a filename and a line of text as command line arguments and write the text to afile.
PROCEDURE:-

1. command line arguments are argc, argv which are used as parameters in main function
2. argc specifies the no. of strings involved in the command
3. argv specifies strings involved in the command
4. command line arguments are used to redefine existing dos commands in our terms
5. as well as to provide file names at run time

CODE:-

#include
void main(int argc,char *argv[])
{
FILE *fp;
int i;
char word[15];
fp=fopen(argv[1],”w”);
printf(“\nNo. Of arguments in command line =%d\n”,argc);
for(i=2;i
void main(int argc,char *argv[])
{
FILE *f1,*f2;
char c;
f1=fopen(argv[1],”r”);
f2=fopen(argv[2],”w”);
while((c=getc(f1))!=EOF)
putc(c,f2);
fclose(f1);
fclose(f2);
}

PROBLEM 96:- Write a program to see contents of a file using command line arguments.

PROCEDURE:-

1 command line arguments are argc, argv which are used as parameters in main function
2 argc specifies the no. of strings involved in the command
3 argv specifies strings involved in the command
4 command line arguments are used to redefine existing dos commands in our terms
5 as well as to provide file names at run time

CODE:-

#include
void main(int argc,char *argv[])
{
FILE *f1;
char c;
f1=fopen(argv[1],”r”);
while((c=getc(f1))!=EOF)
putchar (c );
fclose(f1);
}


PROBLEM 97:- Write a program to delete a file using command line arguments.

PROCEDURE:-

1. command line arguments are argc, argv which are used as parameters in main function
2. argc specifies the no. of strings involved in the command
3. argv specifies strings involved in the command
4. command line arguments are used to redefine existing dos commands in our terms
5. as well as to provide file names at run time

CODE:-

void main(int argc,char *argv[])
{
FILE *f1;
f1=fopen(argv[1],”r”);
remove(f1);
fclose(f1);
}


PROBLEM 98:- Write a program to store a character string in a block of memory space created by MALLOC() and then modify the same to store a larger string.
PROCEDURE:-
1. MALLOC() function is used to allocate memory at run time , initialized with garbage values
2. realloc() is used to modify the existing size of memory allocated with the help of MALLOC()

CODE:-

#include
#define NULL 0
void main()
{
char *buffer;
if((buffer=(char *)malloc(10))= = NULL)
{
printf(“malloc failed”);
exit(1);
}
printf(“buffer of size %d created \n”,_msize(buffer));
strcpy(buffer,”HYDERBAD”);
printf(“\nBuffer containsn:%s\n”,buffer);
if((buffer=(char *)realloc(buffer,15))= =NULL)
{
printf(“reallocation failed\n”);
exit(1);
}
printf(“\nbuffer size modified\n”);
printf(“\nbuffer still contains : %s\n”,buffer);
strcpy(buffer,”SECUNDERABAD”);
printf(“\nbuffer now contains : %s\n”,buffer);
free(buffer);
}

PROBLEM 99:- Write a small example program to explain the usage of CALLOC().

PROCEDURE:-

1. CALLOC() function is used to allocate memory for a set of blocks at run time

CODE:-

#include
#define NULL 0
void main()
{
char *buffer;
if((buffer=(char *)calloc(10,1))= = NULL)
{
printf(“calloc failed”);
exit(1);
}
printf(“buffer of size %d created \n”,_msize(buffer));
strcpy(buffer,”HYDERBAD”);
printf(“\nBuffer containsn:%s\n”,buffer);
if((buffer=(char *)realloc(buffer,15))= =NULL)
{
printf(“reallocation failed\n”);
exit(1);
}
printf(“\nbuffer size modified\n”);
printf(“\nbuffer still contains : %s\n”,buffer);
strcpy(buffer,”SECUNDERABAD”);
printf(“\nbuffer now contains : %s\n”,buffer);
free(buffer);
}








No comments:

Post a Comment