Friday, 23 November 2012

Example:Program of Perfect Number and Reverse of a Number in C language


By on 01:19

Program of Perfect Number:

Suppose we enter 6 then


Perfect no. like   "  6->1+2+3+6=12(factors)  then 

                                    2*6=12 when we double"     

Q)Write a program to enter a value and check the number is perfect or not.


Note:-You can also create this program in java,c# etc. with the same logic.


P:#include<stdio.h>
void main()
{
int i,n,s;
printf("enter no.");
scanf("%d",&n);
s=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==2*n)
printf("perfect no.");
else
printf("No. is not perfect");
getch();
}

Program of Reverse Number:


Reverse of no. like 134=431(Reverse)


Q)Write a program to enter a value and print the reverse of digit.



P:#include<stdio.h>
void main()
{
int r,n,d;
printf("enter no.");
scanf("%d",&n);
r=1;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf("reverse value=%d",r);
getch();
}

If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter. 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...