Sunday, January 27, 2013

Decimal to Binary Logic

/*
Program to to conver the decimal number to the binary equivalent
OS: UNIX or Windows
*/

#include<stdio.h>
void main()
{
int num,bin=0,rem,k=1;
printf("Enter the decimal number:\n");
scanf("%d",&num);
while(num!=0)
{
rem=num%2;
bin=bin+rem*k;
k=k*10;
num=num/2;
}
printf("Binary Equivqlent of a given number is %d\n",bin);
}

Perfect Number Logic

/*
Program to check the given number is perfect or not
OS: UNIX or Windows

*/

#include<stdio.h>
void main()
{
         int sum=0, num, i;
         printf("Enter the number");
         scanf("%d",&num);
         for(i=1;i<num;i++)
         {
                 if(num%i==0)
                          sum=sum+i;
         }
         if(sum==num)
                   printf("%d is perfect number\n",num);
         else
                   printf("%d is not a perfect number\n",num);
}

/*
 Refer these links to get the clear idea about Perfect numbers

http://mathworld.wolfram.com/PerfectNumber.html

Perfect number 'n' is a positive number where sum of divisors of n is equal to n.

Ex: number is 6, its divisors are 1,2,3 there fore sum of 1+2+3=6

*/