Sie sind auf Seite 1von 9

12/18/2017 Chapter 2 - Let Us C Solutions

http://letuscsolutions.weebly.com/chapter-2.html 1/21

(/)
(http://rating-widget.com/my-rating-report/star/oxygen/rating-86327220-5/)
Rate this (170 Votes)
[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.
#include<stdio.h>
#include<conio.h>
main()
{
int cp,sp,l,p; //cp=cost price,sp=selling price,l=loss,p=profit
clrscr();
printf("Enter Cost Price of an item : RS ");
scanf("%d",&cp);
printf("Enter Selling Price of an item : RS ");
scanf("%d",&sp);
if(cp>sp) // Loop for Loss
{
l=cp-sp;
printf("You have made LOSS. Your Loss is RS %d",l);
}e
lse if(sp>cp) // Loop for Profit
{
p=sp-cp;
printf("You have gain PROFIT. Your Profit is RS %d",p);
}e
lse if(sp=cp) // Loop for no Loss no Profit
{
printf("You have neither Loss nor Profit");
}g
etch();
}
(b) Any integer is input through the keyboard. Write a program to find out whether it is an
odd number or even number.
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 2/21
#include<stdio.h>
#include<conio.h>
main()
{
int num; // num=number
clrscr();
printf("Enter any integer to know weather its is Even or Odd : ");
scanf("%d",&num);
if(num%2==0)
{p
rintf("%d is Even number",num);
}e
lse
{
printf("%d is Odd number",num);
}g
etch();
}
(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>
#include<conio.h>
main()
{
int year;
clrscr();
printf("Enter any year : ");
scanf("%d",&year);
if(year%4==0)
printf("%d is a leap year.",year);
else
printf("%d is not a leap year.",year);
getch();
}
(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.
Coming Soon...
(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.
main()
{
int a,b,c,d,e,f,g,i,j;
clrscr();
printf("Enter the five digit number\n");
scanf("%d",&a);
b=a%10;
c=a/10;
d=c%10;
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 3/21
e=c/10;
f=e%10;
g=e/10;
i=g%10;
j=g/10;
printf("The reverse number is %d%d%d%d%d",b,d,f,i,j);
printf("\nThe original and reverse number is not equal");
getch();
}
(f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to
determine the youngest of the three.
main()
{
int ram, shyam, ajay;
clrscr();
printf("Enter ages of Ram, Shayam, Ajay\n");
scanf("%d %d %d", &ram, &shyam, &ajay);
if (ram<shyam)
{
if (ram<ajay)
printf("Ram is younger.");
else
printf("Ajay is younger");
}e
lse
{
if (shyam<ajay)
printf("Shayam is younger");
else
printf("Ajay is younger");
}g
etch();
}
(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>
#include<conio.h>
main()
{
float a1,a2,a3,sum; //a1=angle1,a2=angle2,a3=angle3,sum=sum of all angles
clrscr();
printf("Enter three angle of a triangle : ");
scanf("%f%f%f",&a1,&a2,&a3);
sum=a1+a2+a3;
if(sum==180)
{
printf("Triangle is Valid.");
}e
lse
{
printf("Triangle is invalid.");
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 4/21
}g
etch();
}
(h) Find the absolute value of a number entered through the keyboard.
void main( )
{
int i, j;
printf ("Enter number : ");
scanf ("%d", &i);
if(i<0)
j = -i;
else
j = i;
printf ("%d", j);
getch();
}
(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>
#include<conio.h>
main()
{
int l,b,a,p; // l=length,b=breadth,a=area,p=perimeter of rectangle
clrscr();
printf("Enter Length of a rectangle : ");
scanf("%d",&l);
printf("Enter Breadth of a rectangle : ");
scanf("%d",&b);
p=2*(l+b);
a=l*b;
if(a>p)
{ // "\n" is use for new line
printf("\nYes! Area[%d] is greater that its
perimeter[%d]",a,p);
}e
lse
{
printf("\nNo! Area[%d] is not greater that its perimeter[%d]",a,p);
}g
etch();
}
(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>
#include<conio.h>
main()
{
int x1,y1,x2,y2,x3,y3,m1,m2; // m1=slope 1,m2=slope 2
Submitted by Rahul Raina
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 5/21
clrscr();
printf("Enter coordinates of 1st point (x1,y1) : ");
scanf("%d%d",&x1,&y1);
printf("Enter coordinates of 2nd point (x2,y2) : ");
scanf("%d%d",&x2,&y2);
printf("Enter coordinates of 3rd point (x3,y3) : ");
scanf("%d%d",&x3,&y3);
m1=(y2-y1)/(x2-x1);
m2=(y3-y2)/(x3-x2);
if(m1==m2)
{
printf("The given point fall on one straight line.");
}e
lse
{
printf("The given point does not fall on one straight line.");
}g
etch();
}
(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)
Coming Soon...
(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>
#include<conio.h>
main()
{
int x,y;
clrscr();
printf("Enter coordinates of a point (x,y) = ");
scanf("%d%d",&x,&y);
if(x==0&&y!=0)
{
printf("The point lies on y axis.");
}e
lse if(y==0&&x!=0)
{
printf("The point lies on x axis.");
}e
lse if(x==0&&y==0)
{
printf("The point lies on origin.");
}g
etch();
}[
G] Attempt the following:
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 6/21
(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>
#include<conio.h>
main()
{
int year;
clrscr();
printf("Enter any year : ");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||year%400==0)
{
printf("%d is a Leap Year.");
}e
lse
{
printf("%d is not a Leap Year.");
}g
etch();
}
(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. Characters ASCII
Values for various characters.
#include<stdio.h>
#include<conio.h>
main()
{
char a;
clrscr();
printf("Enter any single letter, digit or special symbol : ");
scanf("%c",&a);
if(a>=65&&a<=90)
{
printf("You entered a CAPITAL LETTER.\n");
}if
(a>=97&&a<=122)
{
printf("You entered a SMALL LETTER.\n");
}if
(a>=48&&a<=57)
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 7/21
{
printf("You entered a DIGIT.\n");
}if
((a>=0&&a<=47)||(a>=58&&a<=64)||(a>=91&&a<=96)||(a>=123&&a<=127))
{
printf("You entered an SPECIAL SYMBOL.\n");
}g
etch();
}
(c) 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>
main()
{
float hard,carbon,tensile;
clrscr();
printf("Enter Hardness of steel : ");
scanf("%f",&hard);
printf("Enter Carbon content of steel : ");
scanf("%f",&carbon);
printf("Enter Trnsile strength of steel : ");
scanf("%f",&tensile);
if(hard>50&&carbon<0.7&&tensile>5600)
printf("\nThe grade of steel is 10");
else if(hard>50&&carbon<0.7)
printf("\nThe grade of steel is 9");
else if(carbon<0.7&&tensile>5600)
printf("\nThe grade of steel is 8");
else if(hard>50&&tensile>5600)
printf("\nThe grade of steel is 7");
else if(hard>50||carbon<0.7||tensile>5600)
printf("\nThe grade of steel is 6");
else
printf("\nThe grade of steel is 5");
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 8/21
getch();
}
(d) 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>
#include<conio.h>
main()
{
int days;
clrscr();
printf("Enter the number of days the member is late to return the book : ");
scanf("%d",&days);
if(days<=5)
printf("\nYou must pay 50 paisa fine..."); // '\n' is used for new line
else if(days>=6&&days<=10)
printf("\nYou must pay 1 rupee fine...");
else if(days>10&&days<30)
printf("\nYou must pay 5 rupees fine...");
else if(days>=30)
printf("\nYour membership is cancelled...");
getch();
}
(e) 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>
#include<conio.h>
main()
{
float s1,s2,s3; //s1=side1,s2=side2,s3=side3
clrscr();
printf("Enter three sides of triangle in ascending order:\n");
scanf("%f%f%f",&s1,&s2,&s3);
if(s1+s2>s3)
printf("\nThe triangle is valid.");
else
printf("\nThe triangle is invalid.");
getch();
}
(f) 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>
#include<conio.h>
main()
{
float a1,a2,a3; //a1=angle1,a2=angle2,a3=angle3
clrscr();
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 9/21
printf("Enter three angles of a triangle:\n");
scanf("%f%f%f",&a1,&a2,&a3);
if((a1+a2+a3)==180)
{i
f(a1==a2&&a1==a3&&a2==a3)
printf("\nThe triangle is Equilateral.");
else if(a1==a2||a1==a3||a2==a3)
printf("\nThe triangle is Isosceles.");
else if(a1==90||a2==90||a3==90)
printf("\nThe triangle is Right Angled.");
else if(a1!=a2&&a1!=a3&&a2!=a3)
printf("\nThe triangle is Scalene");
}e
lse
printf("The triangle is not valid");
getch();
}
(g) 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>
#include<conio.h>
main()
{
float h; //h=hours
clrscr();
printf("Enter the time taken by the worker (In Hours) : ");
scanf("%f",&h);
if(h>=2&&h<=3)
printf("\nWorker is highly efficient.");
else if(h>=3&&h<=4)
printf("\nWorker should improve his speed.");
else if(h>=4&&h<=5)
printf("\nWorker should take training to improve the speed.");
else if(h>=5)
printf("\nWorker has to leave the company.");
getch();
}
(h) 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.
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 10/21
(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.
Coming Soon...
[K] Attempt the following:
(a) Using conditional operators determine:
(1) Whether the character entered through the keyboard is a lower case alphabet or not.
#include<stdio.h>
#include<conio.h>
main()
{
char ch; //ch=character
clrscr();
printf("Enter any charcter to find out weather it is lower case or not : ");
scanf("%c",&ch);
(ch>=97&&ch<=122?printf("\nThe input character is lower case."):printf("\nThe input
character is not lower case."));
getch();
}
(2) Whether a character entered through the keyboard is a special symbol or not.
#include<stdio.h>
#include<conio.h>
main()
{
char ch; //ch=character
clrscr();
printf("Enter any charcter to find out weather it is special symbol or not : ");
scanf("%c",&ch);
((ch>=0&&ch<=47)||(ch>=58&&ch<=64)||(ch>=91&&ch<=96)||(ch>=123&&ch<=127)?
printf("\nThe input character is special symbol."):printf("\nThe input character is not special
symbol."));
getch();
}
(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>
#include<conio.h>
main()
{
int year;
clrscr();
printf("Enter any year : ");
scanf("%d",&year);
((year%4==0&&year%100!=0)||(year%400==0)?printf("\n%d is a leap
year.",year):printf("\n%d is not a leap year.",year));
getch();
}
12/18/2017 Chapter 2 - Let Us C Solutions
http://letuscsolutions.weebly.com/chapter-2.html 11/21
(c) Write a program to find the greatest of the three numbers entered through the keyboard
using conditional operators.
#include<stdio.h>
#include<conio.h>
main()
{
int n1,n2,n3; //n1=1st number,n2=2nd number,n3=3rd number
clrscr();
printf("Enter three numbers:\n");
scanf("%d%d%d",&n1,&n2,&n3);
(n1>n2&&n1>n3?printf("\n%d is greater.",n1):(n2>n3&&n2>n1?printf("\n%d is
greater.",n2):printf("\n%d is greater.",n3)));
getch();
}
Back to Top ▲

Das könnte Ihnen auch gefallen