EXERCISE 01 Complete Solved LET US C by Yashwant Kanetkar
Let Us C by Yashwant Kanetkar
LET US C
EXERCISE
01
[A] Which of the following
are invalid variable names and why?
·
//Space is also
not allowed in “C” Variables
·
//There
should be no other symbol then underscore
group. basic-hra, #MEAN, team’svictory, queue. Plot # 3,
population in 2006, over time,
[B] Point out the errors, if
any, in the following C statements:
(a)- A float value is being used in the
integer variable.
(b)&(c)- To initialized value in the character variable double
quotation are used.
(d)- Variable name should be in the left
side while initializing it’s value.
(e)- Brackets are not logically closed.
(f)-
Variable names are not properly declared;
(g)- This
is a fine statement.
(h)-
Double “**” is being used.
(i)- ^
operation is being used in the arithmetical expression.
(j)- This
is a fine statement.
(k)- This
is a fine statement.
(l)- This
is a fine statement.
(m)- To initialized value in the character variable double
quotation are used.
[D] Fill the following table
for the expressions given below and then evaluate the result. A sample entry
has been filled in the table for expression (a).
S/No. |
Operators |
Left |
Right |
Remarks |
a)- |
/ |
10 |
5 or 5 / 2 / 1 |
1 |
b)- |
/,*,+ |
3/2 |
+5*4/3 |
7 |
c)- |
+ |
3 |
+4 |
7 |
a)- g = 10 / 5 /2 / 1 ; b)- b = 3 / 2 + 5 * 4 / 3 ;
c)- a = b = c = 3 + 4 ;
[C] Evaluate the following expressions and show their hierarchy.
(a) g = big / 2 +
big * 4 / big - big + abc / 3 ;
(abc = 2.5, big = 2,
assume g to be a float)
2 / 2 + 2 * 4 / 2 - 2 + 2.5 / 3;
1 + 2 * 4 / 2 - 2 +
2.5 / 3;
1 + 8 / 2 - 2 + 2.5
/ 3;
1 + 4 - 2 + 2.5 /
3;
1 + 4 - 2 +
0;
5 - 2 +
0;
3+ 0
3
g =
3
(b) on = ink
* act / 2 + 3 / 2 * act + 2 + tig ;
(ink = 4, act = 1, tig = 3.2,
assume on to be an int)
4 * 1 / 2 + 3 / 2 * 1 + 2 + 3.2
4 / 2 + 3 / 2 * 1 + 2 + 3.2
2 + 3 / 2 * 1
+ 2 + 3.2
2 + 1 * 1 + 2
+ 3.2
2 + 1 + 2
+ 3.2
3 + 2 + 3.2
5 +
3.2
8
on = 8
(c)
s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;
(qui
= 4, add = 2, god = 2, assume s to be an int)
4 * 2 / 4 - 6 / 2 + 2 / 3 * 6 / 2
8 / 4 - 6 / 2 + 2 / 3 * 6 / 2
2 - 6 / 2 + 2 / 3 * 6 / 2
2 - 3 + 2 / 3 * 6 /
2
2 - 3 + 0 * 6 / 2
2 - 3 + 0 / 2
2 - 3 + 0
-1 + 0
-1
s= -1
(d)
s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
(a = 4, g = 3, assume s to be an int)
1 / 3 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3
0 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3
0 / 4 - 6 / 2 + 2 / 3 * 6 / 3
0 - 6 / 2 + 2 / 3 *
6 / 3
0 - 3 + 2 / 3 * 6
/ 3
0 - 3 + 0 * 6 /
3
0 - 3 + 0 / 2
0 - 3 + 0
-3 +0
-3
s = -3
[E] Convert the following
equations into corresponding C statements.
a. Z=(8.8*(a+b)*2/c-(0.5+2*a)/(q+r))/(a+b)*(1/m);
b. X=(-b+(b*b)+2*4*a*c)/2*a;
c. R=(z*v+6.22*(c+d))/(g+v);
d. A=(7.7*b*(x*y+a))/c(-0.8+2*b)/(x+a)*(1/y);
[F] What would be the output
of the following programs:
a.
b.
c.
d.
e.
f.
[G] Pick up the correct alternative for each of the following questions:
a. (2) b. (4) c.(2) d. (4) e.(1) f.(1)
g. (4) h. (3) i.(4) j.(4) k.(4) l.(1)
m. (3) n.(1) o.(3) p.(4) q.(1) r.(3)
s.(2) t.(1) u.(4) v.(4) w.(1) x.(4)
y.(4) z.(2)
[H] Write C programs for the
following:
(a) Ramesh’s basic salary is input through the keyboard. His
dearness allowance is 40% of basic salary, and house rent allowance is 20% of
basic salary. Write a program to calculate
his gross salary.
{
float bs,da,hr,ts;
printf("\n\t\t Do You Want to Calculate Your Salary ? "); printf("\n\t\t*******************************************");
printf("\n\t\t Enter Your Basic Salary = ");
scanf("%f",&bs);
da=(40.0/100)*bs;
hr=(20.0/100)*bs;
ts=da+hr+bs;
printf("\n\t\t Your Total Salary is = %f",ts);
return 0;
}
(b) The distance between two cities (in km.) is input through the
keyboard. Write a program to convert and print this distance in meters, feet,
inches and centimeters.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float distance,m,f,i,cm;
printf("Enter The Distance in Km = ");
scanf("%f",&distance);
m=distance*1000;
printf("%f m\n",m);
f=m*3.28084;
printf("%f feets\n",f);
i=12*f;
printf("%f inches\n",i);
cm=2.54*i;
printf("%f cm\n",cm);
return 0;
}
(c) If the marks obtained by a student in five different subjects are
input through the keyboard, find out the aggregate marks and percentage marks
obtained by the student. Assume that the maximum marks that can be obtained by
a student in each
subject is 100.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pf,dld,ps,dm,la,aggregate;
printf("\n\t\t Do You Want to Calculate Your Aggregate ?");
printf("\n\t\t************************************************");
printf("\n\t\t Enter Programming Fundamental Marks = ");
scanf("%d",&pf);
printf("\n\t\t Enter Digital Logic Design Marks = ");
scanf("%d",&dld);
printf("\n\t\t Enter Pakistan Studies Marks = ");
scanf("%d",&ps);
printf("\n\t\t Enter Discrete Mathematics = ");
scanf("%d",&dm);
printf("\n\t\t Enter Linear Algebra Marks = ");
scanf("%d",&la);
aggregate = (pf+dld+ps+dm+la)*100/500;
printf("\n\t\tYour Aggregate = %d",aggregate);
return 0;
}
(d) Temperature of a city in Fahrenheit degrees is input through the
keyboard. Write a program to convert this temperature into Centigrade degrees.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float centigrade_degrees,fahrenheit;
printf("\n\t\t Enter The Temperature in Fahrenheit = ");
scanf("%f",&fahrenheit);
centigrade_degrees = (fahrenheit-32)*5/9;
printf("\n\t\t Temperature in Centigrade = %f",centigrade_degrees);
return 0;
}
(f) Two numbers are input through the keyboard into two locations
C and D. Write a program to interchange the contents of C and D.
int main ()
{
int A,B,C;
printf("\n\t\tEnter Your First Number
=");
scanf("%d",&A);
printf("\n\t\tEnter Your 2nd Number =
");
scanf("%d",&B);
C=A; A=B; B=C;
printf("\n\t\t A = %d",A);
printf("\n\t\t B = %d\n",B);
}
(e) The length & breadth of a rectangle and radius of a circle
are input through the keyboard. Write a program to calculate the area &
perimeter of the rectangle, and the area & circumference of the circle.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float lenght,width,radius,areaR,perimeterR,areaC,circumferenceC;
printf("\t\tEnter The Lenght = ");
scanf("%f",&lenght);
printf("\n\t\tEnter the width = ");
scanf("%f",&width);
areaR=lenght*width;
perimeterR=2*(lenght*width);
printf("\n\t\tArea of the rectangle = %f",areaR);
printf("\n\t\tPerimeter of Rectangle = %f",perimeterR);
printf("\n\n\t\tEnter the value of radius =");
scanf("%f",&radius);
areaC=3.14*radius*radius;
circumferenceC=2*3.14*radius;
printf("\n\t\tArea of the Circle = %f",areaC);
printf("\n\t\tCircumference of Circle = %f\n",circumferenceC);
return 0;
}
(g) If a five-digit number is input through the keyboard, write a program
to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,a,b,c,d,e,f,g,h,sum;
printf("\n\t\t Enter Your Desire 5 Digit Number = ");
scanf("%d",&num);
a=num/10000;
b=num%10000;
c=b/1000;
d=b%1000;
e=d/100;
f=d%100;
g=f/10;
h=f%10;
sum=a+c+e+g+h;
printf("\n\t\t Sum of the Digits = %d",sum);
return 0;
}
(h) If a five-digit number is input through the keyboard, write a program
to reverse the number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,rem1,rem2,rem3,rem4,rem5,rev;
printf("\n\t\t Enter Your Favourite 5
Digits =");
scanf("%d",&num);
rem1=num/10000;
num=num%10000;
rem2=num/1000;
num=num%1000;
rem3=num/100;
num=num%100;
rem4=num/10;
num=num%10;
rem5=num;
rev=rem5*10000+rem4*1000+rem3*100+rem2*10+rem1*1;
printf("\n\t\t Your Reverse Number is
=%d\n",rev);
return 0;
}
(i) If a four-digit number is input through the keyboard, write a program
to obtain the sum of the first and last digit of this number.
#include
<stdio.h>
#include
<stdlib.h>
int Main()
{
int num,rem1,rem2,rem3,rem4,sum;
printf("\n\t\t Enter Your Favourite 4
Digits =");
scanf("%d",&num);
rem1=num/1000;
num=num%1000;
rem2=num/100;
num=num%100;
rem3=num/10;
num=num%10;
rem4=num;
sum=rem1+rem4;
printf("\t\tThe Sum of first and last
digit = %d",sum);
return 0;
}
(j) In a town, the percentage of men is 52. The percentage of total
literacy is 48. If total percentage of literate men is 35 of the total
population, write a program to find the total number of illiterate men and
women if the population of the town is 80,000.
int main ()
{
int pop,percentagemen,percentagewomen;
int percentageliteracy,percentageliteratemen,
percentageliteratewomen,numofmen,numofwomen;
int
numofliteratemen,numofliteratewomen,numofillitratemen,numofillitratewomen;
pop=80000;
//given
percentagemen=52; //given
percentagewomen=48; //100-52=48
percentageliteracy=48; //given
percentageliteratemen=35; //given
percentageliteratewomen=13; //48-35=13
numofmen=(pop/100)*52;
numofwomen=pop-numofmen;
numofliteratemen=(numofmen/100)*35;
numofliteratewomen=(numofwomen/100)*13;
numofillitratemen=numofmen-numofliteratemen;
numofillitratewomen=numofwomen-numofliteratewomen;
printf("\n\t\t Number of illitrate Men
= %d",numofillitratemen);
printf("\n\t\t Number of illitrate
Women = %d\n",numofillitratewomen);
return 0;
}
(k) A cashier has currency notes of denominations 10, 50 and 100.
If the amount to be withdrawn is input through the keyboard in hundreds, find
the total number of currency notes of each denomination the cashier will have
to give to the withdrawer.
int main()
{
int withdraw,h,f,t,remember,remember1;
printf("\n\t\t Enter Your Withdraw
Amount = ");
scanf("%d",&withdraw);
h=withdraw/100;
remember=withdraw%100;
printf("\n\t\t Number Of Hundreds
Notes = %d",h);
if (remember>50)
{
f=remember/50;
remember1=remember%50;
remember1=remember1/10;
printf("\n\t\t Number of Fifty
Notes = %d",f);
printf("\n\t\t Number of Tens Notes
=%d\n",remember1);
}
else
{
t=remember/10;
printf("\n\t\t Number of Tens Notes =
%d\n",t);
}
return 0;
}
(l) If the total selling price of 15 items and the total profit
earned on them is input through the keyboard, write a program to find the cost
price of one item.
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
float sellingprice,profit,costperitem;
printf("\n\t\t Enter the total selling
Price = ");
scanf("%f",&sellingprice);
printf("\n\t\t Enter the total profit
= ");
scanf("%f",&profit);
costperitem=(sellingprice-profit)/15;
printf("\n\t\t Cost price of one item
= %f",costperitem);
return 0;
}
(m) If a five-digit number is input through the keyboard, write a program
to print a new number by adding one to each of its digits. For example if the
number that is input is 12391 then the output should be displayed as 23502.
{
int
num,rem1,rem2,rem3,rem4,rem5;
int dig1,dig2,dig3,dig4,dig5;
int carry1,carry2,carry3,carry4,carry5;
printf("\n\t\t Enter Your Favourite 5
Digits =");
scanf("%d",&num);
rem1=num/10000;
num=num%10000;
rem2=num/1000;
num=num%1000;
rem3=num/100;
num=num%100;
rem4=num/10;
num=num%10;
rem5=num;
rem5=rem5+1;
dig5=rem5%10;
carry1=rem5/10;
rem4=rem4+1;
rem4=rem4+carry1;
dig4=rem4%10;
carry2=rem4/10;
rem3=rem3+1;
rem3=rem3+carry2;
dig3=rem3%10;
carry3=rem3/10;
rem2=rem2+1;
rem2=rem2+carry3;
dig2=rem2%10;
carry4=rem2/10;
rem1=rem1+1;
rem1=rem1+carry4;
printf("%d",rem1);
printf("%d",dig2);
printf("%d",dig3);
printf("%d",dig4);
printf("%d",dig5);
return 0;
}
Great Initiative
ReplyDeleteMore power to you
Thank You Dear
Delete