Pages

Tuesday, December 27, 2011

Program to find Armstrong number

Definition :

From c's programming point of view, Those numbers whose sum of the cube of its digits is equal to that number are known as Armstrong numbers .
For example

153 = 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
or
370 = 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370

But in general the definition is :

Those numbers whose sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.

Let k be the number of digits in a number, n, and d1,d2,d3,d4... be the digits of n.
n=d1k+d2k+d3k+d4k+...


Write a C program to check whether a number is Armstrong or not.


#include<stdio.h>

void main(){
int num,sum=0,temp,i;
clrscr();
printf("Enter number: ");
scanf("%d",&num);

temp=num;
while(num!=0){
i=num%10;
num=num/10;
sum=sum+(i*i*i);

}

if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not a Armstrong number",temp);
getch();

}


Output:

Enter number: 153
153 is an Armstrong number

Enter number: 207
207 is not a Armstrong number

Armstrong number in c using for loop

#include<stdio.hgt;

void main(){
int temp,sum=0,i,num;
clrscr();

printf("Enter number : ");
scanf("%d",&num);

for(temp=num;num!=0;num=num/10){
i=num%10;
sum=sum+(i*i*i);

}
if(temp==sum)
printf("%d is an Armstrong Number ",temp);
else
printf("%d is not a Armstrong Number ",temp);
getch();

}

Thursday, December 22, 2011

Program to find sum of all digits of a number

This program calculates the sum of all digits of a number into a single digit.for instance if user has input 2345..the end result will be 2345=14=5


If a number of any digit is iput through keyboard,write a program to obtain the sum of all digits of the number into a single digit


#include<stdio.h>
void main()
{
long num,temp,total=0,r,sum=0;
clrscr();

printf("Enter the number : ");
scanf("%ld",&num);

// it will give sum of all digits
while(num!=0)
{
r=num%10;
num=num/10;
total=total+r;

}

// This will further sum up the total of digits
while(total!=0)
{
temp=total%10;
total=total/10;
sum=sum+temp;
}

printf("Total=%ld ",sum);
getch();

}

Program to find sum of first and last digit of a number of any digit

This program finds the sum of first and last digit of a number that would be of any length


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


#include<stdio.h>

void main(){

/*
since integer range is between -32768 to 32767
it will support till 5 digit number only
so for higher digits you have to declare
with long
*/

long firstdigit,sum=0,r,num;
clrscr();

printf("Enter number : ");
scanf("%ld",&num);

sum=sum+num%10; // num%10 gives lastdigit

while(num!=0){
firstdigit=num; // gives first digit
num=num/10;
}

sum=sum+firstdigit;

printf("Sum of first and last digit : %ld",sum);
getch();


}

Saturday, December 17, 2011

Program to reverse input number

This program reverse the input number.

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

#include<stdio.h>
#include<conio.h>
void main()
{
long num,r;
long sum=0;
clrscr();

printf("Enter number : ");
scanf("%ld",&num);


while(num!=0)
{
r=num%10;
num=num/10;

sum=(sum*10)+r;

}
printf("%ld",sum);

getch();
}

Friday, December 16, 2011

Program to calculate sum of digits of a number

This program calculates sum of any digit number(within integer range).since integer range is-32768 to 32767,it gives error wen number exceeds 5 digit....

#include<stdio.h>
void main()
{
int num,r;
int sum=0;
clrscr();

printf("Enter number : ");
scanf("%d",&num);

while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("%d",sum);

getch();
}

Program to calculate sum of 5-digit number

This program calculates sum of five digit number enterd through keyboard

If a five-digit number is input through the keyboard,write a program to calculate the sum of its digits
#include<stdio.h>
void main()
{
int num,next_num,last_num,total;
clrscr();

printf("Enter the number : ");
scanf("%d",&num);

last_num=num%10;
total=last_num;

next_num=(num/10)%10;
total=total+next_num;

next_num=(num/100)%10;
total=total+next_num;

next_num=(num/1000)%10;
total=total+next_num;

next_num=(num/10000)%10;
total=total+next_num;
printf("\nThe sum of entered 5 digit number is : %d",total);

getch();

}

Program to interchange two values

This program interchanges two values input through keyboard

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>
void main()
{
int C,D,temp;
clrscr();
printf("Enter value of C and D :");
scanf("%d %d",&C,&D);
temp=C;
C=D;
D=temp;
printf("\nThe interchanged value of C and D are %d %d",C,D);
getch();

}

Saturday, November 12, 2011

Program to calculate area,perimeter of rectangle and circumference ,area of circle (Ref:Let us c-chapter1)

This program calculates the area and perimeter of a rectangle and circumference and area of a circle.

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 circumference of the circle.

#include<stdio.h>


void main()
{

float length,breadth,radius,rec_area,circle_area,perimeter,circumference;

clrscr();

printf("Enter length and breath for rectangle:");
scanf("%f%f",&length,&breadth);

printf("Enter radius for circle:");
scanf("%f",&radius);

rec_area=length*breadth;
perimeter=2*(length+breadth);

circle_area=3.14*radius*radius;
circumference= 2*3.14*radius;

printf("\n\nArea of rectancle is : %f",rec_area);
printf("\n\nPerimeter of rectangle is : %f",perimeter);

printf("\n\nArea of circle is : %f",circle_area);
printf("\n\nCircumference of circle is : %f",circumference);

getch();

}

Program to convert Farenhite temperature into Celcius temperature and vice-versa (Ref:Let us c-chapter1)

This program convert entered Farenhite temperature into centigrade temperature or vice-versa.


Temperature of a city in Farenhite degrees is input through the keyboard.Write a program to convert this temperature into centigrade degrees.


Note:To convert Fahrenheit to Celsius, subtract 32 degrees  and divide by 1.8.
while for converting Celsius to Fahrenheit, multiply by 1.8 and add 32 degrees


#include<stdio.h>


void main()


{


float temp_f,temp_c;


clrscr();


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 Fahrenheit is :%f",temp_f);


getch();


}

Monday, November 07, 2011

Program for aggregate marks and percentage calculation
(Ref: Let us c-chapter1)


This program calculates the total and percentage marks.


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>


void main()
{
int m1,m2,m3,m4,m5,total;
float prcnt;
clrscr();
printf("Enter marks for first subject:");
scanf("%d",&m1);
printf("Enter marks for second subject:");
scanf("%d",&m2);
printf("Enter marks for third subject:");
scanf("%d",&m3);
printf("Enter marks for fourth subject:");
scanf("%d",&m4);
printf("Enter marks for fifth subject:");
scanf("%d",&m5);
total=m1+m2+m3+m4+m5;
prcnt=total/5;
printf("Total marks obtained by the student is :%d",total);
printf("\nPercentage is:%f ",prcnt);


getch();


}

Monday, June 27, 2011

Distance Converter Program(Ref: Let us c-chapter1)
This program converts the entered value into meters,feet,inches and centimeters

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.

#include<stdio.h>

/*
1 kilometer=39370 inches
1 kilometer=3281 Feet
1 kilometer=100000 centimeters
1 kilometer=1000 meters

*/

void main()
{
float km,meter,feet,inches,centimeter;
clrscr();
printf("Enter the distance between two cities:");
scanf("%f",&km);

meter=1000*km;
centimeter=100000*km;
inches=39370*km;
feet=3281*km;

printf("\nThe Entered distance between two cities in meter is:%f",meter);
printf("\nThe Entered distance between two cities in centimeter is:%f",centimeter);
printf("\nThe Entered distance between two cities in inches is:%f",inches);
printf("\nThe Entered distance between two cities in feet is:%f",feet);

getch();
}
Gross Salary Program(Ref: Let us c-chapter1)

This program calculate gross salary of an employee if his basic slaray is input thru keyboard.


Ramesh's basic salary is input throught the keyboard.His dearness allowance is 40% of basic salary,and house rent allowance is 20% of basic salary.Write a program to calclate gross salary.

#include<stdio.h>
void main()
{

float bs,gs,hra,da;
clrscr();
printf("Enter basic salary:");
scanf("%f",&bs);
hra=bs*20/100;
da=bs*40/100;
gs=bs+hra+da;
printf("Gross salary= %f",gs);
getch();
}