Powered By Blogger

Saturday, December 25, 2010

C Program to find the reverse of a number

/* C Program to print the reverse of a number */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    int num;
    int count=0;
    int ctr;
    int mult=1;
    int result=0;

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

    while(num>0)
    {
        mult=1;
        for(ctr=0;ctr<count;ctr++)
        {
            mult=mult*10;
        }
        result=result*mult;
        result=result+(num%10);
        num=num/10;
        count++;
    }

    printf("The Reverse of the given number is %d",result);
    return 0;
}

C Program to check if the entered number is a factorial number or not

/* C Program to check whether the entered number is a factorial number or not */

#include <stdio.h>

int main()
{
    int num;
    int flag=0;
    int ctr;

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

    for(ctr=1;;ctr++)
    {
        if((num%ctr)!=0)
        {
            flag++;
            break;
        }
        num=num/ctr;
        if(num==1)
        {
            break;
        }
    }

    if(flag==0)
        printf("The number is a factorial of a number\n");
    else
        printf("The number is not a factorial of a number\n");

    return 0;
}

C Program to check if a number is a magic number or not

/* C Program to check whether an enterd number is a magic number or not*/

#include<stdio.h>
int main()
{
    int n;

    printf("Enter the number\n");
    scanf("%d",&n);

    if(n%9==0)
        printf("The number entered is a magic number\n");
    else
        printf("The number entered is not a magic number\n");

    return 0;
}

C Program to find the sum of individual digitis of a number till the sum becomes a single digit

/* C Program to find the sum of individual digits of a long integer until the sum becomes
a single digit */

#include <stdio.h>
int main()
{
    long number;
    int sum=0;
    int digit;

    printf("Enter the number\n");
    scanf("%ld",&number);

    while(number>0)
    {
        sum=0;
        while(number>0)
        {
            digit=number%10;
            number=number/10;
            sum=sum+digit;
        }
        if(sum>9)
        {
            number=sum;
        }
    }

    printf("The sum is %d",sum);

    return 0;
}


C Program to find the sum of factors of a given number

/* C Program to find the sum of factors of a given number */

#include <stdio.h>

int main()
{
    int sum=0;
    int ctr;
    int num;

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

    if(num==1)
    {
        sum=1;
    }

    for(ctr=2;ctr<=num;ctr++)
    {
        if((num%ctr)==0)
        {
            num=num/ctr;
            sum=sum+ctr;
            ctr=1;
        }
        if(num==1)
        {
            break;
        }
    }
    printf("The sum of the factors is %d",sum);

    return 0;

}

Friday, December 24, 2010

C Program to write a simple calculator program

/* C Program to write a simple calculator to preform addition, subtraction, multiplication
 and division operations */

 #include <stdio.h>
 #include <stdlib.h>

 int main()
 {
     int num1;
     int num2;
     float result;
     int choice;

     printf("Enter the first number\n");
     scanf("%d",&num1);
     printf("Enter the second number\n");
     scanf("%d",&num2);
     printf("Enter the number of the operation you want to perform on the two entered numbers\n");
     printf("1. Addition\n");
     printf("2. Subtraction\n");
     printf("3. Multiplication\n");
     printf("4. Division\n");
     scanf("%d",&choice);

     switch(choice)
     {
         case 1:
         result=num1+num2;
         printf("Result is %f",result);
         break;

         case 2:
         result=num1-num2;
         printf("Result is %f",result);
         break;

         case 3:
         result=num1*num2;
         printf("Result is %f",result);
         break;

         case 4:
         result=(float)num1/num2;
         printf("Result is %f",result);
         break;

         default:
         printf("Invalid choice\n");
     }

     return 0;
 }

C Program to find the number of digits in a long number

/* C Program to find the number of digits in a given long integer number */

#include <stdio.h>

int main()
{
    long num;
    int count=0;

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

    while(num>0)
    {
        num=num/10;
        count++;
    }

    printf("The number of digits in the give number is %d",count);

    return 0;
}

C Program to find the sum of all digits in a number

/* C Program to find the sum of all digits in a given long number accepted from the user */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long number;
    int sum=0;
    printf("Enter a long number\n");
    scanf("%ld",&number);

    while(number>0)
    {
        sum=sum+(number%10);
        number=number/10;
    }
    printf("The sum of the digits is %d",sum);
    return 0;
}


C Program to find the sum of first n natural numbers

/* C Program to Print the sum of the first N natural numbers. The value of N is accepted from
the user */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N;
    int ctr;
    int sum=0;
    printf("Enter the value of N\n");
    scanf("%d",&N);

    for(ctr=1;ctr<=N;ctr++)
    {
        sum=sum+ctr;
    }
    printf("The sum of the first %d natural numbers is %d",N,sum);
    return 0;
}


C Program to find the factorial of a number

/* C Program to find the factorial of a number accepted from the user */


#include <stdio.h>
int main()
{
    int i;
    int num;
    int fact=1;
    printf("Enter the number\n");
    scanf("%d",&num);
    for(i=num;i>=1;i--)
        fact=fact*i;
    printf("The factorial of the number is %d",fact);
    return 0;
}

C Program to check if a number is a prime number or not

/* C Program to check whether the entered number is prime or not */

#include <stdio.h>

int main()
{
    int num;
    int ctr;
    int count=0;

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

    for(ctr=2;ctr<num;ctr++)
    {
        if((num%ctr)==0)
        {
            count++;
        }
    }

    if(count==0)
    {
        printf("The number is a prime number\n");
    }
    else
    {
        printf("The number is not a prime number\n");
    }

    return 0;
}



C Program to find the Simple Interest

/* C Program to Calculate and Display the Simple Interest of the Principal,
Time in years and the Annual Rate of Interest from user */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int principal;
    int time;
    int rate;
    float simpleinterest;
    printf("Enter the Principal Amount\n");
    scanf("%d",&principal);
    printf("Enter the Time in years\n");
    scanf("%d",&time);
    printf("Enter the Annual Rate of Interest\n");
    scanf("%d",&rate);
    simpleinterest=(principal*rate*time)/100;
    printf("The Simple Interest is %f\n",simpleinterest);
    return 0;
}

C Program to find if the number is an even or odd number

/* C Program to Print whether a number accepted from a user is an odd or an even
number */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    printf("Enter a number\n");
    scanf("%d",&number);
    if((number%2)==0)
    {
        printf("The number is Even\n");
    }
    else
    {
        printf("The number is Odd\n");
    }
    return 0;
}


C Program to find the greater of the 2 numbers

/* C Program to accept two numbers from the user and to print the greater number */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1;
    int num2;
    printf("Enter the First number\n");
    scanf("%d",&num1);
    printf("Enter the Second number\n");
    scanf("%d",&num2);
    if(num1>num2)
    {
        printf("The First number is Greater than the Second number\n");
    }
    else
    {
        if(num1==num2)
        {
            printf("Both the numbers are equal\n");
        }
        else
        {
            printf("The Second number is Greater than the First number\n");
        }
    }
    return 0;
}

C Program to find the sum of 2 numbers

/* C Program to accept 2 numbers from the user and to display their sum */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1;
    int num2;
    int sum;
    printf("Enter the First number\n");
    scanf("%d",&num1);
    printf("Enter the Second number\n");
    scanf("%d",&num2);
    sum=num1+num2;
    printf("The sum of %d and %d is %d\n",num1,num2,sum);
    return 0;
}

C Program to display "Hello world"

/* C Program to print "Hello world!"
#include <stdio.h>      
#include <stdlib.h>


int main()
{
    printf("Hello world!\n");
    return 0;
}