Monday, June 26, 2006

Sum of First and Last Digits of a Four-Digit number.

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.


If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.

/*HINT: If a number is divided using % , then the number to the right side of the decimal point

is the result. (This applies only to integers.) */






#include<stdio.h>

main ()

{

int number, last_digit, first_digit, total;

printf (" Enter the number which is to be operated on: ");

scanf ("%d", &number);



last_digit = number % 10;

total = last_digit;



first_digit = (number / 1000) % 10;

total = total + first_digit;



printf ("The total of the first and the last digit of the entered number is: %d", total);



}





/*This program is based on the "Sum of Digits" Program */

The File can be found out at:
Download File

Sum of digits of a Five Digit Number.

This program is a bit of a tricky one. The Program clears our concepts about using the "Modulus Operator" . Once done, there is no stopping us then. The other programs follow the same path and almost have the same logic with some minor changes.

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 '%')


/*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 '%') */





/*Is 12345 / 100 % 10 not 3?
The divide by 100 strips the 45 and the remainder of 123 / 10 would be 3.

unit digit 5 would be 12345 % 10
tens digit would be (12345 / 10) % 10
hundreds digit would be (12345 / 100) % 10 ...
*/



/* Using / ..the normal division opeator returns the quotient.
Using % ..the modulus Operator returns the Remainder. */



#include<stdio.h>
main ()
{
int number, last_digit, next_digit, total;

printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &number);

last_digit = number%10;
total = last_digit;

next_digit = (number/10) % 10;

total = total + next_digit;

next_digit = (number/100) % 10;

total = total + next_digit;

next_digit = (number/1000) %10;

total = total + next_digit;

next_digit = (number/10000) %10;

total = total + next_digit;


printf ("The sum of the digits of the entered number is: %d", total);

}





The File can be found at:
Download File

C Program to Convert Celcius to Fahrenheit and vice versa

This Program converts the Temperature from Fahrenheit to Celcius and vice versa.
Mind you, the formula for the conversion is just a multiplication by a factor one.

The conversion if non-linear and the logic is included in the program itself.

Temperature of a city in Fahreinheit degrees is input through the keyboard.
Write a program to convert this temerature into Centigrade degrees.



/*To convert Fahrenheit to Celsius, subtract 32 degrees and divide by 1.8.
To convert Celsius to Fahrenheit, multiply by 1.8 and add 32 degrees.*/



#include<stdio.h>

main ()
{
float temp_c, temp_f;

printf ("Enter the value of Temperature in Celcius: ");

scanf ("%f", &temp_c);

temp_f = (1.8 * temp_c) + 32;

printf ("The value of Temperature in Fahreinheit is: %f", temp_f);

}

Reversing a Five Digit Number (Integer)

This program reverses a Five Digit number. The number is entered through the keyboard and worked upon by the program.

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

/*Use an integer variable to store the reverse (say rev_num).
take out the first (rightmost) digit of the number.
Give it to rev_num.
Take out the 2nd digit from right.
Now multiply rev_num by 10 and add this new digit to it.
Do this till the last digit.


*/





/*To understand the program, assume that the five digit number -- 12345 is entered
Calculate the values manually and check against the given number.

The final result (reversed number) is going to be 54321*/




#include<stdio.h>
main()
{
int number, rev_num, next_digit,last_digit;

printf ("Enter the number that is to be reversed: ");
scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);
}



Friday, June 23, 2006

Calculation of Simple Interest

This Program calculates the value of Simple Interest. The Values of Principle, Rate of Interest and the number of years is input through the keyboard.


Write a program to calculate the simple interest.
The values of principle, rate of interest and the number of years are input from the keyboard.
Display the value of the calculated Simple interest on a new line.



#include<stdio.h>
main ()

{
int p, n;
float r, si;
printf(" Enter the values of p, n, r: ");

scanf("%d %d %f", &p, &n, &r);

si=p*n*r/100;

printf("%f", si);
}


The File can be found at:
Download File

Calculation of Total and Percentage

Calculates the total and percentage of the marks obtained in five subjects by a student. The marks obtained are to be entered by the student.


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>
main()
{

int m1, m2, m3, m4, m5, total;
float percentage;

printf ("Enter the marks obtained by the student in all the five subjects: ");
scanf ("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);

total= m1+m2+m3+m4+m5;
percentage = total/5;

printf ("\nThe aggregate marks obtained by the student are: %d", total);
printf ("\nThe percentage obtained by the student are %f", percentage);

}






The File can be found at:
Download File

Interchanging Two Numbers

This program interchanges the two numbers inputted from the keyboard. Just yet another simple program...


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


#include<stdio.h>
main ()

{
int A,C,D;
printf ("Enter the value of C and D: ");

scanf ("%d %d", &C, &D);

A=C;

C=D;
D=A;

printf ("\nThe exchanged values of C and D are: %d and %d ", C, D);

}

Distance Conversion Program

This Program converts the value entered in Km to inches, centimetres, feet and metres.


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


/*
Distance Conversion:
1Km = 39370 inches
1Km = 3281 Feet
1Km = 100000 Cm
1Km = 1000 metres
*/






#include<stdio.h>

main()
{
float km,m,cm,inch,foot;

printf("Enter the distance between the two cities in Km: ");
scanf("%f", &km);

inch = 39370*km;
foot = 3281*km;

cm= 100000*km;
m = 1000*km;

printf ("\nDistance converted to inches: %f", inch);
printf ("\nDistance converted to feet: %f", foot);

printf ("\nDistance converted to centimeters: %f", cm);
printf ("\nDistance converted to meters: %f", m);

}


C Program for calculation of Gross Salary

This program takes into account the allowances of a particular candidate and then finally counts the Gross Salary.
The value of the Basic Salary is entered through the keyboard.

Lara's basic salary is input through the keyboard.
Her dearness allowance is 35% of basic salary, and house rent allowance is
25% of basic salary. Write a program to calculate her gross salary.


#include<stdio.h>
main ()

{
int basic;
float gross;

printf("Enter your Basic Salary: ");

scanf("%d", &basic);

gross = basic+(0.35*basic)+(0.25*basic);

printf("Your Gross Salary is: %f",gross);


}


C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.

The program helps you calculate the area and perimeter of a rectangle.
It also calculates the area and the circumference of the circle.
The values of Length, Breadth and Radius are to be entered through the keyboard.


The length and breadth of a rectangle and radius of a circle are input
through the keyboard. Write a program to calculate the area and perimeter of the rectangle,
and the area and the circumference of the circle.


/*
aor: area of rectangle
por: perimeter of rectangle
aoc: area of circle
coc: circumference of circle
*/



#include <stdio.h>

main()
{
float length, breadth, radius, aor, por, aoc, coc;

printf ("\nEnter the Length and Breadth of the Rectangle: ");
scanf("%f %f", &length, &amp;breadth);

printf("\nEnter the Radius of the Circle: ");
scanf("%f", &radius);

aor = length*breadth;
por= 2*(length+breadth);


aoc = 3.14*radius*radius;

coc = 2*radius*3.14;

printf("\nThe area of the rectangle is: %f", aor);

printf("\nThe perimeter of the rectangle is: %f ", por);


printf("\n\nThe area of the Circle with radius %f is: %f ", radius, aoc);

printf("\nThe circumference of the same circle is: %f", coc);

}