Saturday 15 October 2011

Chapter 1: [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.



/* This program takes basic salary from user, calculates dearness and house rent
allowances and then gross salary and shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*declaring veriables*/
    float bsalary, dallow, hrallow, grsalary;
   
    /*prompt to take bsalary as input*/
    printf("Please enter the basic salery: ");
   
    /*taking input*/
    scanf("%f",&bsalary);
   
    /*calculations*/
    dallow = 0.4* bsalary;
    hrallow = 0.2 * bsalary;
    grsalary = bsalary + dallow + hrallow;
  
    /*showing results*/
    printf("\n\nBasic Salary          :          %f Rs\n",bsalary);
    printf("\nDearness Allownce     :          %f Rs \n",dallow);
    printf("\nHouse Rant Allownce   :          %f Rs \n",hrallow);
    printf("___________________________________________\n");
    printf("\nGross Salary          :          %f Rs\n",grsalary);
    getche();
    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.


/* This program takes lenght in kilo-meters from user, converts it into meters,
centimeters, feet and inches and then shows results .*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*delaring and initializing variables*/
    float km=0, meter=0, cm=0, feet=0, inch=0;
   
    /*prompt for input*/
    printf("Please enter the distance in 'kilo meters': ");
  
    /*taking length in kilo-meters as input*/
    scanf("%f",&km);
   
    /*calculations*/
    meter = km * 1000;
    cm = meter * 100;
    feet = meter * 3.280839895;
    inch = feet * 12;
   
    /*showing results*/
    printf("Here is the conversion of %f km",km);
    printf("\n\n      In Meters: %f m      \n\n      In Centi Meters: %f cm      \n\n      In Feet: %f feet      \n\n      In inches: %f inch     ",meter,cm,feet,inch);    

    getche();
    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.



/* This program takes marks of a student in five subjects from user,
finds aggregate marks and percentage, and then shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*Declaring and initializing veriables*/
    int sub1 = 0, sub2 = 0, sub3 = 0, sub4 = 0, sub5 = 0, aggmarks  = 0;
    float percentmarks = 0;
   
    /*Taking marks as input from the user*/
    printf("Plese enter the marks of the student in subject 1: ");
    scanf("%d",&sub1);
    printf("\nnow enter the marks of the student in subject 2: ");
    scanf("%d",&sub2);
    printf("\nnow enter the marks of the student in subject 3: ");
    scanf("%d",&sub3);
    printf("\nnow enter the marks of the student in subject 4: ");
    scanf("%d",&sub4);
    printf("\nnow enter the marks of the student in subject 5: ");
    scanf("%d",&sub5);
   
    /*Calculations*/
    aggmarks = sub1 + sub2 +sub3 +sub4 +sub5;
    percentmarks = (aggmarks/500.0)*100.0;
  
    /*Showing Results*/
    printf("\n________________________________________\n");
    printf("\nAggregate Marks of the Student are: %d \n",aggmarks);
    printf("\nwhich are %f percent of total marks.",percentmarks);
    getche();
    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.



/* This program takes temperature in Fahrenhites as input from user converts it into
degree celsius and then shows results .*/
#include<stdio.h>
#include<conio.h>
int main()
{
    /*Declaring and initializing variables*/
    float celcius = 0;
    float fahrenhite =0;
   
    /*prompt for input*/
    printf("\nPlease enter the temperature in Degree Fahrenhites: ");
  
    /*Taking temperature in fahrenhite as input from user*/
    scanf("%f",&fahrenhite);
   
    /*Calculations*/
    celcius = (5.0*(fahrenhite-32))/9.0;
   
    /*showing results*/
    printf("\nTemperature is %f degree celcius",celcius);
    getche();
    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.



/* This program takes length and breadth of rectangle and radius of a circle from
user as input then finds area and perimeter of the rectangle and the circumference of t
he circle, then shows results.*/
#include<stdio.h>
#include<conio.h>
int main()
{
    /*Declaring and initializing variables*/
    float lengthrect = 0, breadthrect =0, radius =0, area =0, perimeter =0, circumference =0 ;
   
    /*Taking input from user*/
    printf("Please enter the length of Rectangle: ");
    scanf("%f",&lengthrect);
    printf("\nNow enter the breadth of Rectangle: ");
    scanf("%f",&breadthrect);
    printf("\nNow enter the radius of circle: ");
    scanf("%f",&radius);
   
    /*Calculations*/
    area = lengthrect * breadthrect;
    perimeter = 2 * (breadthrect + lengthrect);
    circumference = 2 * 3.1416 * radius;
   
    /*showing results*/
    printf("\n\nResults: \n");
    printf("__________");
    printf("\nArea of the Rectangle: %f units\n",area);
    printf("\nPerimeter of the Rectangle: %f units\n",perimeter);
    printf("\nCircumference of the circle: %f units \n",circumference);
    getche();
    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.


/* This program takes 2 digits and stores them in two location, then interchanges
the location of the numbers.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*declaring veriables*/
    int C,D, temp;
   
    /*prompt to take and taking input*/
    printf("   Please enter the number to store in location C: ");
    scanf("%d",&C);
    printf("\n   Now enter the number to store in location D: ");
    scanf("%d",&D);
   
    /*calculations*/
    temp = C;
    C = D;
    D = temp;
   
    /*showing results*/
    printf("\n   After Interchanging: ");
    printf("\n   __________________");
    printf("\n\n    Now number stored in location C is: %d\n",C);
    printf("\n    and the number stored in location D is: %d ",D);
    getche();
    return 0;
}


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


/* This program takes a five-digit integer from user, seprates its digits, adds them
and then shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{

/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,sum;

/*prompt to take the input*/
    printf("\n   Please enter a five-digit number: ");

/*Taking input*/   
    scanf("%d",&value);

/*Calculations*/
  
    dig1 = value % 10;
  
    /* e.g. if value is 12345 then 12345 % 10 gives 5*/
  
    value = value / 10;
  
    /* '12345/10' gives '1234.5' but this is integer division so digit/s after '.' will
    be dropped and we get '1234'*/
  
    dig2 = value % 10;
  
    /*now '1234 % 10' gives '4' and so on*/
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
    sum = dig1+dig2+dig3+dig4+dig5 ;

/*showing Results*/   
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   %d + %d + %d + %d + %d = %d",dig5, dig4, dig3, dig2, dig1, sum );
    getch();
    return 0;
}
   


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


/* This program takes a five-digit integer from user, and shows its reverse.*/
#include<stdio.h>
#include<conio.h>
int main ()
{

/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,reverse,temp;

/*prompt to take the input*/
    printf("Please enter a five-digit number: ");

/*Taking input*/   
    scanf("%d",&value);
 temp = value ;

/*Calculations*/
    dig1 = value % 10;
  
    /* e.g. if value is 12345 then 12345 % 10 gives 5*/
  
    value = value / 10;
  
    /* '12345/10' gives '1234.5' but this is integer division so digit/s after '.' will
    be dropped and we get '1234'*/
   
    dig2 = value % 10;
  
    /*now '1234 % 10' gives '4' and so on*/
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
   
    reverse = (dig1*10000) + (dig2*1000) + (dig3*100) + (dig4*10) + (dig5) ;

/*showing Results*/   
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   By reversing %d it becomes : %d",temp,reverse);
    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.


/* This program takes a four-digit integer from user, and shows sum of first and last digit.*/
#include<stdio.h>
#include<conio.h>
int main ()
{

/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,sum;

/*prompt to take the input*/
    printf("Please enter a foure-digit number: ");

/*Taking input*/   
    scanf("%d",&value);


/*Calculations*/
    dig1 = value % 10;
  
    /* e.g. if value is 12345 then 12345 % 10 gives 5*/
  
    value = value / 10;
  
    /* '12345/10' gives '1234.5' but this is integer division so digit/s after '.' will
    be dropped and we get '1234'*/
   
    dig2 = value % 10;
  
    /*now '1234 % 10' gives '4' and so on*/
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
   
   
    sum = dig4 + dig1;

/*showing Results*/   
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   Sum of %d and %d is: %d",dig4,dig1,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.


#include <stdio.h>
#include<conio.h>
int main()
{
    int amount;
  
    // taking input   
    printf("Enter the amount to be withdrawn (in hundreds): ");
    scanf("%d",&amount);
   
    //calculations
    printf("\n\nRequired notes of Rs. 100  :  %d", amount / 100);
    // e.g. if amount entered is 987 then 987 / 100 will answer 9 (because amount is an intiger variable) 
   
    printf("\n\nRequired notes of Rs. 50   :  %d", (amount % 100) / 50);
    //e.g. 987 % 100 will answer 87 and then 87 / 50 will answer 1.
   
    printf("\n\nRequired notes of Rs. 10   :  %d", (((amount % 100) % 50) / 10));
   
    printf("\n\nAmount still remaining Rs. :  %d", (((amount % 100) % 50) % 10));
   
    getch();
}


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

#include<stdio.h>
#include<conio.h>
int main ()
{
    int pop, popmen, popwomen, poplit, poilit, litmen, ilitmen, litwomen, ilitwomen;
   
    pop = 80000;
   
    //Calculations
    //population of men
    popmen = (52 * pop) / 100;
   
    //population of women
    popwomen = pop - popmen;
   
    //literate population
    poplit = (48 * pop) / 100;
   
    //literate population of men
    litmen = (35 * pop) / 100;
   
    ////literate population of women
    litwomen = poplit - litmen;
   
    //iliterate population of men
    ilitmen = popmen - litmen;
   
    //iliterate population of women
    ilitwomen = popwomen - litwomen;
   
    printf("\n\t\t\tDATA\n\n");
    printf("_____________________________________________\n\n");
    printf("Total population of the city     :\t%d\n\n",pop);
    printf("Total population of men          :\t%d\n\n",popmen);
    printf("Total population of women        :\t%d\n\n",popwomen);
    printf("Literate population of men       :\t%d\n\n",litmen);
    printf("Literate population of women     :\t%d\n\n",litwomen);
    printf("Illiterate population of men     :\t%d\n\n",ilitmen);
    printf("Illiterate population of women   :\t%d\n\n",ilitwomen);
    getche();
}