Monday

Chapter 1: Getting Started

[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?

ANS:
If the Salary of Ramesh is $100.
then his dearness allowance will be =  (100*40%) or Rs.40
then his rent allowance will be  =  (100*20%) or Rs.20
So his gross or total salary will be = 100+40+20
                                                 = Rs.160

Solution in C
Using codeblock IDE

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int salary,gross_salary;
    printf("Enter the basic salary=");
    scanf("%d",&salary);
    gross_salary = salary+(salary*0.20)+(salary*0.40);
    printf("The gross salary is =%d",gross_salary);
    return(0);

}

b) The distance between two cities (in Km) is input through the keybpard. Write a program to convert and print this distance in metres, feet,inches and centumeters.

ANS: As we know,
1km = 1000m
1km = 100000 cm
1km = 39370.1 inch
1km = 3280.84 feet

Solution in C
Using Code#block IDE

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float km,inch,feet,centimeter,meter;
    printf("Enter the distance in Km:");
    scanf("%f",&km);
    meter = km*1000;
    printf("\nDistance in meter=%f",meter);
    feet = km*3280.84;
    printf("\nDistance in feet=%f",feet);
    inch = km*39370.1;
    printf("\nDistance in inch=%f",inch);
    centimeter = km*100000;
    printf("\nDistance in centimeter=%f",centimeter);
    return(0);
}

OR

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float km,inch,feet,centimeter,meter;
    printf("Enter the distance in Km:");
    scanf("%f",&km);
    printf("\nDistance in meter=%f",km*1000);
    printf("\nDistance in feet=%f",km*3280.84);
    printf("\nDistance in inch=%f",km*39370.1);
    printf("\nDistance in centimeter=%f",km*100000);
    return(0);
}

Both will give you the same result.


(c) If the marks obtained by a student in five subjects are input through the keyboard,find out the aggregate marks obtained by a student. Assume that the maximum marks that cab ne obtained by a student in each subject is 100.

ANS:
Suppose
Mr. Paul result sheet is
English     : 80
Math        : 85
Biology    : 80
Chemistry: 90
Physics    : 85

So, He his total mark is  = 80+85+80+90+85 = 420 out of 500
So his Percentage is  = (420/500)*100 = 84%

Solution in C
Using Code#block IDE
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sub1,sub2,sub3,sub4,sub5,total_mark,percentage;
    printf("Enter the mark of 1st subject out of 100:");
    scanf("%d",&sub1);
    printf("Enter the mark of 2nd subject out of 100:");
    scanf("%d",&sub2);
    printf("Enter the mark of 3rd subject out of 100:");
    scanf("%d",&sub3);
    printf("Enter the mark of 4th subject out of 100:");
    scanf("%d",&sub4);
    printf("Enter the mark of 5th subject out of 100:");
    scanf("%d",&sub5);
    total_mark = sub1+sub2+sub3+sub4+sub5;
    percentage = (total_mark*100)/500;
    printf("\nThe total marks obtained in 5 subejects is=%d",total_mark);
    printf("\nThe percentage is=%d %",percentage);
    return(0);

}

(c) Temperature of a city in Farenheit degree is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

ANS:
The formula is C = 5/9 (F-32)

Solution in C
Using Code#block IDE

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float farenheit,centigrade;
    printf("Enter the Farenheit:");
    scanf("%f",&farenheit);
    centigrade = (farenheit-32)*0.5555556;
    printf("The Centigrade degree is:%f",centigrade);
    return(0);
}



(f) Two numbers are input through the keyboard into two location C and D. Write a program to interchange the contents of C and D.

ANS:
Inorder to exchange the number. First it is necessary to keep one number in a temporary varaiable here the value of C has been stored in a temporary variable. Then the value of D has been assigned in the value of C. As we have already stored the value of C in a variable named as "storing_temoporary_value" we will assign that value in the value of D. Now both the value has been exchanged.

Solution in C
Using Code::block IDE

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c,d,storing_temorary_value;
    printf("Enter C:");
    scanf("%d",&c);
    printf("Enter D:");
    scanf("%d",&d);
    storing_temorary_value = c;
    c = d;
    d = storing_temorary_value;
    printf("C is now %d",c);
    printf("\nD is now %d",d);
    return(0);
}

(g) If a 5 digit number is input through the keyboard write a program to calculate the sum of its 5 digits.

ANS:
Inorder to do that we need to use modulus or "%" this sign to make it more simple.
what is it???
It is nothing not the remainder.
for say, if we divide 10/2 we will get = 0 remainder
if we divide 15/2 we will get 1.

Now if we divide the number by 10 using the modulus we will get the last digit only.
Then we will divide the number by 10 to reduce the number.
e.g. 12345%10; it will give us 5 That is the last digit of 12345. 
Now if we divide the number by 10 we will get 1234.
REMEMBER "Integer" always remove the remainder. So it will not consider the remainder. So we will follow the same steps again and again until we get the first digit.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c,d,e,f,g,h,i,add;
    printf("Enter you 5 digit:");
    scanf("%d",&a);
    b = a%10;//5
    c = a/10;//1234
    d = c%10;//4
    e = c/10;//123
    f = e%10;//3
    g = e/10;//12
    h = g%10;//2
    i = g/10;//1

    add = b+d+f+h+i;
    printf("The total addition of digits is = %d",add);
    return(0);


(h) If a 5 digit number if input through a keyboard, write a program to reverse the number.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c,d,e,f,g,h,i,j;
    printf("Enter you 5 digit:");
    scanf("%d",&a);
    b = a%10;//5
    c = a/10;//1234

    d = c%10;//4
    e = c/10;//123

    f = e%10;//3
    g = e/10;//12

    h = g%10;//2
    i = g/10;//1


    printf("The total addition of digits is = %d%d%d%d%d",b,d,f,h,i);
    return(0);

}

(i)  If a 4 digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number.

Solution in C
Using Code::block IDE

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c,d,e,f,g,h,i;
    printf("Enter you 4 digit:");
    scanf("%d",&a);
    b = a%10;//5
    c = a/10;//1234

    d = c%10;//4
    e = c/10;//123

    f = e%10;//3
    g = e/10;//12

    h = g%10;//2
    i = b+h;

    printf("The sum of 1st and last digit is : %d",i);
    return(0);

}