C Programs | Top 10 C Programming Examples with Output



C Programs

In this page you learn important C programming examples with its explanation and output, It contains C programs example with output which improves the logic and help in generating ideas to solve C programming problems. Various types of C programming example are discussed below with its complete explanation ans its output. 


C programming examples/C programming examples with output
C programs




    Q 1. Write a C program to find factorial of number given by user. 


    int main()
    {
        int fact=1,num,temp;
        printf("Enter the Positive Integer:\n");
        scanf("%d",&num);
        temp=num;
        while(num>1)
        {
            fact=fact*num;
            num--;
        }
        printf("Factorial of %d is %d",temp,fact);
        return 0;
    }
    
    Enter the Positive Integer:
    6
    Factorial of 6 is 720



    Q 2. Write a C program to swap two numbers using third variable. 


    int main()
    {
        int a,b,c;
        printf("Enter two value to swap:\n");
        scanf("%d%d",&a,&b);
        printf("\nEntered numbers are a=%d\tb=%d\n\n",a,b);
        c=a;
        a=b;
        b=c;
        printf("Entered number after swapping are a=%d\tb=%d\n",a,b);
        return 0;
    }
    
    Enter two value to swap:
    9
    6
    Entered numbers are a=9 b=6
    Entered number after swapping are a=6    b=9



    Q 3. Write a C program to determine whether the year is leap or not. 


    int main()
    {
        int yr;
        printf("Enter the year to check the year is leap or not:\n");
        scanf("%d",&yr);
        if(yr%400==0)
            printf("Entered year %d is the leap year",yr);
        else if(yr%4==0&&yr%100!=0)
            printf("Entered year %d is the leap year",yr);
        else
            printf("Entered year %d is not the leap year",yr);
        return 0;
    }
    
    Enter the year to check the year is leap or not:
    2020
    Entered year 2020 is the leap year



    Q 4. Write a C program to swap two numbers without using third variable. 


    int main()
    {
        int a,b;
        printf("Enter two numbers to swap:\n");
        scanf("%d%d",&a,&b);
        printf("\nEntered numbers are a=%d and b=%d\n\n",a,b);
        a=a+b;
        b=a-b;
        a=a-b;
        printf("Entered number after swapping are a=%d and b=%d\n",a,b);
        return 0;
    }
    
    Enter two numbers to swap:
    5
    6
    Entered numbers are a=5 and b=6
    Entered number after swapping are a=6 and b=5



    Q 5. Write a C program to determine whether number is Even or Odd. 


    int main()
    {
        int n;
        printf("Enter the number to check whether the number is even or odd:\n");
        scanf("%d",&n);
        if(n%2==0)
            printf("Entered number %d is even",n);
        else
            printf("Entered number %d is odd",n);
        return 0;
    }
    
    Enter the number to check whether the number is even or odd:
    7
    Entered number 7 is odd



    Q 6. Write a C program to determine whether number is prime or not. 


    //Note: "1 is a prime number"
    int main()
    {
        int n,i,temp,count=0;
        printf("Enter any number to check whether it is prime or not:\n");
        scanf("%d",&n);
        temp=n;
        for(i=1;i<=n;i++)
        {
            if(n%i==0)
                count++;
        }
        if(count==2)
            printf("Entered number %d is a prime",temp);
        else
            printf("Entered number %d is not a prime",temp);
        return 0;
    }
    
    Enter any number to check whether it is prime or not:
    7
    Entered number 7 is a prime



    Q 7. Write a C program to determine whether number is Palindrome or not. 


    //Note: "Palindrome no. is a no. which reverse no. is always equal to its original no." 
    int main()
    {
        int n,r,temp,sum=0;
        printf("Enter valid number to check whether it is palindrome or not:\n");
        scanf("%d",&n);
        temp=n;
        while(n>0)
        {
            r=n%10;
            sum=sum*10+r;
            n=n/10;
        }
        n=temp;
        if(n==sum)
            printf("Entered number %d is palindrome",n);
        else
            printf("Entered number %d is not palindrome",n);
        return 0;
    }
    
    
    Enter valid number to check whether it is palindrome or not:
    121
    Entered number 121 is palindrome



    Q 8. Write a C program to determine the reverse of entered number by user. 


    int main()
    {
        int n,r,temp,sum=0;
        printf("Enter valid number to find its reverse number:\n");
        scanf("%d",&n);
        temp=n;
        while(n>0)
        {
            r=n%10;
            sum=sum*10+r;
            n=n/10;
        }
        n=temp;
        printf("Reverse of entered no. %d is %d",n,sum);
        return 0;
    }
    
    
    Enter valid number to find its reverse number:
    1976
    Reverse of entered no. 1976 is 6791



    Q 9. Write a C program to determine the sum of the digit of the entered number by user. 


    int main()
    {
        int n,r,temp,sum=0;
        printf("Enter valid number to find the sum of the digit:\n");
        scanf("%d",&n);
        temp=n;
        while(n>0)
        {
            r=n%10;
            sum=sum+r;
            n=n/10;
        }
        n=temp;
        printf("Sum of the digit of entered no. %d is %d",n,sum);
        return 0;
    }
    
    Enter valid number to find the sum of the digit:
    85456
    Sum of the digit of entered no. 85456 is 28



    Q 10. Write a C program to convert Temperature in Fahrenheit degrees into Centigrade degrees. 


    int main()
    {
        float tf, tc;
        printf("Enter temperature in Fahrenheit:\n");
        scanf("%f", &tf);
        tc = ( tf-32 ) / 1.8;
        printf("Temperature in Centigrade is %f", tc);
        return 0;
    }
    
    Enter temperature in Fahrenheit:
    100
    Temperature in Centigrade is 37.777779

    Conclusion:

    In this page you learn 10 different types of C programming example with its explanation of code and output. You learn the code of 1. factorial, 2. swap of two number, 3. determine whether leap year or not, 4. swap of two number without using third variable, 5. determine whether the number is even or odd, 6. determine whether the number is prime or not, 7. determine whether the number is palindrome or not, 8. determine reverse of a number, 9. determine sum of the digit of the number, 10. converting Fahrenheit degrees into Centigrade degrees. These are the C programs which boost your solving skills of these types C programming problems.

    If you are interested then you can also read:



    If you find this article helpful then please share!


    Tags: C ProgramsC programs example with output, C programming example, C programming basics, C programming problems for beginners


    1 comment:

    1. As for me, C++ programming language was much easier for me, when I was studying at the university. Although, it depends on a person.

      ReplyDelete

    Powered by Blogger.