You are on page 1of 37

C Lab Problem Solve

Lab1 Write the program to add two numbers entered from a #include<stdio.h> keyboard. #include<conio.h> void main() {int a,b,sum=0; printf("Enter the two number:"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum is:%d",sum); getch(); } Write a program to print sum, product and quotient of two numbers 22 and #include<stdio.h> 7. #include<conio.h> void main() {int a=22,b=7,sum,prodt; float quet; sum=a+b; prodt=a*b; quet=(float)a/b; printf("sum is:%d",sum); printf("Product is:%d",prodt); printf("Question is:%f",quet); getch(); } Write a C Program to calculate the volume of a pool. The equation for the volume determining is Vol ume = length x width x depth. Assume that the pool has a length of 25 feet, a width of feet and depth 6 feet. 10 #include<stdio.h> #include<conio.h> void main() {int l=25,b=10,d=6,volume; volume=l*b*d; printf("Volume of pool %d",volume); getch(); } Write a program to input temperature in Celsius & to print its Fahrenheit #include<stdio.h> equivalent. #include<conio.h> void main() { float c,f; printf("Enter the temperature in centigrade"); scanf("%f",&c); f=(5.0/9.0)*(c+32.0); printf("Temperature in farenheit is:%f",f); getch(); } Write a program to find the number of digits of an integer (Hints find logarithm and add one on the integer part.) #include<stdio.h> #include<conio.h> #include<math.h> void main() {

Er.Pramod Kumar Singh

Page 1

C Lab Problem Solve


int n,number; printf("Enter the number"); scanf("%d",&n); number=log10(n)+1; printf("Number of digit=%d", number); getch(); } Given a 5 digit integer number. Write a program to print it in reverse order #include<stdio.h> #include<conio.h> void main() { int n,r,rev=0; printf("Enter the number"); scanf("%d",&n); do { r=n%10; rev=rev*10+r; n=n/10; } while(n>0); printf("Reverse number %d",rev); getch(); } Calculate the functional values as follows: (a) f1 = (x 3 + y 3 + z 3 )2 (b) f2 = (x+y)2 z 2 2xyz + e (x- y) (c) f3 = sin(x) + atan(y) + log(z) /* argument within sin(x) is in radians */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x,y,z,b,f1,f2,f3; printf("Enter value of x:"); scanf("%f",&x); printf("Enter the value of y:"); scanf("%f",&y); printf("Enter the value of z:"); scanf("%f",&z); b=pow(x,3)+pow(y,3)+pow(z,3); f1=pow(b,2); f2=pow((x+y),2)-pow(z,2)-2*x*y*z+exp(x-y); f3=sin(x)+atan(y)+log(z); printf("f1=%f f2=%f f3=%f",f1,f2,f3); getch(); } Write a program to compute: F= 2.9678 x 10 -27 + 0.876 x 10 -38 ________________________ 7.150X 101 6 9.75 x 10 #include<stdio.h> #include<conio.h> #include<math.h>
12

Er. Pramod Kumar Singh

Page 2

C Lab Problem Solve


void main() {double i; i=(2.9678*pow(10,-27)+0.876*pow(10,-38))/(7.150*pow(10,16)-9.75*pow(10,12)); printf("%e",i); getch(); } Write a program that accepts a character from the keyboard and displays it on the monitor. getchar and putchar i/o Use #include<stdio.h> function #include<conio.h> void main() {clrscr(); char a; printf("Enter the char:"); a=getchar(); printf("The character is:"); putchar(a); getch(); } Write a program that reads various data item ( numeric, character and string ) from the key board and display it on the monitor. Use scanf and printf i/o #include<stdio.h> functions #include<conio.h> void main() {char name[2],gender; clrscr(); int id; printf("Enter ur name:"); scanf("%[^\n]",&name); fflush(stdin); printf("Enter ur Gender:"); scanf("%c",&gender); printf("Enter Ur Id"); scanf("%d",&id); printf("\nUr name is:%s",name); printf("\nUr gender is:%c",gender); printf("\nur Id is:%d",id); getch(); } Write a program that reads a string from the standard input device and display it on the monitor. Use gets and puts i/o #include<stdio.h> functions. #include<conio.h> void main() {char name[20],address[30]; clrscr(); int id; printf("Enter ur name:"); gets(name); printf("Enter Ur Address:"); gets(address); printf("\nUr name is:"); puts(name); printf("\nUr gender is:"); puts(address); getch();

Er. Pramod Kumar Singh

Page 3

C Lab Problem Solve


} Write a program that reads a lowercase character from the keyboard and display it in uppercase on the monitor and vice versa. Use toupper and tolower library functions; include header file ctype.h #include<stdio.h> #include<conio.h> #include<ctype.h> //program to convert lowercase to uppercase /*void main() {char a,b; printf("Lowercase character is:"); a=getchar(); b=toupper(a); printf("Uppercase character is:%c",b); getch(); } */ //program to convert upper to lower void main() {char a,b; printf("uppercase character is:"); a=getchar(); b=tolower(a); printf("Lowercase character is:%c",b); getch(); } Write a program that reads a lowercase character from the keyboard and display it in the uppercase on the monitor without using toupper and tolower library #include<stdio.h> functions. #include<conio.h> //convert upper to lower /* void main() {char b,c; clrscr(); printf("enter char in upper"); scanf("%c",&b); c=b+'a'-'A'; printf("lower case letter%c",c); getch(); } */ void main() {char b,c; clrscr(); printf("enter char in lower"); scanf("%c",&b); c=b+'A'-'a'; printf("upper case letter%c",c); getch(); } Write a program to generate the following output. Enter your name: abcd your roll num, age, gender: 120, 20, Enter E mnter your college name: National College of Engineering Your name is: abcd

Er. Pramod Kumar Singh

Page 4

C Lab Problem Solve


Your roll num: 120 Your age: 20 Your gender: Your college name: National College of Engineering m #include<stdio.h> #include<conio.h> void main() {char name[20],gender,cname[20]; int roll,age; clrscr(); printf("Enter ur name:"); scanf("%[^\n]",&name); printf("Enter ur rollnum,age,Gender:"); scanf("%d%d %c",&roll,&age,&gender); fflush(stdin); printf("Enter Ur College name"); scanf("%[^\n]",&cname); printf("\nUr name is:%s",name); printf("\n\tyour rollnum:%d ",roll); printf("\n\t\tyour age:%d",age); printf("\n\t\t\t your gender:%c",gender); printf("\n Your college name:%s",cname); getch(); } Lab2 Write a program that reads a number from the keyboard and displays message the number is odd if it is odd otherwise message the number is even. #include<stdio.h> #include<conio.h> void main() { int x; printf("enter the number"); scanf("%d",&x); if(x%2==0) printf("Number %d is Even",x); else printf("Number %d is Odd",x); getch(); } Write a program to find all possible roots of a quadratic equation: ax2 + bx + c = 0; Possible cases: (i) Roots are real and distinct (ii) Roots are real and equal (iii) Roots are complex conjugate #include<stdio.h> #include<conio.h> #include<math.h> void main() {float a,b,c,d,x1,x2; printf("Enter the value of a,b,c"); scanf("%f %f d=b*b-4*a*c;//calculate square root of b2-4ac %f",&a,&b,&c); if(d==0) //calculation of real and equal {x1=-b/(2*a); x2=x2;

Er. Pramod Kumar Singh

Page 5

C Lab Problem Solve


printf("Roots are x1=%f x2=%f",x1,x2); } else if(d>0)//calculation of real and distinct {x1=(-b+sqrt(d))/(2*a); x2= (-b-sqrt(d))/(2*a); printf("Roots are x1=%f x2=%f",x1,x2); } else //calculation of complex conjugate { x1=-b/(2*a); x2=sqrt(-d)/(2*a); printf("The Comple eqn is %f+%fi",x1,x2); } getch(); } Write a program to find whether the given 4digit number (Year) is a leap year or not leap year . #include<stdio.h> #include<conio.h> void main() {int year; printf("Enter the year"); scanf("%d",&year); if(year%4==0) {if(year%100==0) {if(year%400==0) printf("Year is leap year"); else printf("year isnot leap year"); } else printf("Year is leap Year"); } else printf("Year is not a leap year"); getch(); } Write a program to read 3-digits number and test whether it is an Armstrong number or not #include<stdio.h> Armstrong. #include<conio.h> void main() {int i,n,r,a,b,s=0; printf("Enter the range say "); scanf("%d",&n); b=n w ; hile(b!=0) { r=b %10; s=s+r*r*r; b=b/10; } if(s==n ) rintf("%d is a amstrong number",n); p else printf("%d isnot a amstrong number",n); getch(); return;

Er. Pramod Kumar Singh

Page 6

C Lab Problem Solve


} Write a program to input five digit integer and print sum of digits in it.(52137=>5+2+1+3+7=18) #include<stdio.h> #include<conio.h> void main() {long int n,a,sum=0,i; printf("Enter the number"); scanf("%ld",&n); for(i=0;i<5;i++) { a=n%10; n=n/10; sum=sum+a; } printf("sum=%ld",sum); getch(); return; } Write a program to find highest common factor of two numbers. #include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e,r; printf("Enter the value of a:b"); scanf("%d %d",&a,&b); c=a; d=b; l1: r=c%d; if(r>0) { c=d; d=r; goto l1; } printf("hcf=%d",d); e=a*b/d; printf("lcm=%d",e); getch(); return; } #include<stdio.h> #include<conio.h> void main() { int a,b,r; printf("enter the ewo number"); scanf("%d%d",&a,&b); do { r=a%b; if(r==0) printf("HCF=%d",b); else {

Er. Pramod Kumar Singh

Page 7

C Lab Problem Solve


a=b; b=r; } } while(r!=0); getch(); } Write a program to read three numbers and display the following menu. Menu : 1 Summatio 2 Sum of . n . squares of 3 Sum . cubes 4 Produc and perform tasks as per users choice. (Use switch statements) . t #include<stdio.h> #include<conio.h> void main() { int a,b,c,n,square,cube,sum,product; printf("Enter the three number"); scanf("%d%d%d",&a,&b,&c); printf("\n1.sum\n"); printf("2.sum of square\n"); printf("3.sum of cube\n"); printf("4.product\n"); printf("Enter ur choice"); scanf("%d",&n); switch(n) { case 1: sum=a+b+c; printf("Sum is:%d",sum); break; case 2: square=a*a+b*b+c*c; printf("Sum of square is:%d",square); break; case 3: cube=a*a*a+b*b*b+c*c*c; printf("Sum of cube is:%d",cube); break; case 4: product=a*b*c; printf("Product is:%d",product); break; default: printf("Wrong choice"); } getch(); } Write a program to read a character and to test whether it is an alphabet or a number or a character. special #include<stdio.h> #include<conio.h> void main() {

Er. Pramod Kumar Singh

Page 8

C Lab Problem Solve


char a; printf("Enter the character:"); scanf("%c",&a); if(a>=65&&a<=90) printf("Enter character ia upper Alphabet"); else if(a>=97&&a<=122) printf("Enter character is lower Alphate"); else if(a>=48&&a<=57) printf("Enter character is number"); else if(a>=33&&a<=47) printf("The character is special synbol"); else if(a>=58&&a<=64) printf("the character is relational symbol"); else if(a>=91&&a<=96) printf("Enter character is special symbol[\]^"); else printf("These are other symbol"); getch(); } Write a program to read three sides of triangle and print area for valid data and to print Invalid data if either one side of the triangle is greater or equals to the sum of other two sides. #include<stdio.h> #include<conio.h> void main() { int a,b,c,area=0; printf("enter the three sides"); scanf("%d%d%d",&a,&b,&c); if(c>=(a+b)||a>=(b+c)||b>=(c+a)) printf("Wrong value"); else { s=1/2(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("area of triangle=%d",area); } getch(); } Lab3 To print table of ASCII Characters for code 32 to 255. #include<iostream.h> #include<conio.h> void main() { int i; for (i=32;i<=255;i++) { printf("%c\t",i); } getch(); } To read any integer number and to print its multiplication table. #include<stdio.h> #include<conio.h> void main() { int i=1,a,b;

Er. Pramod Kumar Singh

Page 9

C Lab Problem Solve


printf("Enter the number:"); scanf("%d",&a); while(i<=10) { b=a*i; printf(" %dX%d=%d\n",a,i,b); i++; } getch(); } Write a program to generate the following multiplication table of numbers: 1 2 3 4 5 6 7 8 9 10 . 20 30 40 50 60 70 80 90 100 10 Note: solve this problem using both for loop, while and do-while #include<stdio.h> #include<conio.h> //using for loop void main() {clrscr(); int i,j,n; for(i=1;i<=10;i++) { for(j=1;j<=10;j++) { n=i*j; printf("%d\t",n); } printf("\n"); } getch(); } //using while void main() { clrscr(); int i=1,j=1,n; while(i<=10) { while(j<=10) { n=i*j; printf("%d\t",n); j++; } i++; j=1; printf("\n"); } getch(); } //using do void main() while { clrscr(); int i=1,j=1,n; do { do { n=i*j;

Er. Pramod Kumar Singh

Page 10

C Lab Problem Solve


printf("%d\t",n); j++; } while(j<=10); i++; j=1; printf("\n"); } while(i<=10); getch(); } To print all three digit Armstrong #include<stdio.h> numbers #include<conio.h> void main() {int i,n,r,a,b,s; printf("Enter the range say "); scanf("%d",&n); for(i=1;i<=n;i++) { a=i; s=0; while(a>0) { r=a%10; s=s+r*r*r; a=a/10; } b=s; if(b==i) printf("n=%d",b); } getch(); return; } To find out decimal equivalent of a given octal number of arbitrary length. #include<stdio.h> #include<conio.h> void main() { int num,i,base=1,sum=0,rem; printf("Enter the Octal Number"); scanf("%d",&num); while(num!=0) { rem=num%10; sum=sum+rem*base; base=base*8; num=num/10; } printf("Decimal Equivalent is:%d",sum); getch(); } To find out octal equivalent when a decimal integer number is #include<stdio.h> given. #include<conio.h> void main()

Er. Pramod Kumar Singh

Page 11

C Lab Problem Solve


{ int num,i,base=1,sum=0,rem; printf("Enter the decimal Number"); scanf("%d",&num); while(num!=0) { rem=num%8; sum=sum+rem*base; base=base*10; num=num/8; } printf("octal Equivalent is:%d",sum); getch(); } Write a program to read x and n, and generate the following series & print the sum. 2 -3/x 3 + 4/x 4 -5/x 5 n x + 2/x terms #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); float sum,x,n,t; printf("\Enter the value of x and n\n"); scanf("%f %f",&x,&n); sum=x; printf("%f ,",x); for(int i=1;i<=n-1;i++) { t=((i+1)*pow(-1,i+1))/pow(x,i+1); printf("%f ,",t); sum=sum+t; } printf("sum=%f",sum); getch(); } Find the factorial of a positive integer n using a repetitive structure. #include<stdio.h> #include<conio.h> void main() { int n,i; long int fact=1; clrscr(); printf("Enter the number"); scanf("%d",&n); if(n<=1) fact=1; else for(i=2;i<=n;i++) { fact*=i; } printf("n!=\t%ld",fact); getch(); } To print first 50 prime numbers.

Er. Pramod Kumar Singh

Page 12

C Lab Problem Solve


#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,j,n=1; for(i=0;i<50;i++) { for(j=2;j<n;j++) { if(n%j==0) { n++; j=2; } } printf("%4d",n); n++; } getch(); } Write a program to generate first n terms of a Fibonacci series. #include<stdio.h> #include<conio.h> void main() { int n,f1=1,f2=1,f3=0,i; clrscr(); printf("Enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) { f1=f2; f2=f3; f3=f1+f2; printf("f%d\t%d\n",i,f3); } getch(); } Write a computer program that reads in an integer value for n and then sums the integers from n to 2n if n non-negative, or from 2n to n if n is negative. Display the is #include<stdio.h> sum. #include<conio.h> void main() { clrscr(); int i,n,sum=0; printf("Enter the number"); scanf("%d",&n); if(n==0) sum=0; else if(n>0) { for(i=n;i<=2*n;i++) {sum+=i; }

Er. Pramod Kumar Singh

Page 13

C Lab Problem Solve


} else { for(i=2*n;i<=n;i++) {sum+=i; } } printf("the sum is:%d",sum); getch(); } Write a program to count the number of odd and even number entered by user. Your program must read numbers until the user enters zero. After user enters zero, display the #include<stdio.h> counts. #include<conio.h> void main() { int i,n,codd=0,ceven=0; printf("Enter the number"); while(n!=0) { scanf("%d",&n); if(n%2==0) ceven++; else codd++; } printf("Count of even number:%d",ceven); printf("Count of odd number:%d",codd); getch(); } Write a program to print the following series 1 2 5 10 17 26 37 until the term value is less than 500. #include<stdio.h> #include<conio.h> void main() { clrscr(); int a=1,b=1; while(a<500) {printf("%d\t",a); a=a+b; b=b+2; } getch(); } Write a program to find the sum of numbers from 1 to 999 which are exactly divisible by 3 and not by 5. #include<stdio.h> #include<conio.h> void main() { clrscr(); long int i,sum=0; for(i=1;i<=999;i++) { if(i%3==0&&i%5!=0) { sum=sum+i; printf("%ld\t",i); } }

Er. Pramod Kumar Singh

Page 14

C Lab Problem Solve


printf("sum is:%ld",sum); getch(); } Lab4 Program to display the following Menu menu: 1. Input three 2. numbers Summation of the 3. numbers Sum of 4. squares Product of the three 5. numbers program Exit prom and perform task as per users choice repeatedly until he/she chooses to exit. #include<stdio.h> #include<conio.h> void input(void); int sum(int,int,int); int sumsq(int,int,int); int pro(int,int,int); int a,b,c; void main() { clrscr(); int ch; printf("\n\t\t\tMain Menu\n"); printf("\t\t\t===============\n"); printf(\t\t\t1. Input number\n); printf("\t\t\t2.Sum of numbers\n"); printf("\t\t\t3. Sum of square of numbers\n"); printf("\t\t\t4. Product of numbers\n"); printf("\t\t\t5. Exit\n"); do { printf("\t\t\t\t\t\t\n\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: input(); break; case 2: { printf("\nSum of three numbers=%d",sum(a,b,c)); break; } case 3: { printf("\nSum of square of three numbers=%d",sumsq(a,b,c)); break; } case 4: { printf("\nProduct of three numbers=%d",pro(a,b,c)); break; } }

Er. Pramod Kumar Singh

Page 15

C Lab Problem Solve


} while(ch!=5); getch(); } void input(void) { printf("Input three numbers"); scanf("%d %d %d",&a,&b,&c); } int sum(int a,int b,int c) { return(a+b+c); } int sumsq(int a,int b,int c) { return(a*a+b*b+c*c); } int pro(int a,int b,int c) { return(a*b*c); } Menu 1. Circumference of a Circle 2. Area of cross-section of a hemisphere 3. Surface area of a sphere 4. Volume of a sphere 5. Exit from program #include<stdio.h> #include<conio.h> #define pi 3.1416 float cf(int); float csh(int); float as(int); float vs(int); void main() { clrscr(); int r,ch; printf("Input radius:"); scanf("%d",&r); printf("\n\t\t\tMain Menu\n"); printf("\t\t\t===============\n"); printf("\t\t\t1.circumference of a circle\n"); printf("\t\t\t2. Cross section area of hemisphere\n"); printf("\t\t\t3. Area of sphere\n"); printf("\t\t\t4. Volume of Sphere\n"); printf("\t\t\t5. Exit\n"); do { printf("\t\t\t\t\t\t\n\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: { printf("\nCircumference of a circle=%f",cf(r));

Er. Pramod Kumar Singh

Page 16

C Lab Problem Solve


break; } case 2: { printf("\nCross section area of hemisphere=%f",csh(r)); break; } case 3: { printf("\nArea of a sphere=%f",as(r)); break; } case 4: { printf("\nVolume of a sphere=%f",vs(r)); break; } } } while(ch!=5); getch(); } float cf(int r) { return(2*pi*r); } float csh(int r) { return(3*pi*r*r); } float as(int r) { return(pi*r*r); } float vs(int r) { return(4.0/3*pi*r*r*r); } Write a program to calculate factorial of a positive integer using 1. Programmer defined 2. function function Recursive #include<stdio.h> #include<conio.h> #define pi 3.1416 long int fact1(int); long int fact2(int); void main() { clrscr(); int n,ch; printf("Input a number:"); scanf("%d",&n); printf("\n\t\t\tMain Menu\n"); printf("\t\t\t===============\n"); printf("\t\t\t1.factorial using user defined function\n"); printf("\t\t\t2. factorial using recursive function\n");

Er. Pramod Kumar Singh

Page 17

C Lab Problem Solve


printf("\t\t\t\t\t\t\n\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: { printf("\nFactorial1=%ld",fact1(n)); break; } case 2: { printf("\nFactorila2=%ld",fact2(n)); break; } } getch(); } long int fact1(int n) { int i; long int fact=1; for(i=1;i<=n;i++) return(fact*i); } long int fact2(int n) { if(n==1) return (1); else return(n*fact2(n-1)); } Write a program to print the Fibonacci series using 3. Programmer defined 4. function function Recursive #include<stdio.h> #include<conio.h> #define pi 3.1416 void fib1(int); int fib2(int); int n,a,b,c,i; void main() { clrscr(); int ch; printf("\nHow many terms:"); scanf("%d",&n); printf("\n\t\t\tMain Menu\n"); printf("\t\t\t===============\n"); printf("\t\t\t1.fibonacci using user defined function\n"); printf("\t\t\t2. fibonacci using recursive function\n"); do { printf("\t\t\t\t\t\t\n\nEnter your choice:"); scanf("%d",&ch);

Er. Pramod Kumar Singh

Page 18

C Lab Problem Solve


switch(ch) { case 1: { fib1(n); break; } case 2: { for(i=1;i<=n;i++) printf(" %d",fib2(i)); break; } } } while(ch!=3); getch(); } void fib1(int n) { a=1; b=1; printf("\n%d %d",a,b); for(i=3;i<=n;i++) { c=a+b; printf(" %d",c); a=b; b=c; } } int fib2(int n) { if(n<=1) return (n); return(fib2(n-1)+fib2(n-2)); } Write a function to swap the contents of two variables using call by reference #include<stdio.h> method. void swap(int *,int *); void main() { int x,y; printf(Enter x and y); scanf(%d%d,&x,&y); printf(before calling swap \nx=%d\ny=%d,x,y); swap(&x,&y); printf(\n\n after calling the swap\nx=%d\n=y=%d,x,y); } void swap(int *p,int *q) { int temp; temp=*p; *p=*q; *q=temp; } Write a program that convert the hexadecimal number in the equivalent decimal numbers and versa. vice-

Er. Pramod Kumar Singh

Page 19

C Lab Problem Solve


#include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> int hextodec(char str[]) { int m,n,b,i,ss=0,s=0; printf("\nEnter hexadecimal number:"); scanf("%s",str); m=strlen(str); m--; for(i=0;str[i]!='\0';i++) { if(str[i]>='A'&&str[i]<='F') { n=str[i]-55; s+=n*pow(16,m-i); } if(str[i]>='0'&&str[i]<='9') { b=(int)str[i]-48; ss+=b*pow(16,m-i); } } return(s+ss); } void dectohex(int dec) { char hex[20]; int i=0,j,d; printf("\nEnter a decimal number"); scanf("%d",&dec); while(dec!=0) { d=dec%16; if(d>=10) hex[i]=(char)(d+55); else hex[i]=(char)(d+48); i++; dec=dec/16; } printf("\nHexadecimal="); for(j=i-1;j>=0;j--) printf("%c",hex[j]); } void main() { clrscr(); int ch,dec; char str[20]; printf("\n1.Hexacimal to decimal\n"); printf("\n2.Decimal to Hexadecimal \n"); printf("\n3.Exit"); do {

Er. Pramod Kumar Singh

Page 20

C Lab Problem Solve


printf("\n\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nHexadecimal=%d",hextodec(str)); break; case 2: dectohex(dec); break; } } while(ch!=3); getch(); } Write a program to calculate the value of the given series using user defined function.

x 1!

x2 2!

x3 3!

.......... ........

( 1) n

xn n!

#include<stdio.h> #include<conio.h> #include<math.h> float series(int n,float x); main() { int n; float x,y; printf("enter the value of n:"); scanf("%d",&n); printf("enter the value of x:"); scanf("%f",&x); y=series(n,x); printf("y=%f",y); getch(); return(0); } float series(int n,float x) { float term=(-x),sum=1; int i; for(i=1;i<n;i++) {sum+=term; term*=(-x)/(i+1); } return(sum); } Write a program to calculate the value of combination of n different objects taken r at a time. C(n,r) =

n! (n r )!r!

#include<stdio.h> #include<conio.h> long int fact(int n); main() {int r,n,s; long int comb; printf("Enter the value of n:"); scanf("%d",&n);

Er. Pramod Kumar Singh 21

Page

C Lab Problem Solve


printf("enter the value of r:"); scanf("%d",&r); s=n-r; comb=fact(n)/(fact(s)*fact(r)); printf("combination=%ld",comb); getch(); return(0); } long int fact(int n) {int i; long int fact=1; if(n<=1) return(1); else for(i=2;i<=n;i++) { fact*=i; } return(fact); } } Lab5 Write program to read seven days temperatures in an array and print deviations average temperature for each day. #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); float x[10],av,sum=0; int i; printf("\nEnter temperatures for\n"); for(i=0;i<7;i++) { printf("\nday[%d]: ",i); scanf("%f",&x[i]); sum=sum+x[i]; } av=sum/7; printf("\n\nAverage=%f",av); printf("\nDeviations of temperature from mean\n"); for(i=0;i<7;i++) printf("day[%d]: %f\n",i,fabs(x[i]-av)); getch(); } Read 10 nos. in to an array & print in reverse #include<stdio.h> order. #include<conio.h> #include<math.h> void main() { clrscr(); int x[10],i; printf("\nEnter 10 numbers");

Er. Pramod Kumar Singh

Page 22

C Lab Problem Solve


for(i=0;i<10;i++) { printf("\nNumber[%d]:",i); scanf("%d",&x[i]); } printf("\nReverse order:"); for(i=9;i>=0;i--) printf("\n%d",x[i]); getch(); } Write a program sort the numbers in either ascending or descending order using onedimensional array. #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); int x[10],i,j,temp,n; printf("\nHow many numbers?"); scanf("%d",&n); printf("\nEnter Numbers:"); for(i=0;i<n;i++) { printf("\nEnter number[%d]",i); scanf("%d",&x[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) {if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } } printf("\nData imn ascending order\n"); for(i=0;i<n;i++) printf("%d\n",x[i]); getch(); } Write a program to read two arrays having element of m x n matrix and display the sum of the array elements in the matrix form. #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); int a[5][5],b[5][5],s[5][5],m,n,i,j; printf("\nHow many rows?"); scanf("%d",&m); printf("\nHow many columns?"); scanf("%d",&n);

Er. Pramod Kumar Singh

Page 23

C Lab Problem Solve


printf("\nEnter elements of first matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nEnter elements of second matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); clrscr(); printf("\nFirst Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d\t",a[i][j]); printf("\n"); } printf("\nSecond Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d\t",b[i][j]); printf("\n"); } printf("\nSum:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { s[i][j]=a[i][j]+b[i][j] ; printf("%d\t",s[i][j]); } printf("\n"); } getch(); } Write a program to read an array of two di mensions, double the content of each element of that arraydisplay the result. Your program must have three functions read, process and display to and process read and display the array. (Write a computer program to raise the power of each elements of given matrix of order M X N by 2. Display the resultant #include<stdio.h> matrix.) #include<conio.h> #include<math.h> void input(int a[5][5]); void process(int a[5][5],int d[5][5]); void display(int d[5][5]); int a[5][5],d[5][5],m,n,i,j; void main() { clrscr(); input(a); process(a,d); display(d); getch(); } void input(int a[5][5]) {

Er. Pramod Kumar Singh

Page 24

C Lab Problem Solve


printf("\nHow many rows ?"); scanf("%d",&m); printf("\nHow many columns?"); scanf("%d",&n); printf("\nEnter elements of a matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); } void process(int a[5][5],int d[5][5]) { for(i=0;i<m;i++) for(j=0;j<n;j++) d[i][j]=2*a[i][j]; } void display(int d[5][5]) { printf("\nOriginal Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%4d",a[i][j]); printf("\n"); } printf("\n\nDoubled Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%4d",d[i][j]); printf("\n"); } } Read a matrix of 3 x 4 and calculate transpose of the matrix and print #include<stdio.h> them. #include<conio.h> #include<math.h> void main() { clrscr(); int a[5][5],t[5][5],m,n,i,j; printf("\nHow many rows ?"); scanf("%d",&m); printf("\nHow many columns?"); scanf("%d",&n); printf("\nEnter elements of a matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); clrscr(); printf("\nOriginal Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d\t",a[i][j]); printf("\n"); }

Er Pramod Kumar Singh

Page 25

C Lab Problem Solve


printf("\nTransposed Matrix:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%d\t",a[j][i]); printf("\n"); } getch(); } Write a program to arrange characters in a word in alphabetical order. #include<stdio.h> #include<conio.h> #include<string.h> void main() {char name[20]; int n,i,j,temp; printf("Enter the string"); gets(name); n=strlen(name); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(name[i]>name[j]) { temp=name[i]; name[i]=name[j]; name[j]=temp; } } } printf("required string is"); puts(name); getch(); }

Er. Pramod Kumar Singh

Page 26

C Lab Problem Solve


Lab 6 Write a program that takes three variables (a, b, c) and rotates the values stored such that value of a goes to b, b to c and c to a. use pointers for #include<stdio.h> rotation. #include<conio.h> void main() { int *a,*b,*c,temp; printf("\nEnter the three number"); scanf("%d%d%d",a,b,c); printf("\n Before rotation"); printf("a=%d b=%d c=%d",*a,*b,*c); temp=*a; *a=*b; *b=*c; *c=temp; printf("\nAfter Swaping "); printf("a=%d b=%dc=%d",*a,*b,*c); getch(); } Write a computer program to find the sum of all the elements of an integer array of length k using concept of pointer the #include<stdio.h> #include<conio.h void main() { int *a,j, k,sum=0; printf("Enter number of term"); scanf("%d",&k); for (int i=0;i<k;i++) scanf("%d",(a+i)); for(j=0;j<k;j++) sum=sum+*(a+j); printf("Sum is=%d",sum); getch(); } Write a user defined function to swap the values of two variables. After swapping, display the value from the main function (Use Pointer swapped #include<stdio.h> Concept) #include<conio.h> void swap1(int *,int *); void main() { int a,b; printf("Enter first no"); scanf("%d",&a); printf("Enter decond no"); scanf("%d",&b); printf("Before swapping:"); printf("a=%d b=%d",a,b); swap1(&a,&b); printf("a=%d b=%d",a,b); getch(); } void swap1(int *a,int *b) { int temp;

Er. Pramod Kumar Singh

Page 27

C Lab Problem Solve


temp=*a; *a=*b; *b=temp; } Write a computer program to read the score of 24 students in a class using integer type of array. Find and display the average score of that class. Use the concept of function and #include<stdio.h> pointer. #include<conio.h> int score(int *a[]); void main() { int sum,average,*a[20]; for(int i=0;i<3;i++) { printf("Enter the number"); scanf("%d",&*a[i]); } average=score(a); printf("%d",average); getch(); } int score(int *a[]) { int sum=0,average; for(int i=0;i<3;i++) { sum+=*a[i]; } average=sum/3; return(average); } Write a function that takes a string as the input and counts the number of vowels and consonants, numeric character and other characters. The calling function should read the string and pass it to the function. After the function call, the count of vowels, consonants, numeric characters and character should be displayed in the calling . other #include<stdio.h> function #include<conio.h> #include<ctype.h> void charactercount(char[],int[]); void main() {char string[50]; int counts[4]={0,0,0,0}; printf("Enter the string:\n"); gets(string); charactercount(string,counts); printf("\nNumber of vowel:%d",counts[0]); printf("\nNumber of Consoneants:%d",counts[1]); printf("\nNumber of digit=%d",counts[2]); printf("\nNumberof other character=%d",counts[3]); getch(); } void charactercount(char str[],int count[]) { int i; for(i=0;str[i]!='\0';i++) { if(isalpha(str[i])!=0)

Er. Pramod Kumar Singh

Page 28

C Lab Problem Solve


{ if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u' ||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') count[0]++; else count[1]++; } else if(isdigit(str[i])!=0) count[2]++; else count[3]++; } } Write a computer program to sort the contents of one dimensional array of length in descending order. Use the concept of both pointer and #include<stdio.h> function. #include<conio.h> void sort(float *,int); void main() {int n,i; float *marks; printf("enter the number of element"); scanf("%d",&n); printf("Enter the element"); for(i=0;i<n;i++) scanf("%f",(marks+i)); sort(marks,n); for(i=0;i<n;i++) printf("element are %f\t",*(marks+i)); getch(); } void sort(float *marks,int n) {int i,j,temp; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(*(marks+j)<*(marks+j+1)) { temp=*(marks+j+1); *(marks+j+1)=*(marks+j); *(marks+j)=temp; } } } } Write a program to reverse of string (character array) using pointer. #include<stdio.h> #include<string.h> #include<conio.h> void main() {char str[15],rev[15]; int i,count; char*output_ptr; printf("Enter the string"); gets(str); count=strlen(str);

Er. Pramod Kumar Singh

Page 29

C Lab Problem Solve


output_ptr=rev+strlen(str)-1; for(i=0;i<count;i++) { *(output_ptr-i)=*(str+i); } printf("Oroginal string:%s\n",str); printf("Reverse stringis:"); puts(rev); getch(); } Write a program that asks the user to enter number of students. Once the number is entered, allocate just enough storage space to store the marks of one subject for the number of students entered by the Read the data and display them. After display free the reserved memory space.[ Hint: user. malloc() or calloc() and free()] use #include<stdio.h> #include<conio.h> #include<alloc.h> void main() { int *marks,i,n; printf("Enter the number of students"); scanf("%d",&n); marks=(int *)malloc(n*sizeof(int)); printf("Enter the number of marks"); for(i=0;i<n;i++) scanf("%d",(marks+i)); printf( "\nThe marks of the relative number of student"); for(i=0;i<n;i++) {printf("\nThe marks of %d student is:%d",i,*(marks+i)); } free(marks); getch(); } Lab7 Write a program to maintain the records of different parts available in store using their Part no, Part name, Stock quantity, Rate, Re-order level using #include<stdio.h> structure. #include<conio.h> struct part { int part_no; char part_name[20]; int stock_qty; int rate; int re_order_level; }; void main() {struct part p1; printf("Enter the detail of parts"); printf("\npart no:"); scanf("%d",&p1.part_no); printf("\n part name"); scanf("%s",p1.part_name); printf("\nstock quantity"); scanf("%d",&p1.stock_qty); printf("\n rate:"); scanf("%d",&p1.rate);

Er. Pramod Kumar Singh

Page 30

C Lab Problem Solve


printf("\nreorder level"); scanf("%d",&p1.re_order_level); printf("\nDetail of the entered parts:\n"); printf("Partno:%d\npart_name:%s\nstock:%d\nrate:%d\nreorder level:%d",p1.part_no,p1.part_name,p1.stock_qt y,p1.rate,p1.re_order_level); getch(); } Write a computer program that reads several different names and addresses into the rearranges computer, the name in alphabetical order, and then writes out the rearranged list. Make use of structures variables within the program. #include<stdio.h> #include<conio.h> #include<string.h> void reorder(int n, char name[][12]); struct student { char name[10][12]; }; void main() {clrscr(); struct student record; int n=0,i; do{ printf("String %d:",n+1); scanf("%s ",record.name[n]); } while(strcmp(record.name[n++],"END")); n--; reorder(n,record.name); for(i=0;i<n;i++) printf("\n string %d:%s ",i+1,record.name[i]); getch(); } void reorder(int n, char name[][12]) { char temp[12]; int i,item; for(item=0;item<n-1;item++) { for(i=item+1;i<n;i++) { if(strcmp(name[item],name[i])>0) {strcpy(temp,name[item]); strcpy(name[item],name[i]); strcpy(name[i],temp); } } } return; } Create a structure named student that has name, roll, marks and remarks as members. appropriate type and size of members. Write a complete program using the structure to read Assume and display the data entered by the #include<stdio.h> user. #include<conio.h> struct student{ char name[20]; int roll_no;

Er. Pramod Kumar Singh Page 31

C Lab Problem Solve


int marks; char remarks[20]; }; void main() { clrscr(); struct student record[10]; for( int i=1;i<=2;i++) { printf("Enter the name roll no & marks"); scanf("%s %d %d %s",&record[i].name,&record[i].roll_no,&record[i].marks,&record[i].remarks); } for( i=1;i<=2;i++) printf("name is:%s\t roll no:%d\t marks is:%d \tremarks is:%s\n",record[i].name,record[i].roll_no,record[i].marks,record[i].remarks); getch(); } Create a structure named marks that has the subject and mark as its members. Include this as a member for the structure created in Program 1 and read data for 10 students. The structure modified must have provision to store marks of three subjects for each student. Write a structure complete for that. [Hint: Create a structure before student structure and make one member an array program three elements of type marks]. of #include<stdio.h> #include<conio.h> struct marks { char subject[20]; int marks; }; struct student { char name[20]; int roll_no; char remarks[20]; struct marks record2[3]; }; void main() { clrscr(); int i,j; struct student record[10]; for( i=1;i<=2;i++) { printf("Enter the name roll no & remarks"); scanf("%s %d %s",&record[i].name,&record[i].roll_no,&record[i].remarks); for(j=1;j<=3;j++) { printf("Enter the subjectmarks"); scanf("%s %d",&record[i].record2[j].subject,&record[i].record2[j].marks); } } for( i=1;i<=2;i++) { printf("name is:%s\t roll no:%d\tremarks is:%s\n",record[i].name,record[i].roll_no,record[i].remarks); for(j=1;j<=3;j++) { printf(" subject is:%s marks is:%d\n",record[i].record2[j].subject,record[i].record2[j].marks);

Er. Pramod Kumar Singh

Page 32

C Lab Problem Solve


} } getch();101203 } Write a program that contains the customers name (char), loan_number (int) and balance (float) of customers in a bank and print the name that has the highest loan from the 100 #include<stdio.h> bank. #include<conio.h> struct customer { char name[20]; int loan_number; float balance; }; void main() { int i,j; float b,*cb; //it is used to eliminate the linker error //floating point formats are not linked,abnormal program termination . cb=&b; //it assign the address of b into cb so that floating //version point can be used without error. of scanf struct customer c[100],temp; printf("\nEnter the detail of customer"); for(i=0;i<5;i++) { printf("\nEnter detail of %d customer",i+1); printf("\nEnter name:"); scanf("%s",c[i].name); flushall(); printf("\nEnter loan number"); scanf("%d",&c[i].loan_number); printf("\nEnter the balance"); scanf("%f",&c[i].balance); } printf("\nCustomer name\t\t Loan number\t\tbalance"); for(i=0;i<5;i++) {printf("\n%s\t\t\t%d\t\t\t%f",c[i].name,c[i].loan_number,c[i].balance); } // searching name of person who have height loan for(i=0;i<4;i++) { for(int j=i+1;j<5;j++) { if(c[i].balance<c[j].balance) { temp=c[i]; c[i]=c[j]; c[j]=temp; } } } printf("\nThe name of the person who have high loan %s",c[0].name); getch(); } Write a program to read name and date of birth of n students and displayed their name corresponding age. and #include<stdio.h> #include<conio.h> #include<dos.h> struct dob{

Er. Pramod Kumar Singh

Page 33

C Lab Problem Solve


int day; int month; int year; }; struct student { char name[20]; struct dob sdob[100]; }; void main() {int dday,dmonth,dyear,age[100],i; struct date d; getdate(&d); struct student s[100]; for(i=0;i<=1;i++) { printf("\nenter the %d student name",i+1); scanf("%s",s[i].name); printf("\nEnter the day:"); scanf("%d",&s[i].sdob[i].day); printf("\n Enter the months"); scanf("%d",&s[i].sdob[i].month); printf("\nEnter the year"); scanf("%d",&s[i].sdob[i].year); } for(i=0;i<=1;i++) { dday=d.da_day-s[i].sdob[i].day; dmonth=d.da_mon-s[i].sdob[i].month; dyear=d.da_year-s[i].sdob[i].year; if(dday<0) { dmonth-=1; } if(dmonth<0) {dyear-=1; } age[i]=dyear; } printf("name \t\t\t\t Age:\n"); for(i=0;i<=1;i++) printf("\n%s\t\t\t\t\t%d",s[i].name,age[i]); getch(); } Lab8 Write a program that creates a file and write some text entered by user to this file. #include<stdio.h> #include<conio.h> main() { FILE *fp1; char name[20]; fp1=fopen("3.txt","w"); printf("Enter the name"); scanf("%s",&name); fprintf(fp1,"%s ",name); fclose(fp1);

Er. Pramod Kumar Singh

Page 34

C Lab Problem Solve


getch(); return(0); } Write a program to create a file Employee.dat having several data of the following format of the following Employee code, Employee name, date of birth and #include<stdio.h> salary. #include<conio.h> struct dob{ int day; int month; int year; }; struct employee { int employee_code; char employee_name[20]; struct dob s1; int salary; }; void main() {FILE *fp; struct employee e; int i,n; fp=fopen("employee.txt","w"); clrscr(); printf("Enter the number of employee\n"); scanf("%d",&n); printf("\n Enter the record of the employee\n(employee_code,employee name,dob(day/month/year),salary)"); for(i=0;i<n;i++) { scanf("%d%s%d%d%d%d",&e.employee_code,e.employee_name,&e.s1.day,&e.s1.month,&e.s1.year,&e.sal ary); fprintf(fp,"%d\t%s\t%d/%d/%d\t%d\n",e.employee_code,e.employee_name,e.s1.day,e.s1.month,e.s1.year,e.s alary); } fclose(fp); getch(); } Write another program to access the above file and print : -Employee details who were born before year 1980. -Average salary. Write a program that opens a file and copies all its contents to another file. Take the source and destination file from the user. the #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fs,*ft; char ch; fs=fopen("3.txt","r");

Er. Pramod Kumar Singh

Page 35

C Lab Problem Solve


if(fs==NULL) { puts("Cannot open source file"); exit(1); } ft=fopen("xyz.txt","w"); if(ft==NULL) { puts("cannot open target file"); fclose(fs); exit(1); } while(1) { ch=fgetc(fs); if(ch==EOF) break; else fputc(ch,ft); } fclose(fs); fclose(ft); getch(); } Write a program to open a new file. Read the name, address and telephone no. of ten persons from the and write them to the file. After writing display the content of the user #include<stdio.h> file. #include<conio.h> void main() { FILE *fp1; char name[20][10],address[20][10]; long int telno[10]; int i; fp1=fopen("4.txt","w"); for(i=1;i<=2;i++) { printf("Enter the name,address,telno"); scanf("%s %s %ld",&name,&address,&telno); fprintf(fp1,"%s %s %ld ",name,address,telno); } fclose(fp1); fp1=fopen("4.txt","r"); for(i=1;i<=2;i++) { fscanf(fp1,"%s %s %ld",&name,&address,&telno); printf("name is: %s\t address is:%s\t tel no is %ld",name,address,telno); } fclose(fp1); getch(); }

Er. Pramod Kumar Singh

Page 36

C Lab Problem Solve


Write a program to open a new file ,read roll number ,name address and phone numberof until the user says no, after reading all the data write it to the file.And display the records from students file the in alphabetical order of students name. A file Elect.dat contains the consumers data; custom_no, old and new meter reading . Write a rogram to print electricity bills according to the following policy. p Units Consumed Rate of 0-200 Rs 4.50 per charge 201-400 Rs 5.50 per unit unit 401-above Rs 7.50 per unit

Er. Pramod Kumar Singh

Page 37

You might also like