Key CP 2 Prog C
Key CP 2 Prog C
(a)
main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
}
output
garbage-value 200
(b)
main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
}
output
200 300
(c)
main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}
output
10 20
(d)
main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
output
3
5
(e)
main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
output
main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
output
0 50 0
(h)
main( )
{
int i = 65 ;
char j = 'A' ;
if ( i == j )
printf ( C is WOW ) ;
else
printf( "C is a headache" ) ;
}
output
C is WOW
(i)
main( )
{
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "\na = %d b = %d c = %d", a, b, c ) ;
}
output
a = 15 b = 15 c = 0
(j)
main( )
{
int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
output
1 20 1
Chapter 2: [B] Point out the errors, if any, in the following programs:
(a)
main( )
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "\na and b are equal" ) ;
}
No syntax error however logical error can occur due to missing relational
operator == inside if condition.
(b)
main( )
{
int j = 10, k = 12 ;
if ( k >= j )
{
{
k=j;
j=k;
}
}
}
No Error
(c)
main( )
{
if ( 'X' < 'x' )
printf ( "\nascii value of X is smaller than that of x" ) ;
}
No Error
(d)
main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
(e)
main( )
{
int x = 10 ;
if x >= 2
printf ( "\n%d", x ) ;
}
(f)
main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "\nCarpathians" ) ;
}
(g)
main( )
{
int x = 30 , y = 40 ;
if ( x == y )
printf( "x is equal to y" ) ;
elseif ( x > y )
printf( "x is greater than y" ) ;
elseif ( x < y )
printf( "x is less than y" ) ;
}
(h)
main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
(i)
main( )
{
int a, b ;
scanf ( "%d %d",a, b ) ;
if ( a > b ) ;
printf ( "This is a game" ) ;
else
printf ( "You have to play it" ) ;
}
(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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int cp,sp,rslt;
clrscr();
if(cp>sp) {
rslt=cp-sp;
printf("\nSeller has incurred LOSS of %d rupees.\n",rslt);
}
if(cp<sp)
{
rslt=sp-cp;
printf("\nSeller has made PROFIT of %d rupees.\n",rslt);
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(b) Any integer is input through the keyboard. Write a program to find out
whether it is an odd number or even number.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int i;
clrscr();
if(i%2==
printf("\nThe number is EVEN.\n");
else
printf("\nThe number is ODD.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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)
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int yr;
clrscr();
if(yr%4==0)
printf("\nThe year is a LEAP YEAR.\n");
else
printf("\nThe Year is NOT A LEAP YEAR.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
main() {
int month=1,year,a,day=1;
clrscr();
printf("Enter year: ");
scanf("%d",&year);
a=(14-month)/12;
year=year-a;
month=month+12*a-2;
day=(day+year+(year/4)-(year/100)+(year/400)+((31*month)/12)) % 7;
if(day==0)
printf("Sunday");
else if(day==1)
printf("Monday");
else if(day==2)
printf("Tuesday");
else if(day==3)
printf("Wednesday");
else if(day==4)
printf("Thursday");
else if(day==5)
printf("Friday");
else if(day==6)
printf("Saturday");
getch();
return 0;
}
------------------------------------------------------------------------------------------------------------
Solution:
#include<stdio.h>
#include<conio.h>
main() {
long i,d1,d2,d3,d4,d5,a,b,c,d,n_num;
clrscr();
d1=i/10000;
a=i%10000;
d2=a/1000;
b=a%1000;
d3=b/100;
c=b%100;
d4=c/10;
d=c%10;
d5=d/1;
n_num=(d5*10000)+(d4*1000)+(d3*100)+(d2*10)+(d1*1);
if(n_num==i){
printf("\nboth numbers are SAME.\n");
}
else {
printf("\nboth number are NOT SAME.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a
program to determine the youngest of the three.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int ram,shyam,ajay;
clrscr();
if(ram<shyam)
{
if(ram<ajay) {
printf("\nRam is the YOUNGEST.\n");
}
}
if(shyam<ram)
{
if(shyam<ajay) {
printf("\nShyam is the YOUNGEST.\n");
}
}
if(ajay<ram) {
if(ajay<shyam) {
printf("\nAjay is the YOUNGEST.\n");
}
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int a1,a2,a3;
clrscr();
if(a1+a2+a3==180) {
printf("\nThe triangle is VALID.\n");
}
else {
printf("\nThe triangle is NOT VALID.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(h) Find the absolute value of a number entered through the keyboard.
Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main() {
int i,av;
clrscr();
av=abs(i);
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int l,b,area,peri;
clrscr();
area=l*b;
peri=2*(l+b);
if(area>peri) {
printf("\nThe Area of Rectangle is GREATER then it's perimeter.\n");
}
else {
printf("\nThe area of Rectangle is NOT GREATER then it's perimeter.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int x1,y1,x2,y2,x3,y3,curve1,curve2;
clrscr();
curve1=(x2-x1)/(y2-y1);
curve2=(x3-x2)/(y3-y2);
if(curve1==curve2)
printf("\nAll three points fall on one straight line.\n");
else
printf("\nThese three points do not fall on one straight line.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(k) Given the coordinates (x, y) of a center of a circle and its 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)
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main() {
int x,y,x_,y_,r,point;
clrscr();
point=pow(x_,2)+pow(y_,2)+pow(x,2)+pow(y,2)-(2*x_*x)-(2*y_*y)-pow(r,2);
if(point<0)
printf("\nThe point lies inside the circle.\n");
if(point>0)
printf("\nThe point lies outside the circle.\n");
if(point==0)
printf("\nThe point lies on the circle.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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).
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int x,y;
clrscr();
if(x==0&&y!=0) {
printf("\nThe point lies on Y AXIS.\n");
}
else if(x!=0&&y==0) {
printf("\nThe point lies on X AXIS.\n");
}
else if(x==0&&y==0) {
printf("\nThe point lies at the origin.\n");
}
else {
printf("\nThe point doesn't lie on any axis or at origin.\n");
}
getch();
return 0;
___________________________________________________________________
Exercise [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 ||.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int yr;
clrscr();
if(yr%4==0 || !((yr%4)>0) )
printf("\nThe is a LEAP YEAR.\n");
else
printf("\nThis is NOT A LEAP YEAR.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
The following table shows the range of ASCII values for various characters.
Characters
AZ 65 - 90
az 97 - 122
09 48 - 57
special symbols 0 - 47, 58 - 64, 91 - 96, 123 - 127
Solution:
#include<stdio.h>
#include<conio.h>
main() {
char ch;
clrscr();
if(ch>=65&&ch<=90)
printf("\nThe character you entered is a CAPITAL LETTER.\n");
if(ch>=97&&ch<=122)
printf("\nThe character you entered is a SMALL LETTER.\n");
if(ch>=48&&ch<=57)
printf("\nThe character you entered is a DIGIT.\n");
if(ch>=0&&ch<=47 || ch>=58&&ch<=64 || ch>=91&&ch<=96 ||
ch>=123&&ch<=127)
printf("\nThe character you entered is a SPECIAL SYMBOL.\n");
getch();
return 0;
------------------------------------------------------------------------------------------------------------
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int age,premium,p_a,hlt,rsdnc,sex;
long amount;
clrscr();
printf("Please tell the HEALTH of the person\n(1 for excellent or 2 for poor)
:\n");
scanf("%d",&hlt);
else {
printf("\nThis person cannot be insured.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int hdn,tnsl,hardness=50,tensile_strength=5600;
float c,carbon_content=0.7;
clrscr();
else
printf("\nThe steel has been Graded 5.\n");
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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int days;
clrscr();
if(days<=5)
printf("\nYou have been fined for 50 paise.\n");
if(days>=6&&days<=10)
printf("\nYou have been fined for 1 rupee.\n");
if(days>10&&days<30)
printf("\nYou have been fined for 5 rupee.\n");
if(days>=30)
printf("\nYour membership has been cancelled.\n");
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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int s1,s2,s3,ls,ss1,ss2;
clrscr();
if(s1>s2&&s1>s3){
ls=s1;
ss1=s2;
ss2=s3;
}
if(s2>s1&&s2>s3) {
ls=s2;
ss1=s1;
ss2=s3;
}
if(s3>s1&&s3>s2) {
ls=s3;
ss1=s1;
ss2=s2;
}
if((ss1+ss2)>ls) {
printf("\nThe Triangle is VALID.\n");
}
else {
printf("The triangle is NOT VALID.\n");
}
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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int s1,s2,s3,largest,s_1,s_2;
clrscr();
if(s1>s2&&s1>s3){
largest=s1;
s_1=s2;
s2=s3;
}
if(s2>s1&&s2>s3){
largest=s2;
s_1=s1;
s_2=s3;
}
if(s3>s1&&s3>s2){
largest=s3;
s_1=s1;
s_2=s2;
}
if(pow(largest,2)==pow(s_1,2)+pow(s_2,2))
printf("The Triangle is RIGHT ANGLED.\n");
getch();
return 0;
}
------------------------------------------------------------------------------------------------------------
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int hrs;
clrscr();
if(hrs>5)
printf("\nThe worker should be asked to leave the company.\n");
else
printf("The worker is HIGHLY EFFICIENT.\n");
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 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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int a,b;
clrscr();
else {
printf("Student has failed.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
(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.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int stock,credit,order;
clrscr();
if(credit!=0&&order<=stock) {
printf("\nSupply\n");
}
else if(credit!=0&&order>stock) {
printf("\nAvailable items will be supplied.\n");
}
else {
printf("\nNo supply.\n");
}
getch();
return 0;
}
______________________________________________________________________
Exercise [J]
(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.
Solution:
#include<stdio.h>
#include<conio.h>
char main() {
char ch;
clrscr();
else {
printf("\n[A] This character is NOT A SMALL CASE alphabet.\n");
}
if(ch>=0&&ch<=47||ch>=58&&ch<=64||ch>=91&&ch<=96||ch>=123&&ch<=127)
{
printf("\n[B] This character is a SPECIAL SYMBOL.\n");
}
else{
printf("\n[B] This character is NOT A SPECIAL SYMBOL.\n");
}
getch();
return 0;
------------------------------------------------------------------------------------------------------------
Solution:
#include<stdio.h>
#include<conio.h>
main () {
int yr;
clrscr();
if(yr%4!=0)
printf("\nThis is NOT A LEAP YEAR.\n");
else
printf("\nThis is a LEAP YEAR.\n");
getch();
return 0;
}
------------------------------------------------------------------------------------------------------------
(c) Write a program to find the greatest of the three numbers entered through
the keyboard using conditional operators.
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int n1,n2,n3;
clrscr();
else {
printf("\nwrong input!\n");
}
getch();
return 0;