C Programming
C Programming
C Programming lab
1. While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses. 2. If his basic salary is less than Rs. 1500, then HRA=10% of basic salary and DA=25% of basic. If his salary is either equal to or above Rs. 1500, then HRA=Rs. 500 and DA=50% of basic. If the employees salary is input through the keyboard write a program to find his gross salary. 3. The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules: Percentage above or equal to 75 Honors Percentage above or equal to 60 First Division Percentage between 50 and 59 Second Division Percentage between 40 and 49 Third Division Percentage less than 40 Fail Write a program to calculate the division obtained by the student. 4. If a number 972 is entered through the keyboard, your program should print Nine Seven Two. Write a program such that it does this for any positive integer. 5. Write a program that, for all positive integers i,j,k, and l from 1 through 500, finds and prints all possible combinations of i , j,k and l such that i+j+k=l and i<j<k<l. 6. A positive integer is entered through the keyboard. Along with it the base of the numbering system in which you want to convert this number is entered. Write a program to display the number entered, the base, and the converted number. For example, if the input is 64 2 then the output should be 64 2 1000000. Similarly, if the input is 64 16, then the output should be 64 16 40. 7. Any character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters. Characters ASCII Values A-Z 65-90 a-z 97-122 0-9 48-57 Special symbols 0-47, 58-64, 91-96, 123-127 8. Write a program to find the factorial value of any number entered through the keyboard. 9. Two numbers are entered through the keyboard. Write a program to find the value of one number raised to power of another. 10. Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255. 11. Write a program to print out all Armstrong numbers between 1 to 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1*1*1) + (5*5*5) + (3*3*3).
12. Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered. 13. Write a program to receive an integer and find its octal, hexadecimal and binary equivalent. 14. Write a program to generate all combinations of 1, 2 and 3 using for loop. 15. Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue). 16. Write a program to print the multiplication table of the number entered by the user. 17. Write a program to check whether number entered through keyboard is palindrome or not. 18. Write a program to print Fibonacci Series from 1 to 100. 19. Write a program to compute and display the sum of all integers that are divisible by 6 but not divisible by 4 and lie between 0 to 100. The program should also count and display the numbers of such values. 20. Write a program to compute the real roots of a quadratic equation ax2 +bx +c =0 The roots are given by the equations x1= -b + (sqrt(b2-4ac)/2a) x2= -b - (sqrt(b2-4ac)/2a) The program should request for the values of the constants a, b and c and print the values of x1 and x2. Use the following rules: (a) No solution, if both a and b are zero (b) There is only one root, if a=0 (x=-c/b) (c) There are no real roots, if b2-4ac is negative (d) Otherwise, there are two real roots Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output message. 21. Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321. Also find the sum of all digits, even digits and odd digits. 22. An electric power distribution company charges its domestic consumers as follows: Computation Units Rate of Charge 0-200 Rs. 0.50 per unit 201-400 Rs. 100 plus Rs. 0.65 per unit excess of 200 401-600 Rs. 230 plus Rs. 0.80 per unit excess of 400 601 and above Rs. 390 plus Rs. 1.00 per unit excess of 600 The program reads the customer number and power consumed and prints the amount to be paid by the customer. 23. Write a program to input ten numbers from keyboard and find the sum of all even numbers and odd numbers. 24. Write a program to find HCF (Highest Common Factors) of two numbers. 25. Write a program to compute the value of Eulers number (E) that is used as the base of natural logarithms. Use the following formula. E=(1/0!)+(1/1!)+(1/2!)+..+1/n!
1. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { int qty,price,total=0,dis=0; clrscr(); printf("Enter Price: Rs. "); scanf("%d",&price); printf("\nEnter Quantity: "); scanf("%d",&qty); if(qty>1000) dis=0.1*price*qty; total=(qty*price)-dis; printf("\nDiscount: Rs. %d",dis); printf("\n\nTotal Expense: Rs. %d",total); getch(); }
Enter Price: Rs. 700 Enter Quantity: 1200 Discount: Rs. 84000 Total Expense: Rs. 756000
2. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int salary; float gross,hra,da; printf("Enter your basic salary: Rs. "); scanf("%d",&salary); if(salary<1500) { hra=0.1*salary; da=0.25*salary; } else { hra=500.0; da=0.5*salary; } gross=salary+hra+da; printf("Gross Salary is: Rs. "); printf("%f",gross); getch(); }
Enter your basic salary: Rs. 5000 Gross Salary is: Rs. 8000.000000 (Screen Clears) Enter your basic salary: Rs. 1000 Gross Salary is: Rs. 1350.000000
3. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); float m1,m2,m3,m4,m5,per,tot; printf("Enter marks in 1st subject: "); scanf("%f",&m1); printf("Enter marks in 2nd subject: "); scanf("%f",&m2); printf("Enter marks in 3rd subject: "); scanf("%f",&m3); printf("Enter marks in 4th subject: "); scanf("%f",&m4); printf("Enter marks in 5th subject: "); scanf("%f",&m5); tot=m1+m2+m3+m4+m5; per=tot/5; printf("Percentage: %f%\n",per); if(per>=75) printf("Division: Honors "); if(per<75 && per>=60) printf("Division: First Division "); if(per<60 && per>=50) printf("Division: Second Division "); if(per<50 && per>=40) printf("Division: Third Division "); if(per<40) printf("Division: Fail "); getch(); }
Enter marks in 1st subject: 84 Enter marks in 2nd subject: 89 Enter marks in 3rd subject: 92 Enter marks in 4th subject: 78 Enter marks in 5th subject: 81 Percentage: 84.800003% Division: Honors (Screen Clears)
Enter marks in 1st subject: 68 Enter marks in 2nd subject: 45 Enter marks in 3rd subject: 56 Enter marks in 4th subject: 48 Enter marks in 5th subject: 34 Percentage: 50.200001% Division: Second Division
4. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int number,rev=0,d; printf("Enter number: "); scanf("%d",&number); while(number>0) { d=number%10; rev=d+rev*10; number/=10; } while(rev>0) { d=rev%10; rev/=10; switch(d) { 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; default: printf("Wrong choice"); } } getch(); }
Enter number: 972 Nine Seven Two (Screen Clears) Enter number: 842 Eight Four Two
5. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,j,k,l; printf("Combinations are: "); for(i=1;i<=500;i++) { for(j=1;j<=500;j++) { for(k=1;k<=500;k++) { for(l=1;l<=500;l++) { if(i<j && j<k && k<l && i+j+k==l) { printf("%d %d %d %d ",i,j,k,l); } } } } } getch(); }
6. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a,b,c,d,i=0,k=0,l=0,base; int A[30],ar[30],arr[30]; printf("\nEnter any Decimal Number: "); scanf("%d",&a); d=c=a; printf("\n Enter Base: "); scanf("%d",&base); if(base==2) { while(a>0) { b=a%2; A[i]=b; ++i; a=a/2; } printf("\nBinary Conversion: "); for(int j=i-1; j>=0; --j) printf("%d",A[j]); } if(base==8) { while(c>0) { b=c%8; ar[k]=b; ++k; c=c/8; } printf("\nOctal Conversion: "); for(j=k-1; j>=0; --j) printf("%d",ar[j]); } if(base==16) { while(d>0) { b=d%16; arr[l]=b; ++l;
d=d/16; } printf("\nHexadecimal Conversion: "); for(j=l-1; j>=0; --j) { switch(arr[j]) { case 10: printf("%c",65); break; case 11: printf("%c",66); break; case 12: printf("%c",67); break; case 13: printf("%c",68); break; case 14: printf("%c",69); break; case 15: printf("%c",70); break; default: printf("%d",arr[j]); } } getch(); }
Enter any Decimal Number: 64 Enter base: 2 Binary Conversion: 1000000 (Screen Clears) Enter any Decimal Number: 64 Enter base: 8 Octal Conversion: 100 (Screen Clears) Enter any Decimal Number: 64 Enter base: 16 Hexadecimal Conversion: 40
7. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); char i; printf("Enter character: "); scanf("%c",&i); if(i>=65&&i<=90) printf("Capital letter "); if(i>=97&&i<=122) printf("Small letter "); if(i>=48&&i<=57) printf("Digit "); if((i>=0&&i<=47)||(i>=58&&i<=64)||(i>=91&&i<=96)||(i >=123&&i<=127)) printf("Special Symbol "); getch(); }
Enter character: r Small letter (Screen Clears) Enter character: B Capital letter (screen Clears) Enter character: ? Special Symbol
8. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int f=1,num,i; printf("Enter number: "); scanf("%d",&num); for(i=1;i<=num;i++) { f=f*i; } printf("Factorial is: "); printf("%d",f); getch(); }
(Screen Clears)
9. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a,b,i,res=1; printf("Enter 1st number: "); scanf("%d",&a); printf("Enter 2nd number: "); scanf("%d",&b); for(i=0;i<b;i++) { res=res*a; } printf("Value is: "); printf("%d",res); getch(); }
(Screen Clears)
10.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i=0; while(i<256) { printf("%d %c \t",i,i); i++; } getch(); }
1 2 3 4 5 6 7 8 9 10 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 0
11.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,sum,d,j; printf("Armstrong numbers are: "); for(i=1;i<=500;i++) { j=i; sum=0; while(j>0) { d=j%10; sum=sum+(d*d*d); j/=10; } if(sum==i) { printf("%d ",i); } } getch(); }
12.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a[100],i=0,j,cntp=0,cntn=0,cntz=0; char ans; do { printf("\nEnter number: "); scanf("%d",&a[i]); i++; printf("\nDo you want to continue (y/n): "); scanf("%s",&ans); }while(ans=='y'); for(j=0;j<i;j++) { if(a[j]>0) cntp++; if(a[j]<0) cntn++; if(a[j]==0) cntz++; } printf("\nNo. of postivie nos: "); printf("%d\n",cntp); printf("\nNo. of negative nos: "); printf("%d\n",cntn); printf("\nNo. of zeros: "); printf("%d\n",cntz); getch(); }
Enter number: 10 Do you want to continue (y/n): y Enter number: -90 Do you want to continue (y/n): y Enter number: 0 Do you want to continue (y/n): y Enter number: 78 Do you want to continue (y/n): y Enter number: -39 Do you want to continue (y/n): n No. of postivie nos: 2 No. of negative nos: 2 No. of zeros: 1
13.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a,b,c,d,i=0,k=0,l=0; int A[30],ar[30],arr[30]; printf("\nEnter any Decimal Number: "); scanf("%d",&a); d=c=a; while(a>0) { b=a%2; A[i]=b; ++i; a=a/2; } printf("\nBinary Conversion: "); for(int j=i-1; j>=0; --j) printf("%d",A[j]); while(c>0) { b=c%8; ar[k]=b; ++k; c=c/8; } printf("\n\nOctal Conversion: "); for(j=k-1; j>=0; --j) printf("%d",ar[j]); while(d>0) { b=d%16; arr[l]=b; ++l; d=d/16; } printf("\n\nHexadecimal Conversion: "); for(j=l-1; j>=0; --j) { switch(arr[j]) { case 10: printf("%c",65); break;
case 11: printf("%c",66); break; case 12: printf("%c",67); break; case 13: printf("%c",68); break; case 14: printf("%c",69); break; case 15: printf("%c",70); break; default: printf("%d",arr[j]); } } getch(); }
Enter any Decimal Number: 64 Binary Conversion: 1000000 Octal Conversion: 100 Hexadecimal Conversion: 40 (Screen Clears) Enter any Decimal Number: 32 Binary Conversion: 100000 Octal Conversion: 40 Hexadecimal Conversion: 20
14.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,j,k; printf("All combinations are: "); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { for(k=1;k<=3;k++) { if(i!=j && j!=k && k!=i) printf("%d %d %d\t",i,j,k); } } } getch(); }
15. #include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,j; printf("Prime numbers are: "); for(i=1;i<=300;i++) { for(j=2;j<=i;j++) { if(i%j==0) { break; } } if(j==i) printf("%d ",i); } getch(); }
Prime numbers are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293
16.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,num,m; printf("Enter number: "); scanf("%d",&num); printf("Multiplication table: "); for(i=1;i<=10;i++) { m=num*i; printf("%d ",m); } getch(); }
Enter number: 4 Multiplication table: 4 8 12 16 20 24 28 32 36 40 (Screen Clears) Enter number: 10 Multiplication table: 10 20 30 40 50 60 70 80 90 100
17.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int number,rev=0,d,temp; printf("Enter number: "); scanf("%d",&number); temp=number; while(number>0) { d=number%10; rev=d+rev*10; number/=10; } if(temp==rev) printf("Palindrome "); else printf("Not a Palindrome "); getch(); }
Enter number: 121 Palindrome (Screen Clears) Enter number: 123 Not a Palindrome
18.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a=1,b=2,i,c; printf("Fibonacci series: "); printf("%d %d ",a,b); for(i=3;i<=100;i++) { c=a+b; if(c<=100) { printf("%d ",c); a=b; b=c; } } getch(); }
Fibonacci series: 1 2 3 5 8 13 21 34 55 89
19.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int i,sum=0,cnt=0; printf("Numbers are: "); for(i=0;i<=100;i++) { if(i%6==0 && i%4!=0) { sum=sum+i; cnt++; printf("%d ",i); } } printf("\nNo. of numbers divisible are: "); printf("%d ",cnt); printf("\nSum is: "); printf("%d",sum); getch(); }
20.
#include <stdio.h> #include <conio.h> #include<math.h> main() // Made By Bhumika Bhateja { clrscr(); int a,b,c,d; float x1,x2; printf("\nEnter the value of a: "); scanf("%d",&a); printf("\nEnter the value of b: "); scanf("%d",&b); printf("\nEnter the value of c: "); scanf("%d",&c); d=b*b-4*a*c; if(d==0) { printf("\nBoth roots are equal"); x1=-b/(2*a); x2=x1; printf("\n\nFirst Root x1= %f",x1); printf("\n\nSecond Root x2= %f",x2); } if(d>0) { printf("\nBoth roots are real and different"); x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("\n\nFirst Root x1= %f",x1); printf("\n\nSecond Root x2= %f",x2); } else printf("\nNo Solution"); getch(); }
Enter the value of a: 1 Enter the value of b: 3 Enter the value of c: 2 Both roots are real and different First Root x1= -1.000000 Second Root x2= -2.000000 (Screen Clears) Enter the value of a: 1 Enter the value of b: 1 Enter the value of c: 1 No Solution
21.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int number,rev=0,d,sum=0,sume=0,sumo=0; printf("Enter number: "); scanf("%d",&number); while(number>0) { d=number%10; sum=sum+d; if(d%2==0) sume=sume+d; else sumo=sumo+d; rev=d+rev*10; number/=10; } printf("Reversed Number is: "); printf("%d",rev); printf("\nSum of digits is: "); printf("%d",sum); printf("\nSum of even digits is: "); printf("%d",sume); printf("\nSum of odd digits is: "); printf("%d",sumo); getch(); }
Enter number: 4326 Reversed Number is: 6234 Sum of digits is: 15 Sum of even digits is: 12 Sum of odd digits is: 3
22.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int custno,powercons; float amount; printf("Enter customer number: "); scanf("%d",&custno); printf("Enter power consumed: "); scanf("%d",&powercons); if(powercons<=200) amount=powercons*0.50; if(powercons>=201 && powercons<=400) amount=100+((powercons-200)*0.65); if(powercons>=401 && powercons<=600) amount=230+((powercons-400)*0.8); if(powercons>=601) amount=390+(powercons-600); printf("Amount to be paid is: Rs. "); printf("%f",amount); getch(); }
Enter customer number: 201 Enter power consumed: 250 Amount to be paid is: Rs. 132.500000 (Screen Clears) Enter customer number: 104 Enter power consumed: 560 Amount to be paid is: Rs. 358.000000
23.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a[10],i,sume=0,sumo=0; for(i=0;i<10;i++) { printf("\nEnter number: "); scanf("%d",&a[i]); if(a[i]%2==0) sume=sume+a[i]; else sumo=sumo+a[i]; } printf("\nSum of Even Numabers: "); printf("%d",sume); printf("\n\nSum of Odd Numbers: "); printf("%d",sumo); getch(); }
Enter number: 1 Enter number: 2 Enter number: 3 Enter number: 4 Enter number: 5 Enter number: 6 Enter number: 7 Enter number: 8 Enter number: 9 Enter number: 10 Sum of Even Numabers: 30 Sum of Odd Numbers: 25
24.
#include <stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int a, b, x, y, t, hcf; printf("Enter 1st number: "); scanf("%d",&x); printf("Enter 2nd number: "); scanf("%d",&y); a = x; b = y; while (b != 0) { t = b; b = a % b; a = t; } hcf = a; printf("HCF of %d and %d = %d\n", x, y, hcf); getch(); }
Enter 1st number: 10 Enter 2nd number: 5 HCF of 10 and 5 = 5 (screen Clears) Enter 1st number: 12 Enter 2nd number: 36 HCF of 12 and 36 = 12
25.
#include<stdio.h> #include<conio.h> main() // Made By Bhumika Bhateja { clrscr(); int n,i,f,j; float sum=1; printf("Enter the value of n: "); scanf("%d",&n); for(i=1;i<=n;i++) { f=1; for(j=i;j>0;j--) f=f*j; sum=sum+(1.0/f); } printf("Sum is: %f",sum); getch(); }
Enter the value of n: 5 Sum is: 2.716667 (Screen Clears) Enter the value of n: 3 Sum is: 2.666667