C Program to Display Factors of a Number

Source code:

#include <stdio.h>
int main()
{
    int number, i;

    printf("Enter a positive integer: ");
    scanf("%d",&number);

    printf("Factors of %d are: ", number);
    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            printf("%d ",i);
        }
    }

    return 0;
}

Output:


Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60

C Program to Check Whether a Number is Prime or Not

Source code:

#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    
    return 0;
}

Output:

Enter a positive integer: 29
29 is a prime number.

C Program to Check Leap Year

Source code:

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year: ");
    scanf("%d",&year);

    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            if ( year%400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
    }
    else
        printf("%d is not a leap year.", year);
    
    return 0;
}

Output 1:

Enter a year: 1900
1900 is not a leap year.

Output 2:

Enter a year: 2012
2012 is a leap year.

C Program to Calculate Area and Circumference of circle

Source Code:

#include<stdio.h>

int main() {

   int rad;
   float PI = 3.14, area, ci;

   printf("\nEnter radius of circle: ");
   scanf("%d", &rad);

   area = PI * rad * rad;
   printf("\nArea of circle : %f ", area);

   ci = 2 * PI * rad;
   printf("\nCircumference : %f ", ci);

   return (0);
}

Output:

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Introduction Examples

Program to Display "Hello, World!"

#include <stdio.h>
#include <conio.h>
void main()
{
    // printf() displays the string inside quotation
    printf("tutorialsforcprogramming.blogspot.com");
    getch();
}

Output

tutorialsforcprogramming.blogspot.com

Get differnet value from user & print it on diaplay

#include <stdio.h>
#include <conio.h>
void main()
{
    int in;
    float fn;
    char c; 

    printf("Enter an integer: ");  
    scanf("%d", &in);                //  %d is used for get or print integer
    printf("You entered: %d", in);

    printf("Enter an float: ");  
    scanf("%f", &fn);                //  %f is used for get or print float
    printf("You entered: %f", fn);

    printf("Enter an character: ");  //  %c is used for get or print character
    scanf("%c", &c);                 
    printf("You entered: %c", c);

    getch();
}

Output

Enter an integer: 5
You entered: 5
Enter an float: 5.005
You entered: 5.005
Enter an character: D
You entered: D

Perform some operation on integer

#include <stdio.h>
#include <conio.h>
void main()
{
    int a=5;b=10,c;
    c = b + a;
    printf("b + a = %d", c);

    c = b - a;
    printf("b - a = %d", c);

    c = b * a;
    printf("b * a = %d", c);

    c = b / a;
    printf("b / a = %d", c);

}

Output

b + a = 15
b - a = 5
b * a = 50
b / a = 2