Chapter 2
If , If – Else, Else – If
2.1 Write a program to check whether a number is greater than 10 or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“ Enter a number”);
scanf("%d",&a);
if (a>10)
{
printf(“The number is greater than 10”);
}
if (a<10)
{
printf("The Number is less than 10");
}
if (a==10)
{
printf(" The Number is equal to 10");
}
getch();
}
2.2 Write a program to check whether two numbers are greater than to each other.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“ Enter 1st number”);
scanf("%d",&a);
printf(“ Enter 2nd number”);
scanf("%d",&b);
if (a>b)
{
printf(“%d is greater than %d”,a,b);
}
if (a<b)
{
printf("%d is less than %d",a,b);
}
if (a==b)
{
printf(" %d Number is equal to %d",a,b);
}
getch();
}
2.3 Write a program to check which greatest number is?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x=-32767;
clrscr();
printf(“ Enter three numbers”);
scanf("%d%d%d",&a,&b,&c);
if (a>x)
{
x=a;
}
if (b>x)
{
x=b;
}
if (c>x)
{
x=c;
}
printf("The greatest among three is %d",x);
getch();
}
2.4 Write a program to check which lowest number is?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x=32767;
clrscr();
printf(“ Enter three numbers”);
scanf("%d%d%d",&a,&b,&c);
if (a<x)
{
x=a;
}
if (b<x)
{
x=b;
}
if (c<x)
{
x=c;
}
printf("The lowest among three is %d",x);
getch();
}
2.5 Write a program to check the password using if only.
#include<stdio.h>
#include<conio.h>
void main()
{
int p;
clrscr();
printf(“ Enter the password numbers”);
scanf("%d ",&p);
if (p==12345)
{
printf(“Password Accepted”);
}
if (p!=12345)
{
printf(“ Password Denied”);
}
getch();
}
2.5 Write a program to check the password using if only and it will add two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,a,b,c;
clrscr();
printf(“ Enter the password numbers”);
scanf("%d ",&p);
if (p==12345)
{
printf(“Password Accepted”);
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("The Result = %d",c);
}
if (p!=12345)
{
printf(“ Password Denied”);
}
getch();
}
2.6 Write a program to check the password with if – else and if it is matched then add two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,a,b,c;
clrscr();
printf(“ Enter the password numbers”);
scanf("%d ",&p);
if (p==12345)
{
printf(“Password Accepted”);
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("The Result = %d",c);
}
else
{
printf(“ Password Denied”);
}
getch();
}
2.7. Write a program to check a triangle is perfect or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“ Enter the three angles of a triangle”);
scanf(“%d%d%d”,&a,&b,&c);
if (a+b+c==180)
{
printf(“ The Triangle is Perfect”);
}
else
printf(“The Triangle is not Perfect”);
getch();
}
2.8. Write a program to check a rectangle whether it is perfect rectangle or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf(“ Enter the four triangles);
scanf(“%d%d%d%d”,& a,&b,&c,&d);
if (a+b+c+d==360)
printf(“ The Triangle is Perfect”);
else
printf(“ The Triangle is not perfect”);
getch();
}
Q2.9. Write a program to check is there any supply needs as for the respect of demand.
#include<stdio.h>
#include<conio.h>
void main()
{
int demand, supply,need;
clrscr();
printf(“Enter the Demand”);
scanf(“%d”,&demand);
printf(“Enter the Supply”);
scanf(“%d”,&supply);
if (demand>=supply)
{
need=demand-supply;
printf(“We Need to Purchase %d”,need);
}
else
{
printf(“ We have no need to purchase’);
}
getch();
}
Q2.10. Write a program to check a year whether it is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf(“ Enter the year”);
scanf(“%d”,&year);
if(year%4==0)
printf(“ This is a leap year);
else
printf(“ This is not a leap year”);
getch();
}
Q2.11. Write a program to determine a number is odd or even.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“ Enter the Number”);
scanf(“%d”,&a);
if (a%2==0)
{
printf(“ The Number is Odd”);
}
else
{
printf(“ The Number is even”);
getch();
}
Q2.12. Create a program to make a result sheet with division.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int eng=0,ben=0,math=0,geo=0,hist=0,phy=0,lsc=0;
float total= 0,avg=0;
clrscr();
printf( “ Enter your Name”);
scanf(“%s”,name);
printf(“ Enter the marks for English”)
scanf(“%d”,& eng);
printf(“ Enter the marks for Bengali”)
scanf(“%d”,& ben);
printf( “ Enter the marks for Math”);
scanf(“%d”,& math);
printf(“ Enter the marks for Geography”);
scanf(“%d”,& geo);
printf(“ Enter the marks for History”)
scanf(“%d”,& hist);
printf(“ Enter the marks for Physical Science ”);
scanf(“%d”,&phy);
printf(“ Enter the marks for Life Science”);
scanf(“%d”,&lsc);
total=eng+ben+math+geo+hist+phy+lsc
avg=total/7
printf(“ Total Marks = %.2f”, total);
printf(“ Average = %.2f”, avg);
if (avg>=60)
printf(“ 1st Division”);
else
if( avg>=45)
printf(“ 2nd Division”);
else
if (avg>=30)
printf( “ P Division”);
else
printf(“Fail”);
getch();
}
Q2.13 A certain grade of steel is graded according to the following conditions:
1) Hardness must be greater than 50.
2) Carbon content must be less than 0.7
3) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three condition are met
Grade is 9 if conditions(1) and (2) are met.
Grade is 8 if conditisons(2) and (3) are met.
Grade is 7 if conditisons(1) and (3) are met.
Grade is 6 if only one conditison is met.
Grade is 5 if none of the conditions are met.
#include<stdio.h>
#include<conio.h>
void main()
{
float carbon;
int hard,tensile;
clrscr();
printf("enter the hardness of steel");
scanf("%d",&hard);
printf("enter the carbon of steel");
scanf("%f",&carbon);
printf("enter the tensile");
scanf("%d",&tensile);
if(((hard>=50)&&(carbon<=0.7)&&(tensile>=5600)))
{
printf("Grade is 10");
printf("\npress any key");
}
getch();
if(((hard>=50)&&(carbon<=0.7)&&(tensile<=5600)))
{
printf("grade is 9");
printf("\n press any key");
exit();
}
if(((hard<=50)&&(carbon<=0.7)&&(tensile>=5600)))
{
printf("Grade is 8");
printf("\n press any key");
exit();
}
if(((hard>=50)&&(carbon>=0.7)&&(tensile>=5600)))
{
printf("Grade is 7");
printf("\n press any key");
exit();
}
if(((hard>=50)&&(carbon>=0.7)&&(tensile<=5600)))
{
printf("Grade is 6");
printf("\n perss any key");
exit();
}
if(((hard==40)&&(carbon==0.5)&&(tensile==5600)))
{
printf("Grade is 5");
printf("\n press any key");
exit();
}
getch();
}
Q 2.14 WAP to add two distance
#include<stdio.h>
#include<conio.h>
void main()
{
int f1,f2,i1,i2,f3,i3;
clrscr();
printf("Enter 1st distance=");
scanf("%d%d",&f1,&i1);
printf("Enter 2nd distance=");
scanf("%d%d",&f2,&i2);
f3=f1+f2;
i3=i1+i2;
if(i3>=12)
{
i3=i3%12;
f3=f3+(i3/12);
}
printf("\nFeet=%d\nInch=%d",f3,i3);
getch();
}
Q2.15 How to make a program that can check your voter ability.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
print(" enter your age");
scant();
if (age>=18)
{
printf("You can vote");
}
else
{
printf("You can note vote");
}
getch();
}
Q2.16 write a program to cast vote for certain age.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter your age");
scanf("%d",&age);
if ((age>=18) && (age<=30))
{
printf("You can vote");
}
else
{
printf("You cannot vote");
}
getch();
}
Q2.17 Write a program to determine below age groups of people of 18 and above age group of 40 people can cast their vote.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter your age");
scanf("%d",&age);
if ((age<=18) || ((age>=40))
{
printf("You can vote");
}
else
{
printf("You cannot vote");
}
getch();
}
Q2.18 Write a program to determine two age groups of people can cast their vote.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter your age");
scanf("%d",&age);
if (((age>=18) && (age<=30)) || ((age>=40) && (age<=50)))
{
printf("You can vote");
}
else
{
printf("You cannot vote");
}
getch();
}
Q2.19 write a program to determine whether discount of 10% will be allowed if purchase price will exceed Rs 5000.
#include<stdio.h>
#include<conio.h>
void main()
{
char pn[20];
float rate,qty,disp,pr;
clrscr();
printf("Enter your product name");
scanf("%s",pn);
printf("Enter your rate");
scanf("%f",&rate);
printf("Enter the Quantity");
scanf("%f",&qty);
pr=rate*qty;
if (pr>=5000)
{
disp=pr-pr*.1;
printf("price after 10% discount = Rs",disp);
}
else
{
printf("No discount allowed as price= Rs. ",pr);
}
getch();
}
Q2.20 write a program to levied Re1 for each day if submit date exceeds more than 10th of the current month.
#include<stdio.h>
#include<conio.h>
void main()
{
char n[20];
int date,sub,fine,total;
clrscr();
printf("Enter your name");
scanf("%s",pn);
fflush(stdin);
printf("Enter Subscrition Amount");
scanf("%d",&sub);
printf("Enter the Date");
scanf("%d",&date);
if (date>=10)
{
fine=date-10;
total=fine+sub;
printf("Total Amount=",total);
}
else
{
printf("No fine Please Submit the Subscription Rs. ",sub);
}
getch();
}
Q2.21 write a program to levied Rs 5 for each day if the submission date exceeds 20th of the month Re1 f6r each day if submit date exceeds more than 10th of the current month.
#include<stdio.h>
#include<conio.h>
void main()
{
char n[20];
int date,sub,fine,total;
clrscr();
printf("Enter your name");
scanf("%s",pn);
fflush(stdin);
printf("Enter Subscrition Amount");
scanf("%d",&sub);
printf("Enter the Date");
scanf("%d",&date);
if (pr>=5000)
{
disp=pr-pr*.1;
printf("price after 10% discount = Rs",disp);
}
else
{
printf("No discount allowed as price= Rs. ",pr);
}
getch();
}
Q2.22 Write a C program to check given year is a leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("\n enter the year in yyyy format: ");
scanf("%d",&year);
if(year%100==0)
{
if(year%400==0)
{
printf("\n The given year %d is leap year.",year);
}
else
{
printf("\n The given year is not a leap year.");
}
}
else if(year%4==0)
{
printf("\n The given year %d is a leap year.",year);
}
else
{
printf("\n The given year is not a leap year.");
}
getch();
}
Q2.23 Write a program to find the grade using else if()
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the average of marks: ");
scanf("%d",&a);
if(a>=90)
{
printf("\nGrade:A+");
}
else if(a>=80)
{
printf("\nGrade:A");
}
else if(a>=70)
{
printf("\nGrade:B+");
}
else if(a>=60)
{
printf("\nGrade:B");
}
else if(a>=50)
{
printf("\nGrade:C");
}
else if(a>=40)
{
printf("\nGrade:D");
}
else
{
printf("\nFail");
}
getch();
}
Q2.24 WAP to find the dates in a month
#include<stdio.h>
#include<conio.h>
void main()
{
int i,m;
clrscr();
printf("Enter the month no=");
scanf("%d",&m);
if(m==1)
{
printf("\nMonth name is=january");
printf("\nDays are=31");
}
else if(m==2)
{
printf("\nMonth name is=Februry");
printf("\nDays are=28");
}
else if(m==3)
{
printf("\nMonth name is=March");
printf("\nDays are=31");
}
else if(m==4)
{
printf("\nMonth name is=April");
printf("\nDays are=30");
}
else if(m==5)
{
printf("\nMonth name is=May");
printf("\nDays are=31");
}
else if(m==6)
{
printf("\nMonth name is=June");
printf("\nDays are=30");
}
else if(m==7)
{
printf("\nMonth name is=July");
printf("\nDays are=31");
}
else if(m==8)
{
printf("\nMonth name is=Augest");
printf("\nDays are=31");
}
else if(m==9)
{
printf("\nMonth name is=September");
printf("\nDays are=30");
}
else if(m==10)
{
printf("\nMonth name is=October");
printf("\nDays are=31");
}
else if(m==11)
{
printf("\nMonth name is=November");
printf("\nDays are=30");
}
else if(m==12)
{
printf("\nMonth name is=December");
printf("\nDays are=31");
}
else
{
printf("\nInvalid month");
}
getch();
}
Q.2.25 Write a program to check an alphabet is vowel, consonant or semi vowel.
#include<stdio.h>
#include<conio.h>
void main()
{
char n;
int a;
clrscr();
printf("Enter the Alphabet");
scanf("%c",&n);
a=n;
if((a>=65)&&(a<=92))
{
if((a==65)||(a==69)||(a==73)||(a==79)||(a==85))
{
printf("\nCapital Vowel");
}
else if((a==87)||(a==89))
{
printf("\nCapital Semi Vowel");
}
else
{
printf("\nCapital Consonant");
}
}
else
{
if((a==97)||(a==101)||(a==105)||(a==111)||(a==117))
{
printf("\nSmall Vowel");
}
else if((a==119)||(a==121))
{
printf("\nSmall Semi Vowel");
}
else
{
printf("\nSmall Consonant");
}
}
getch();
}
No comments:
Post a Comment