PPS-UNIT-2

SWN | PPS-Practical

Indus University

CSE Section

PPS-Practical

Unit-II:Using conditional statements

1.Write a program to find the maximum of (i) two integer values and (ii) three integer values.

#include <stdio.h>
#include <conio.h>
void main(){
//for two integer
int a,b,c;
printf("enter a and b:") ;
scanf("%d %d",&a,&b);
a>b ? printf("%d is a maximum\n",a) : printf("%d is maximum\n",b);
//for three integer
printf("enter a,b and c:") ;
scanf("%d %d %d",&a,&b,&c);
if(a>b){
if(a>c){
printf("%d is maximum",a);
}
else{
printf("%d is maximum",c);
}
}
else if(b>c){
printf("%d is maximum",b);
}
else{
printf("%d is maximum",c);
}
getch();
}

enter a and b:43 67
67 is maximum
enter a,b and c:12 0 -65
12 is maximum

2.Write a program to check whether the given character is a vowel or not.

#include <stdio.h>
#include <conio.h>
void main(){
char ch;
printf("enter a character:") ;
scanf("%c",&ch);
switch (ch){
case 'a':
printf("%c is vowel",ch);
break;
case 'i':
printf("%c is vowel",ch);
break;
case 'o':
printf("%c is vowel",ch);
break;
case 'u':
printf("%c is vowel",ch);
break;
case 'e':
printf("%c is vowel",ch);
break;
default:
printf("%c is not a vowel",ch);
getch();
}
}

enter a character:a
a is vowel

3.Write a program that reads a number from 1 to 7 and accordingly it should display MONDAY to SUNDAY (if- else if).

#include <stdio.h>
#include <conio.h>
void main()
{
int a;
printf("enter the number:");
scanf("%d",&a);
if(a==1){
printf("Monday");
}
else if(a==2){
printf("Tuesday");
}
else if(a==3){
printf("Wednesday");
}
else if(a==4){
printf("Thursday");
}
else if(a==5){
printf("Friday");
}
else if(a==6){
printf("Saturday");
}
else if(a==7) {
printf("Sunday");
}
else{
printf("enter number from 1 to 7 and try again");
}
getch();
}

enter the number:6
Saturday

4.Write a menu driven program to perform the arithmetic operations.

#include <stdio.h>
#include <conio.h>
void main(){
float a,b;
int operation;
printf("Enter Value of a:");
scanf("%f",&a);
printf("Enter value of b:");
scanf("%f",&b);
printf("Which operation you want to perform.Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for division:");
scanf("%d",&operation);
switch(operation){
case 1:
printf("addition=%f",a+b);
break;
case 2:
printf("subtraction=%f",a-b);
break;
case 3:
printf("multiplication=%f",a*b);
break;
case 4:
printf("division=%f",a/b);
break;
}
getch();
}

Enter Value of a:45
Enter value of b:34
Which operation you want to perform.Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for division:4
div=1.323529

5.Write a program to print the number of days in a given month using switch statements. The program requires month number (between 1 to 12) as an input and then displays number of days in that month.

#include <stdio.h>
#include <conio.h>
void main(){
int op;
printf("Enter Month number from 1 to 12:");
scanf("%d",&op);
switch(op){
case 1:
printf("January = 31 days");
break;
case 2:
printf("February = 28/29 days");
break;
case 3:
printf("March = 31 days");
break;
case 4:
printf("April = 30 days");
break;
case 5:
printf("May = 31 days");
break;
case 6:
printf("June = 30 days");
break;
case 7:
printf("July = 31 days");
break;
case 8:
printf("August = 31 days");
break;
case 9:
printf("September = 30 days");
break;
case 10:
printf("October = 31 days");
break;
case 11:
printf("November = 30 days");
break;
case 12:
printf("December = 31 days");
break;
default:
printf("enter number between 1-12!!!");
}
getch();
}

Enter Month number from 1 to 12:4
April = 30 days

6.Write a program to check whether a given value is even or odd.

#include <stdio.h>
#include <conio.h>
void main(){
int a;
printf("Enter number:");
scanf("%d",&a);
if(a%2==0){
printf("%d is even",a);
}
else{
printf("%d is odd",a);
}
getch();
}

Enter number:54543
54543 is odd

7.Write a program to calculate the total salary of an employee.Total salary =basic + da +hra +ta.da = 50% of basic
Basic hra ta
<6000 400 100
6001>=&<10000 1400 300
>=10000 2400 700

#include <stdio.h>
#include <conio.h>
void main(){
float total,hra,da,ta,basic;
printf("Enter the basic:");
scanf("%f",&basic);
da=(basic*50)/100;
if(basic<=6000){
hra=400;
ta=100;
} else if(basic>=6001 && basic<=10000){
hra=1400;
ta=300;
}
else{
hra=2400;
ta=700;
}
total=basic+ta+hra+da;
printf("Total salary is %f",total);
getch();
}

Enter the Basic:7000
total salary is= 12200.000000