Saturday, 15 October 2011

(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();
}