Chapter: Decision Control Structure | Let us C solution with details description and tutorials | Yashwant Kanetkar |
if,if-else,Nested if-else
[C] Attempt the following:
(a) If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.
Show Solutions
Hide Solutions
Chapter: Decision Control Structure
/* C (a)If cost price and selling price of an item is input through
the keyboard, write a program to determine whether the seller
has made profit or incurred loss. Also determine how much
profit he made or loss he incurred.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int cp,sp,check;
printf("enter cost price: ");
scanf("%d",&cp);
printf("\nenter selling price:");
scanf("%d",&sp);
check=sp-cp;
if(check>0)
{
printf("\nSeller have profit");
printf("\nHe got %d profit",check);
}
else if(check<0)
{
check=cp-sp;
printf("\nSeller have Loss");
printf("\nHe got %d loss",check);
}
else
printf("You have neither loss nor profit");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return..
//u will read in function chapter
}
(b) Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.
Show Solutions
Hide Solutions
Chapter: Decision Control Structure
/*C (b) Any integer is input through the keyboard. Write a
program to find out whether it is an odd number or even
number.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int no,r;
printf("Enter a number to check odd or even:");
scanf("%d",&no);
r=no%2; // calculating remainder
if(r==0)
printf("Number is even"); //case remainder 0 no is even
else
printf("Number is odd");
getch(); //for holding screen till any key is pressed
return 0; //int main()is function so value must be return.
//u will read in function chapter
}
(c) Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.
(Hint: Use the % (modulus) operator)
Show Solutions
Hide Solutions
Chapter: Decision Control Structure
/*(c) Any year is input through the keyboard. Write a program
to determine whether the year is a leap year or not. (Hint:
Use the % (modulus) operator)*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int year;
printf("Enter year: ");
scanf("%d",&year);
if((year%4)==0&&(year%100)!=0||(year%400)==0)
printf("%d is leap year",year); // reason at Note:1
else
printf("%d is not leap year",year);
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*Note:1 year will be leap year if year is divisible by
4 as well as not divisible by 100 or year is divisible
by 400.
*/
(d) According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.
Show Solutions
Hide Solutions
/*c (d) According to the Gregorian calendar, it was Monday on
the date 01/01/1900. If any year is input through the keyboard
write a program to find out what is the day on 1st January of
this year.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int year,differ,lp_year,day_type;
long int days;
printf("Please enter the year: ");
scanf("%d",&year);
year=year-1; //we will find days before given year so
differ=year-1900;
/*as leap year is not divisible by 100.so,create 2 condition
one difference less than 100 and greater than 100*/
if(differ<100)
{
lp_year=differ/4; //caln of total no. of leap year
days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note1
day_type=days%7; //caln of day type sun, mon......
}
if(differ>=100)
{
lp_year=(differ/4)-(differ/100)+1+((year-2000)/400);//see Note2
days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note3
day_type=days%7;
}
if(day_type==0)
printf("\nSunday");
if(day_type==1)
printf("Monday");
if(day_type==2)
printf("Tuesday");
if(day_type==3)
printf("Wednesday");
if(day_type==4)
printf("Thursday");
if(day_type==5)
printf("Friday");
if(day_type==6)
printf("Saturday");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be returned.
}
(e) A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.
Show Solutions
Hide Solutions
/*c (e) A five-digit number is entered through the keyboard.
Write a program to obtain the reversed number and to
determine whether the original and reversed numbers are
equal or not.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int digit1,digit2,digit3,digit4,digit5,number,rev_num,ini_num;
printf ("Enter 5 numbers to reversed an check for equality:");
scanf("%d", &number);
ini_num=number; // for holding initial no for future use
digit1=number%10; //1st digit digit of reveres no
number=number/10;
digit2=number%10; //2nd digit digit of reveres no
number=number/10;
digit3=number%10; //3rd digit digit of reveres no
number=number/10;
digit4=number%10; //4th digit digit of reveres no
number=number/10;
digit5=number%10; //5th digit digit of reveres no
rev_num=digit1*10000+digit2*1000+digit3*100+digit4*10+digit5;
printf("\nReversed no is %d",rev_num);
if (rev_num==ini_num)
{
printf("\nEntered number and reversed number are equal\n");
}
else
{
printf("\nEntered number and reversed number are not equal\n");
}
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*Note1:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type
Note2:
-(leap year come in every 4 year so) (differ/4) for leap year
-(leap year isn't divisible by 100 so we subtract (differ/100)
from counting as leap year
(f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.
Show Solutions
Hide Solutions
/*(f) If the ages of Ram, Shyam and Ajay are input through the
keyboard, write a program to determine the youngest of the
three.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int agea,ageb,agec;
printf ("Enter age of Ram, Shyam and Ajay: ");
scanf("%d%d%d",&agea,&ageb,&agec);
if(agea<ageb&&agea<agec)
{
printf("\nRam is youngest ");
}
else if(ageb<agea&&ageb<agec)
{
printf("\nShyam is youngest");
}
else if(agec<agea&&agec<ageb)
{
printf("\nAjay is youngest");
}
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
(g) Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.
Show Solutions
Hide Solutions
/*c (g) Write a program to check whether a triangle is valid or
not, when the three angles of the triangle are entered through
the keyboard. A triangle is valid if the sum of all the three
angles is equal to 180 degrees.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int angle1,angle2,angle3,sum;
printf ("Enter three angles of triangle: ");
scanf("%d%d%d",&angle1,&angle2,&angle3);
sum=angle1+angle2+angle3;
if(sum==180)
{
printf("Triangle is valid");
}
else
printf("Triangle is invalid");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*
sum of interior angle of triangle is 180 ...
*/
(h) Find the absolute value of a number entered through the keyboard.
Show Solutions
Hide Solutions
/*c (h) Find the absolute value of a number entered through the
keyboard.*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int number;
printf("Enter any no to find absolute value:");
scanf("%d",&number);
if(number<0)
number=(-1)*number;
printf("Absolute value is: %d",number);
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
(i) Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.
Show Solutions
Hide Solutions
/*c (i) Given the length and breadth of a rectangle, write a
program to find whether the area of the rectangle is greater
than its perimeter. For example, the area of the rectangle
with length = 5 and breadth = 4 is greater than its perimeter.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int length,breadth,perimeter,area;
printf("enter length and breadth: ");
scanf("%d%d",&length,&breadth);
perimeter=2*(length+breadth);
area=length*breadth;
if(area>perimeter)
printf("\narea is greater than perimeter");
else if(area<perimeter)
printf("area is lesser than perimeter");
else
printf("area and perimeter are equal");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
(j) Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.
Show Solutions
Hide Solutions
/*(j) Given three points (x1, y1), (x2, y2) and (x3, y3),
write a program to check if all the three points fall on
one straight line.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int x1,y1,x2,y2,x3,y3,x4,y4,slope1,slope2;
printf("Enter 1st co-ordinate (x1,y1): ");
scanf("%d%d",&x1,&y1);
printf("\nEnter 2nd co-ordinate (x2,y2): ");
scanf("%d%d",&x2,&y2);
printf("\nEnter 3rd co-ordinate (x3,y3): ");
scanf("%d%d",&x3,&y3);
slope1=(y2-y1)/(x2-x1);
slope2=(y3-y2)/(x3-x2);
if(slope1==slope2)
printf("\nThree points fall on the same line");
else
printf("\nThree points doesn't fall on the same line");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
(k) Given the coordinates (x, y) of a center of a circle and it’s radius, write a program which will determine whether a point lies inside the circle, on the circle or outside the circle.
(Hint: Use sqrt( ) and pow( ) functions)
Show Solutions
Hide Solutions
/*c (k) Given the coordinates (x, y) of a center of a circle
and it’s radius, write a program which will determine
whether a point lies inside the circle, on the circle
or outside the circle. (Hint: Use sqrt( ) and pow( )
functions)
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int x=2,y=3,r=3,x1,y1,d1,d2;
float d;
printf("Enter co-ordinate (x1,y1): ");
scanf("%d%d",&x1,&y1);
d1=x1-x;
d2=y1-y;
d=sqrt(pow(d1,2)+pow(d2,2)); //pow() function returns d1*d1
//sqrt gives square root of value inside ()
if(d<r)
printf("The point lies inside the circle");
else if(d==r)
printf("The point lies on the circle");
else
printf("The point lies outside the circle");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
(l) Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0)
Show Solutions
Hide Solutions
/*(l) Given a point (x, y), write a program to find out
if it lies on the x-axis, y-axis or at the origin, viz.
(0,0)
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int x,y;
printf("Enter co-ordinate(x1,y1)to check position: ");
scanf("%d%d",&x,&y);
if(x==0&&y==0)
printf("\nPoint lies on the origin ");
else if(x==0&&y!=0)
printf("\nPoint lies on the y-axis ");
else
printf("\nPoint lies on the x-axis ");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*
*/
[F] ([G] in 4th edition ) Attempt the following:
(a) Any year is entered through the keyboard, write a program to determine whether the year is leap or not. Use the logical operators && and ||.
Show Solutions
Hide Solutions
/*f (a) Any year is entered through the keyboard, write a program
to determine whether the year is leap or not. Use the logical
operators && and ||.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int year;
printf("Enter year: ");
scanf("%d",&year);
if((year%4)==0&&(year%100)!=0||(year%400)==0)
printf("%d is leap year",year); // reason at Note:1
else
printf("%d is not leap year",year);
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*Note:1 year will be leap year if year is divisible by
4 as well as not divisible by 100 or year is divisible
by 400.
*/
(b) 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.
Show Solutions
Hide Solutions
/*f (b) 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.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
char ch; //note 1
printf("Enter any character ");
scanf("%c",&ch);
if(ch>=65&&ch<=90)
printf("Character entered is Capital");
else if(ch>=97&&ch<=122)
printf("Character entered is small");
else if(ch>=48&&ch<=57)
printf("character is digit");
else
printf("Character is special symbols");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*
note 1: every character has ascii value..
Ascii value is the value for representing character
*/
(c) An Insurance company follows following rules to calculate premium.
(1) If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs.
(2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(3) If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male
then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
(4) In all other cases the person is not insured.
Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured.
Show Solutions
Hide Solutions
/*f (c) An Insurance company follows following rules to
calculate premium.(1) If a person’s health is excellent
and the person is between 25 and 35 years of age and
lives in a city and is a male then the premium is Rs.
4 per thousand and his policy amount cannot exceed Rs.
2 lakhs.(2) If a person satisfies all the above
conditions except that the sex is female then the
premium is Rs. 3 per thousand and her policy amount
cannot exceed Rs. 1 lakh.(3) If a person’s health is poor
and the person is between 25 and 35 years of age and lives
in a village and is a male then the premium is Rs. 6 per
thousand and his policy cannot exceed Rs. 10,000.(4) In all
other cases the person is not insured.Write a program to
output whether the person should be insured or not,
his/her premium rate and maximum amount for which he/she
can be insured.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
char health,sex,area;
int age;
printf("Enter health condn(e/p),sex(m/f),area(c/v)&age\n");
scanf("%c %c %c %d",&health,&sex,&area,&age);
if(health=='e'&&sex=='m'&&area=='c'&&age>=25&&age<=35)
{
printf("\nInsured\n");
printf("\nPremium rate = Rs. 4 per 1,000\n");
printf("\nmaximum policy amount = Rs. 2,00,000");
}
else
if(health=='e'&&sex=='f'&&area=='c'&&age>=25&&age<=35)
{
printf("\nInsured");
printf("\nPremium Rate = Rs. 3 per 1000");
printf("\nMaximum policy amount = Rs. 1,00,000");
}
else
if(health=='p'&&sex=='m'&&area=='v'&&age>=25&&age<=35)
{
printf("\nInsured");
printf("\nPremium Rate = Rs. 6 per 1,000");
printf("\nMaximum policy amount = Rs. 10,000");
}
else
printf("\nYou cannot be insured\n");
getch();
return 0;
}
(d) 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.
Show Solutions
Hide Solutions
/*f (d) 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 5600The grades
are as follows:Grade is 10 if all three conditions are
met Grade is 9 if conditions (i) and (ii) are metGrade
is 8 if conditions (ii) and (iii) are metGrade is 7 if
conditions (i) and (iii) are metGrade is 6 if only one
condition is metGrade is 5 if none of the conditions
are metWrite 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> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
float hardness,carbon,tensile;
int grade;
printf("Enter hardness,carbon content & tensile strength:");
scanf("%f %f %f",&hardness,&carbon,&tensile);
if(hardness>0&&carbon>=0&&tensile>0)
{
if(!(hardness>50&&carbon<0.7&&tensile>5600))
grade=5;
if(hardness>50||carbon<0.7||tensile>5600)
grade=6;
if(hardness>50&&tensile>5600)
grade=7;
if(carbon<0.7&&tensile>5600)
grade=8;
if(hardness>50&&carbon<0.7)
grade=9;
if(hardness>50&&carbon<0.7&&tensile>5600)
grade=10;
printf("\nGrade = %d",grade);
}
else
printf("\nYour entry is invalid");
getch();
return 0;
}
/*
*/
(e) A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.
Show Solutions
Hide Solutions
/*f (e)A library charges a fine for every book returned late.
For first 5 days the fine is 50 paise,for 6-10 days fine is
one rupee and above 10 days fine is 5 rupees. If you return
the book after 30 days your membership will be cancelled.
Write a program to accept the number of days the member is
late to return the book and display the fine or the
appropriate message.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int days;
float fine;
printf("Number of days late: ");
scanf("%d",&days);
if(days<=30)
{
if(days<=5)
fine=0.5;
else if(days>5&&days<=10)
fine=1.0;
else if(days>10&&days<=30)
fine=5.0;
printf("you have to pay fine of Rs %f",fine);
}
else
printf("Your membership has been canceled");
getch();
return 0;
}
(f)If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides.
Show Solutions
Hide Solutions
/*f (f) If the three sides of a triangle are entered through
the keyboard, write a program to check whether the
triangle is valid or not. The triangle is valid if the
sum of two sides is greater than the largest of the three
sides.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int side1,side2,side3;
printf("Enter 3 sides of triangle: ");
scanf("%d%d%d",&side1,&side2,&side3);
if(side1>side2&&side1>side3)//case for side1 greater
{
if(side1<(side2+side3))//long side is side1
{
printf("\ntriangle is valid ");
}
else
printf("\n triangle is invalid");
}
if(side2>side1&&side2>side3) //case for side2 greater
{
if(side2<(side1+side3))
{
printf("\ntriangle is valid ");
}
else
printf("\n triangle is invalid");
}
if(side3>side1&&side3>side2) //case for side3 greater
{
if(side3<(side1+side2))
{
printf("\ntriangle is valid ");
}
else
printf("\n triangle is invalid");
}
getch();
return 0;
}
(g)If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.
Show Solutions
Hide Solutions
/*f (g)If the three sides of a triangle are entered through
the keyboard, write a program to check whether the
triangle is isosceles, equilateral, scalene or right
angled triangle.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int side1,side2,side3,h;
printf("Enter 3 sides of triangle: ");
scanf("%d%d%d",&side1,&side2,&side3);
if(side1==side2&&side1==side3)//for equilateral
{
printf("\nEquilateral triangle ");
}
//for isosceles
else if(side1==side2||side2==side3||side1==side3)
{
printf("\ntriangle is isoscelses ");
}
//for scalene
else if(side1!=side2&&side2!=side3&&side1!=side3)
printf("\n triangle is scalene");
if(side1>side2&&side1>side3) //case for side1 greater
{
h=sqrt(side2*side2+side3*side3);
//note1: see about pythagoream theorem
if(side1==h)//Pythagoream Theorem condition check
printf("\ntriangle is right angled triangle");
else
printf("\ntriangle is not right angled triangle");
}
else if(side2>side1&&side2>side3) //case for side2 greater
{
h=sqrt(side1*side1+side3*side3);
if(side2==h)//Pythagoream Theorem condition check
printf("\ntriangle is right angled triangle");
else
printf("\ntriangle is not right angled triangle");
}
else if(side3>side1&&side3>side2) //case for side3 greater
{
h=sqrt(side1*side1+side2*side2);
if(side3==h)//Pythagoream Theorem condition check
printf("\ntriangle is right angled triangle");
else
printf("\ntriangle is not right angled triangle");
}
getch();
return 0;
}
/*
note1: Pythagoream theorem:
square of long side=sum of square of two other side.
which is the condition for right angled triangle.
solution description:
if square of longest side is equal to sum of square
of two other side.so we check for longest side and check
for pythagoream theorem.
*/
(h) In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.
Show Solutions
Hide Solutions
/*(h) In a company, worker efficiency is determined on
the basis of the time required for a worker to complete
a particular job. If the time taken by the worker is
between 2 – 3 hours, then the worker is said to be
highly efficient. If the time required by the worker
is between 3 – 4 hours, then the worker is ordered
to improve speed. If the time taken is between 4 – 5
hours, the worker is given training to improve his
speed, and if the time taken by the worker is more than
5 hours, then the worker has to leave the company. If
the time taken by the worker is input through the
keyboard, find the efficiency of the worker
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
float time;
printf("Enter time taken to complete work: ");
scanf("%f",&time);
if(time>=2&&time<=3)
printf("\nWorker is highly efficient");
else if(time>3&&time<=4)
printf("\nWorker is ordered to improve speed");
else if(time>4&&time<=5)
printf("\nWorker has to given a training");
if(time>5)
printf("Worker has to leave the company");
getch();
return 0;
}
(i) A university has the following rules for a student to qualify for a degree with A as the main subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets less than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed.
Write a program to receive marks in A and B and Output whether the student has passed, failed or is allowed to reappear in B.
Show Solutions
Hide Solutions
/*(i) A university has the following rules for a student
to qualify for a degree with A as the main subject and B
as the subsidiary subject:(a) He should get 55 percent or
more in A and 45 percent or more in B.(b) If he gets than
55 percent in A he should get 55 percent or more in B.
However, he should get at least 45 percent in A.(c) If he
gets less than 45 percent in B and 65 percent or more in
A he is allowed to reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed.Write a
program to receive marks in A and B and Output whether the
student has passed, failed or is allowed to reappear in B.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int pera,perb;
printf("Enter percent in A and B: ");
scanf("%d%d",&pera,&perb);
if(pera>=55&&perb>=45)
printf("\n Student is passed");
else if(pera>=45&&pera<55&&perb>=55)
printf("\n Student is passed");
else if(perb<45&&pera>=65)
printf("Student is allowed to reappear in an exam");
else
printf("Student is failed");
getch();
return 0;
}
(j) The policy followed by a company to process customer orders is given by the following rules:
(a) If a customer order is less than or equal to that in stock and has credit is OK, supply has requirement.
(b) If has credit is not OK do not supply. Send him intimation.
(c) If has credit is Ok but the item in stock is less than has order, supply what is in stock. Intimate to him data the balance will be shipped.
Write a C program to implement the company policy
Show Solutions
Hide Solutions
/*f (j)The policy followed by a company to process customer
orders is given by the following rules:(a) If a customer
order is less than or equal to that in stock and has credit
is OK, supply has requirement.(b) If has credit is not OK
do not supply. Send him intimation.(c) If has credit is Ok
but the item in stock is less than has order, supply what
is in stock. Intimate to him data the balance will be
shipped.Write a C program to implement the company policy
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int stock=12,your_stk,word;
char credit;
printf("Enter the orders: ");
scanf("%d",&your_stk);
fflush(stdin);//to remove enter as buffer see note1
printf("\n Has credit or not (y/n)");
scanf("%c",&credit);
if(your_stk<=stock&&credit=='y')
printf("\norder will be supplied");
else if(your_stk<=stock&&credit=='n')
printf("\norder can't be supplied.credit required");
else if(your_stk>stock&&credit=='y')
printf("\n12 will be supplied remaining will be later");
getch();
return 0;
}
/*
note1:
when u type order and press enter key then enter key
will be in buffer. so %c may hold enter key.so
fflush(stdin) remove enter key as our character.
*/
[J]( [k]in 4th edition) Attempt the following:
(a)Using conditional operators determine:
(1) Whether the character entered through the keyboard is a lower case alphabet or not.
(2) Whether a character entered through the keyboard is a special symbol or not.
Show Solutions
Hide Solutions
/*(j)(a)Using conditional operators determine:
(1) Whether the character entered through the keyboard
is a lower case alphabet or not.(2) Whether a character
entered through the keyboard is a special symbol or not.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
char c;
printf("Enter any character: ");
scanf("%c",&c); //note 1 about use of \
((c>=97)&&(c<=122))?printf("LowerCase"):printf("NotLowercase");
(((c>=0)&&(c<=47))||((c>=58)&&(c<=64))||((c>=91)&&(c<=96)||\
((c>=123)&&(c<=127)))?(printf("\nSpecial Symbol")):\
(printf("\nNot a Special Symbol")));
getch();
return 0;
}
/*
\ is used to join 2 line.If \ is not use syntax error will be.
*/
(b) Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.
Show Solutions
Hide Solutions
/*j (b) Write a program using conditional operators to
determine whether a year entered through the keyboard
is a leap year or not.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int year;
printf("Enter year: ");
scanf("%d",&year); // note 1 about use of \ for joing line. '\' is used at last
(year%4==0&&year%100!=0||year%400==0)?\
(printf("leap year")):(printf("not leap year"));
getch();
return 0;
}
/*
\ is used to join 2 line.If \ is not use syntax error will be.
*/
(c) Write a program to find the greatest of the three numbers entered through the keyboard using conditional operators.
Show Solutions
Hide Solutions
/*j (c)Write a program to find the greatest of the three
no.s entered through the keyboard using conditional operator
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int a,b,c;
printf("enter 3 numbers (a,b,c)to find greatest no: ");
scanf("%d%d%d",&a,&b,&c);
// '\' is used to join line
(a>b&&a>c)?printf("a is greatest"):((b>a&&b>c)?\
printf("b is greatest"):printf("c is greatest"));
getch();
return 0;
}
/*
\ is used to join 2 line.If \ is not use syntax error will be.
*/
Comments
Post a Comment