Let Us C / Chapter 1 (Getting started)

                          Exercise [H]



(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.

Solution:

#include<stdio.h>
#include<conio.h>
main() {
long bs,da,hra,gs;
clrscr();
printf("Please enter Ramesh's Basic Salary: \n");
scanf("%ld",&bs);
gs=(40*bs/100)+(20*bs/100)+bs;
printf("Ramesh's GROSS SALARY = %ld\n",gs);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
long km,m,ft,inc,cm;
clrscr();
printf("Please enter the distance(in km):\n");
scanf("%ld",&km);
m=km*1000;
ft=m*3;
inc=ft*12;
cm=ft*30;
printf("\nDistance in kilometer = %ld\n",km);
printf("\nDistance in meter = %ld\n",m);
printf("\nDistance in feet = %ld\n",ft);
printf("\nDistance in inches = %ld\n",inc);
printf("\nDistance in centimeters = %ld\n",cm);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int m1,m2,m3,m4,m5,ttl;
float prcnt;
clrscr();
printf("Please enter the marks of the student:\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
ttl=m1+m2+m3+m4+m5;
prcnt=ttl/5;
printf("AGGREGATE MARKS = %d\nPERCENTAGE MARKS = %f%\n",ttl,prcnt);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
float fh,cn;
clrscr();
printf("Please enter the temperature in fahrenheit:\n");
scanf("%f",&fh);
cn=5*(fh-32)/9;
printf("Centigrade Temperature = %f \n",cn);
getch();
return 0;
}

------------------------------------------------------------------------------------------------------------

(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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
float l,b,area,peri,ca,cr,r;
clrscr();
printf("Please enter the length of rectangle:");
scanf("%f",&l);
printf("\nPlease enter the breadth of rectangle:");
scanf("%f",&b);
area=l*b;
peri=2*(l+b);
printf("\n\nAREA = %f square cm.\t PERIMETER = %f cm.\n",area,peri);
printf("\n\nPlease enter the radius of the circle:");
scanf("%f",&r);
ca=3.14*r*r;
cr=2*3.14*r;
printf("\n\nAREA = %f square cm.\t CIRCUMFERENCE = %f cm.\n",ca,cr);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int c,d,t;
clrscr();
printf("Enter value of C: ");
scanf("%d",&c);
printf("\n\nEnter value of D: ");
scanf("%d",&d);
t=c;
c=d;
d=t;
printf("\n\nC = %d\nD = %d\n",c,d);
getch();
}

------------------------------------------------------------------------------------------------------------

(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 ‘%’)

Solution:


#include<stdio.h>
#include<conio.h>
main() {
long i,d1,d2,d3,d4,d5,a,b,c,d,sum;
clrscr();
printf("Please enter the five digit number: \n");
scanf("%ld",&i);
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;
sum=d1+d2+d3+d4+d5;
printf("Sum of the digits is %ld\n",sum);
getch();
return 0;
}

------------------------------------------------------------------------------------------------------------

(h) If a five-digit number is input through the keyboard, write a program to reverse the number.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
long i,d1,d2,d3,d4,d5,a,b,c,d,n_num;
clrscr();
printf("Please enter the five digit number: \n");
scanf("%ld",&i);
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);
printf("reversed number = %ld \n",n_num);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int i,d1,d4,a,b,c,sum;
clrscr();
printf("Please enter the four digit number: \n");
scanf("%d",&i);
d1=i/1000;
a=i%1000;
b=a%100;
c=b%10;
d4=c/1;
sum=d1+d4;

printf("The sum of first and last digit is %d\n",sum);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int i,d1,d4,a,b,c,sum;
clrscr();
printf("Please enter the four digit number: \n");
scanf("%d",&i);
d1=i/1000;
a=i%1000;
b=a%100;
c=b%10;
d4=c/1;
sum=d1+d4;
printf("The sum of first and last digit is %d\n",sum);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
int a,t_10,f_50,h_100,b,c;
clrscr();
printf("Please enter the amount to be withdrawn: \n");
scanf("%d",&a);
h_100=a/100;
b=a%100;
f_50=b/50;
c=b%50;
t_10=c/10;

printf("\nCurrency notes of 100 should be = %d\n",h_100);
printf("\nCurrency notes of 50 should be = %d\n",f_50);
printf("\nCurrency notes of 10 should be = %d\n",t_10);
getch();
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.

Solution:



#include<stdio.h>
#include<conio.h>
main() {
int sp,p,cp;
clrscr();

printf("Please enter the selling price of 15 items: \n");
scanf("%d",&sp);
printf("\nPlease enter the total profit of 15 items: \n");
scanf("%d",&p);
cp=(sp-p)/15;
printf("Cost price of one item is %d\n",cp);
getch();
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.

Solution:


#include<stdio.h>
#include<conio.h>
main() {
long i,add;
clrscr();
printf("Please enter a five digit number: \n");
scanf("%ld",&i);
add=i+11111;
printf("addition = %ld\n",add);
getch();
return 0;
}

______________________________________________________________________


14 comments:

  1. program for question no m is not correct for all 5digits no.....

    ReplyDelete
    Replies
    1. practically that is correct. 9 + 1 is 10 not 0. Let us C 5th edition is an old book and had a lot of errors. but the new edition is improved. Refer 13th edition for the same exercise.

      Delete
  2. Answers for Questions (i) & (j) are same by mistake. Write correct answer of question J. and thanks for other answers. they are best solutions.

    ReplyDelete
    Replies
    1. oh yes, sorry for this mistake. Work is in progress, very soon whole blog will be updated with modified solutions and solutions of all other books of yashwant kanetkar.

      Delete
  3. Que (m) not wrong that can be done like below:

    main()
    {
    long num,i,j,k,l,m,new;
    clrscr();
    printf("Enter a five digit number\n");
    scanf("%ld",&num);
    i=((num%10)+1)%10;
    m=num/10;
    j=((m%10)+1)%10;
    m=m/10;
    k=((m%10)+1)%10;
    m=m/10;
    l=((m%10)+1)%10;
    m=((m/10)+1)%10;
    newnum=10000*m+1000*l+100*k+10*j+1*i;
    printf("Result is %ld",newnum);
    }

    ReplyDelete
    Replies
    1. Yes, =) There are many other ways to solve a program, it's programmer's personal choice ;)

      Delete
  4. J ANSWER IS copied as above solution ...BUT IT IS SIMPLE

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. J ANSWER IS copied as above solution ...BUT IT IS SIMPLE

    ReplyDelete
  7. Very helpful
    You did great work
    Thanks

    ReplyDelete

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...