Docsity

programs on if else and switch, Exercises of Computer Programming

Partial preview of the text

Download programs on if else and switch and more Exercises Computer Programming in PDF only on Docsity!

#import <stdio.h> int main() { printf("Hello world !! "); return 0; }

Lab 1

Program 1 : Write a program to print Hello world

Output :

Code:

#import <stdio.h> int main() { int a,b,c; printf("Sum Calculator \n\n"); printf("Enter First number: \n"); scanf("%d",&a); printf("Enter Second number: \n"); scanf("%d",&b); c=a+b; printf("The sum is :%d",c); return 0; }

Lab 1

Program 2: Write a program to Calculate the sum of two numbers entered by user.

Output :

Code :

#import <stdio.h> #import <math.h> int main() { printf("COMPOUND INTEREST CALCULATOR \n \n"); float p,r,t,ci,a; printf("Enter principal : \n"); scanf("%f",&p); printf("Enter rate (in %% p.a.): \n"); scanf("%f",&r); printf("Enter time (in years) : \n"); scanf("%f",&t); a=p*pow((1+r/100),t); ci=a-p; printf("Compound interest : %f \n", ci); printf("Amount: %f \n", a); return 0; }

Lab 1

Program 4 : Write a program to calculate compound interest.

Output :

Code :

#include <stdio.h> int main() { int a; printf("Enter a number :\n"); scanf("%d",&a); if(a%2==0) { printf("%d is divisble by 2 \n",a); } if(a%3==0) { printf("%d is divisble by 3 \n",a); } if(a%5==0) { printf("%d is divisble by 5 \n",a); } if(a%7==0) { printf("%d is divisble by 7 \n",a); } if(!(a%2==0 || a%3==0 || a%5==0 || a%7==0 )) { printf("%d is not divisble by 2,3,5 or 7",a); } return 0; }

Program 3 : Write a program to check whether a number is divisible by 2,3,5 or 7.

Output :

Code :

#include <stdio.h> int main() { int q1,q2,q3,q4; float p1,p2,p3,p4,t1,t2,t3,t4,t; char item1[20],item2[20],item3[20],item4[20]; printf("Enter name of item 1 : \n"); scanf("%s", item1); printf("Enter Quantitiy of %s : \n",item1); scanf("%d",&q1); printf("Enter Price of %s: \n",item1); scanf("%f",&p1); t1=q1p1; printf("\nEnter name of item 2 : \n"); scanf("%s", item2); printf("Enter Quantitiy of %s : \n",item2); scanf("%d",&q2); printf("Enter Price of %s : \n",item2); scanf("%f",&p2); t2=q2p2; printf("\nEnter name of item 3 : \n"); scanf("%s", item3); printf("Enter Quantitiy of %s : \n",item3); scanf("%d",&q3); printf("Enter Price of %s: \n",item3); scanf("%f",&p3); t3=q3p3; printf("\nEnter name of item 4 : \n"); scanf("%s", item4); printf("Enter Quantitiy of %s : \n",item4); scanf("%d",&q4); printf("Enter Price of %s: \n",item4); scanf("%f",&p4); t4=q4p4; t=t1+t2+t3+t4; printf("\nItem Quantity \t Price(per unit) \t Total \n"); printf("%s\t%d\t\t%.2f\t\t\t%.2f \n",item1,q1,p1,t1); printf("%s\t%d\t\t%.2f\t\t\t%.2f \n",item2,q2,p2,t2); printf("%s\t%d\t\t%.2f\t\t\t%.2f \n",item3,q3,p3,t3); printf("%s\t%d\t\t%.2f\t\t\t%.2f \n\n",item4,q4,p4,t4); printf("TOTAL \t\t\t\t\t\t %.2f",t); return 0; }

Program 4 : Write a program to take input for quantity and price of 4 items and print the

Total bill in proper format

Code :

printf("Enter Marks of English : \n"); scanf("%d",&d); printf("Enter Marks of Maths : \n"); scanf("%d",&e); y=((a+b+c+d+e)/500.0)*100; printf("Percentage is : %.2f\n",y); if(y>=60) { printf("FIRST DIVISION !"); } else if(y>=50 & y<60) { printf("SECOND DIVISION !"); } else if(y>=40 & y<50) { printf("PASS !"); } else { printf("FAIL"); } return 0; }

Output :

#include <stdio.h> int main() { int bs; float hra,da,total; printf("HRA and DA calculator\n\n"); printf("Enter Basic Salaray (Rs.) :\n"); scanf("%d",&bs); if(bs<=1500) { hra=(10.0/100)bs; da=(90.0/100)bs; total=hra+da+bs; printf("HRA : Rs.%.2f \n",hra); printf("DA : Rs.%.2f \n\n",da); printf("Total salary : Rs.%.2f \n",total); } else { hra=(50.0/100)bs; da=(98.0/100)bs; total=hra+da+bs; printf("HRA : Rs.%.2f \n",hra); printf("DA : Rs.%.2f \n\n",da); printf("Total salary : Rs.%.2f \n",total); } return 0; }

Program 6 : In a company, an employee is paid as under:

a. If basic salary is <=1500, then HRA = 10% and DA=90% of Basic salary.

b. If basic salary is >1500, then HRA = 500 and DA=98% of Basic salary.

If basic salary is input through keyboard. Write a program to print total salary

(Basic+HRA+DA).

Output :

Code :