Friday 24 June 2016

IF ELSE programming


1. WAP in c to print "hello" if given number is positive otherwise print "bye"

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("enter any number:");
scanf("%d",&x);
if(x>0)
{
printf("\nhello");
}
else
{
printf("\nbye");
}
getch();
}

output:












2. WAP in c to accept 5 subject marks from user calculate and print sum and percentage obtained also check for pass or fail.

#include<stdio.h>
#include<conio.h>
void main()
{
float s1,s2,s3,s4,s5,t,p;
clrscr();
printf("enter five subject marks:\n");
scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
t=s1+s2+s3+s4+s5;
printf("marks obtained=%f\n",t);
p=t*100/500;
printf("percentage=%f\n",p);
if(p>=33)
{
printf("pass");
}
else
{
printf("fail");
}
getch();
}

output:










3. WAP in c to check whether a given number is even or odd.

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

output:








4. WAP in c to calculate total salary according to given condition
/*
if basic salary >=10000
then TA =40% of basic salary
DA =60% of basic salary
HRA=50% of basic salary
if basic salary <10000
then TA =70% of basic salary
DA =80% of basic salary
HRA=5000
total salary=basic salary+TA+DA+HRA
*/

#include<stdio.h>
#include<conio.h>
void main()
{
float bs,ta,da,hra,ts;
clrscr();
printf("Enter basic salary:");
scanf("%f",&bs);
if(bs>=10000)
{
ta=bs*40/100;
da=bs*60/100;
hra=bs*50/100;
}
else
{
ta=bs*70/100;
da=bs*80/100;
hra=5000;
}
ts=bs+ta+da+hra;
printf("\nTA=%f",ta);
printf("\nDA=%f",da);
printf("\nHRA=%f",hra);
printf("\ntotal salary=%f",ts);
getch();
}

output:







5. WAP in c to print greater number between two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
if(x>y)
{
printf("%d is greater",x);
}
else
{
printf("%d is greater",y);
}
getch();
}

output:







6.WAP in c to find greatest number between three numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers:\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("\n\t%d is greater",x);
}
else
{
printf("\n\t%d is greater",z);
}
}
else
{
if(y>z)
{
printf("\n\t%d is greater",y);
}
else
{
printf("\n\t%d is greater",z);
}
}
getch();
}


output:








7. WAP in c to check eligbility criteria for voting in college using nested if else. The condition is that you must be student of college and your age should be between 18 to 25 (both including).

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char ch;
clrscr();
printf("Enter your age:");
scanf("%d",&age);
printf("\nAre you student of college(Y or N):");
ch=getche();
if(age>=18)
{
if(age<=25)
{
if(ch=='Y')
{
printf("\nEligible to vote");
}
else
{
if(ch=='y')
{
printf("\nEligible to vote");
}
else
{
printf("\nnot Eligible to vote");
}
}
}
else
{
printf("\nnot Eligible to vote");
}
}
else
{
printf("\nnot Eligible to vote");
}
getch();
}

output:







8. WAP in c to check eligibility criteria for voting in college using logical operators. The condition is that you must be student of college and your age should be between 18 to 22.

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char ch;
clrscr();
printf("Enter your age:\n");
scanf("%d",&age);
printf("\nAre you student of college(Y or N):\n");
ch=getche();
if(age>=18 && age<=25 && (ch=='Y'|| ch=='y'))
{
printf("\n Eligible to vote");
}
else
{
printf("\n not Eligible to vote");
}
getch();
}

output:








9. WAP in c to accept a character from user and print whether it is vowel or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any character:");
ch=getche();
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
    ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' )
{
printf("\nVowel");
}
else
{
printf("\nnot a Vowel");
}
getch();
}

output:






10. WAP in c to accept 5 subject marks from user calculate and print sum and percentage obtained also print grade according to condition -
00<=p<33 print "F grade"
33<=p<50 print "D grade"
50<=p<60 print "C grade"
60<=p<75 print "B grade"
75<=p<90 print "A grade"
90<=p<=100 print "A+ grade"
also print "PASS" or "FAIL". 


#include<stdio.h>
#include<conio.h>
void main()
{
float s1,s2,s3,s4,s5,t,p;
clrscr();
printf("enter five subject marks:\n");
scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
t=s1+s2+s3+s4+s5;
printf("marks obtained=%f\n",t);
p=t*100/500;
printf("percentage=%f\n",p);
if(p>=33)
{
printf("pass");
}

if(p>=90)
{
printf("\nA+ grade");
}
else
{
if(p>=75)
{
printf("\nA grade");
}
else
{
if(p>=60)
{
printf("B grade");
}
else
{
if(p>=50)
{
printf("\nC grade");
}
else
{
if(p>=40)
{
printf("\nD grade");
}
else
{
if(p>=33)
{
printf("\nE grade");
}
else
{
printf("\nFail \nF grade");
}
}
}
}
}
}
getch();
}

output:









11. WAP in c to accept distance from user and calculate total fare according to the condition-
for 0<d<=3 - rs 50
for 3<d<=10 - rs 15/km
for 10<d<=25 - rs 13/km
for 25<d<=30 - rs 10/km
above 30 km - service not available


#include<stdio.h>
#include<conio.h>
void main()
{
int dis=0,fare=0;
clrscr();
printf("Enter distace to be travelled:");
scanf("%d",&dis);
if(dis<=30)
{
if(dis>25)
{
fare=50+7*15+15*13+(dis-25)*10;
}
else
{
if(dis>10)
{
fare=50+7*15+(dis-10)*13;
}
else
{
if(dis>3)
{
fare=50+(dis-3)*15;
}
else
{
if(dis>0)
{
fare=50;
}
else
{
printf("\nService not available");
}
}
}
}
}
else
{
printf("\nService not available");
}
printf("\nTotal fare= Rs %d",fare);
getch();
}


output:





12. WAP in c to accept number of units consumed by user and calculate total charge accoding to the condition-
for 0<u<=100 - rs 500
for 100<u<=300 - rs 8/unit
for 300<u<=500 - rs 10/km
for above 500 - rs 15/km

#include<stdio.h>
#include<conio.h>
void main()
{
long unsigned int u,bill;
clrscr();
printf("Enter units consumed:");
scanf("%lu",&u);
if(u>500)
{
bill=500+200*8+200*10+(u-500)*15;
}
else
{
if(u>300)
{
bill=500+200*8+(u-300)*10;
}
else
{
if(u>100)
{
bill=500+(u-100)*8;
}
else
{
bill=500;
}
}
}
printf("\n Bill payable= Rs%lu",bill);
getch();
}


output:







13. A company promotes its employees in the following cases:
- If the employee is married.
- If the employee is unmarried, male & above 3 years of experience.
- If the employee is unmarried, female & above 2 years of experience.
In all other cases the employee is not promoted. If the marital status, sex and experience of the employee are the inputs, write a program to determine whether the employee is to be promoted or not (without using logical operators).

#include<stdio.h>
#include<conio.h>
void main()
{
int exp;
char gen,ms;
clrscr();
printf("\nEnter marital status(married-m or unmarried-u):");
scanf("%c",&ms);
if(ms=='m')
{
printf("\nPromoted");
}
else
{
printf("\nEnter your gender (m or f):");
gen=getche();
printf("\nEnter your experience:");
scanf("%d",&exp);
if(gen=='m')
{
if(exp>=3)
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}
}
else
{
if(exp>=2)
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}
}
}
getch();
}

Output :









14. A company promotes its employees in the following cases:
  • If the employee is married.
  • If the employee is unmarried, male & above 3 years of experience.
  • If the employee is unmarried, female & above 2 years of experience.

In all other cases the employee is not promoted. If the marital status, sex and experience of the employee are the inputs, write a program to determine whether the employee is to be promoted or not (using logical operators).


#include<stdio.h>
#include<conio.h>
void main()
{
int exp;
char gen,ms;
clrscr();
printf("\nEnter marital status:(married-m or unmarried-u):");
scanf("%c",&ms);
printf("\nEnter your gender (m or f):");
gen=getche();
printf("\nEnter your experience:");
scanf("%d",&exp);
if(ms=='m'||(ms=='u'&&gen=='m'&&exp>=3)|| (ms=='u'&&gen=='f'&&exp>=2) )
{
printf("\nPromoted");
}
else
{
printf("\nNot Promoted");
}

getch();
}


Output :









15. WAP in c to check whether a given character is in lower case or not 

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter any character:");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
{
printf("\ngiven character is in lower case");
}
else
{
printf("\ngiven character is not in lower case");
}
getch()
}


output:










16. Program to calculate salary of an employee according to given condition

Gender        Service years      qualification        Salary
----------      ----------------       ---------------        --------
Male            >= 10                   Post-Graduate      15000
                     >= 10                  Graduate              10000
                     <=10                   Post-Graduate      10000
                     < 10                     Graduate                7000
Female          >= 10                  Post-Graduate      12000
                      >= 10                  Graduate                9000
                      < 10                     Post-Graduate     10000
                      < 10                     Graduate               6000




#include<stdio.h>
#include<conio.h>
void main( )
{
char g ;
int y, q, s ;
clrscr();
printf("Enter Gender,Years of Service and qualifications(0=G,1=PG ):\n") ;
scanf("%c%d%d", &g, &y, &q ) ;

if ( (g == 'm'|| g == 'M') && y >= 10 && q == 1 )
s = 15000 ;
else if ( ( (g == 'm'|| g == 'M') && y >= 10 && q == 0 )
||( (g == 'm'|| g == 'M') && y < 10 && q == 1 ) )
s = 10000 ;
else if ( (g == 'm'|| g == 'M') && y < 10 && q == 0 )
s = 7000 ;
else if ( (g == 'f'|| g == 'F') && y >= 10 && q == 1 )
s = 12000 ;
else if ( (g == 'f'|| g == 'F') && y >= 10 && q == 0 )
s = 9000 ;
else if ( (g == 'f'|| g == 'F') && y < 10 && q == 1 )
s = 10000 ;
else if ( (g == 'f'|| g == 'F')&& y < 10 && q == 0 )
s = 6000 ;

printf("\nSalary of Employee = %d", s) ;
getch();
}

Output:








17. A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600

The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met

Write a program, which will require the user to give values of hardness,carbon content and tensile strength of the steel under consideration and output the grade of the steel. 


#include<stdio.h>
#include<conio.h>
void main()
{
int h,t;
float c;
clrscr();
printf("Instructions:\n(i) Hardness must be greater than 50\n(ii) Carbon content must be less than 0.7\n(iii) Tensile strength must be greater than 5600\n");
printf("Enter Hardness, cabon content and tensile strength of steel\n");
scanf("%d%f%d",&h,&c,&t);
if(h>50 && c<0.7 && t>5600)
{
printf("Grade 10");
}
else
{
if(h>50 && c<0.7)
{
printf("Grade 9");
}
else
{
if(c<0.7 && t>5600)
{
printf("Grade 8");
}
else
{
if(h>50 && t>5600)
{
printf("Grade 7");
}
else
{
if(h>50 || c<0.7 || t>5600)
{
printf("Grade 6");
}
else
{
printf("Grade 5");
}
}
}
}
}
getch();
}

Output:
















No comments:

Post a Comment

index

MENU Sample Papers  (new sample paper added for computers) C Language Mathematics IX (NCERT solution)   Computer IX (CBSE) ...