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

}