Some Basic C programs to practice – Part 2

Himamshu Bhattarai
13 Min Read
Contents: Click for quick access
1. Write a program in C to input a number and find the number is odd or even.2. Write a program in C to input a number and find the number is divisible by 7 or not.3. Write a program in C to input a number and find the number is divisible by 3 and 5 or not.4. Write a program in C to input two numbers and find the smallest number.5. Write a program in C to input two numbers and find the greatest number.6. Write a program in C to input three numbers and find the smallest number.7. Write a program in C to input three numbers and find the greatest number. 8 .Write a program in C to input four numbers and find the greatest number.9. Write a program in C to input four numbers and find the smallest number. 10. Write a program in C to input a number and find the number is zero, negative or positive.11. Write a program in C to input two numbers and find the sum if the first number is greater than the second number. Otherwise find their product.Switch Case Break Statements12. Input two different numbers and print the sum, product, difference and  remainder of two numbers using switch case statements.13) Using switch case print the days of a week.14.  Using switch case print the months of a year.15.Write a program in C to print the numbers from 1 to 10.16. Write a program in C to print the numbers from 10 to 1.17.) Write a program in C to print the even numbers from 2 to 50.18. Write a program in C to print the odd numbers from 1 to 25. 19.  Write a program in C to print the even numbers from 2 to 50 using the continue statement.20. Write a program in C to print the odd numbers from 1 to 25 using continue statement.21. Write a program in C to print the sum of numbers from 1 to 10.22. Write a program in C to print the sum of even numbers from 2 to 50.23. Write a program in C to print the sum of odd numbers from 1 to 25.24.  Write a program in C to input a number and print the sum of 1 to N. 25. Write a program in C to print the factorial of a number.26.  Write a program in C to print the factors of an input number.

1. Write a program in C to input a number and find the number is odd or even.

#include <stdio.h>

int main() {
    int num;


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

 
    for (int i = 0; i < 1; i++) {
        if (num % 2 == 0) {
            printf("%d is even.\n", num);
        } else {
            printf("%d is odd.\n", num);
        }
    }

    return 0;
}

2. Write a program in C to input a number and find the number is divisible by 7 or not.

#include <stdio.h>

int main() {
    int num;

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

    if (num % 7 == 0) {
        printf("%d is divisible by 7.\n", num);
    } else {
        printf("%d is not divisible by 7.\n", num);
    }

    return 0;
}

3. Write a program in C to input a number and find the number is divisible by 3 and 5 or not.

#include <stdio.h>

int main() {
    int num;

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

    if (num % 3 == 0 && num % 5 == 0) {
        printf("%d is divisible by both 3 and 5.\n", num);
    } else {
        printf("%d is not divisible by both 3 and 5.\n", num);
    }

    return 0;
}

4. Write a program in C to input two numbers and find the smallest number.

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    if (num1 < num2) {
        printf("The smallest number is: %d\n", num1);
    } else {
        printf("The smallest number is: %d\n", num2);
    }

    return 0;
}

5. Write a program in C to input two numbers and find the greatest number.

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    if (num1 > num2) {
        printf("The greatest number is: %d\n", num1);
    } else {
        printf("The greatest number is: %d\n", num2);
    }

    return 0;
}

6. Write a program in C to input three numbers and find the smallest number.

#include <stdio.h>

int main() {
    int num1, num2, num3;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    printf("Enter the third number: ");
    scanf("%d", &num3);

    int smallest = num1;

    if (num2 < smallest) {
        smallest = num2;
    }

    if (num3 < smallest) {
        smallest = num3;
    }
   printf("The smallest number is: %d\n", smallest);

    return 0;
}

7. Write a program in C to input three numbers and find the greatest number. 

#include <stdio.h>

int main() {
    double num1, num2, num3;

    printf("Enter the first number: ");
    scanf("%lf", &num1);

    printf("Enter the second number: ");
    scanf("%lf", &num2);

    printf("Enter the third number: ");
    scanf("%lf", &num3);

    if (num1 >= num2 && num1 >= num3) {
        printf("The greatest number is: %.2lf\n", num1);
    } else if (num2 >= num1 && num2 >= num3) {
        printf("The greatest number is: %.2lf\n", num2);
    } else {
        printf("The greatest number is: %.2lf\n", num3);
    }

    return 0;
}

8 .Write a program in C to input four numbers and find the greatest number.

#include <stdio.h>

int main() {
    int num1, num2, num3, num4;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    printf("Enter the third number: ");
    scanf("%d", &num3);

    printf("Enter the fourth number: ");
    scanf("%d", &num4);

    int greatest = num1;

    if (num2 > greatest) {
        greatest = num2;
    }

    if (num3 > greatest) {
        greatest = num3;
    }

    if (num4 > greatest) {
        greatest = num4;
    }

    printf("The greatest number is: %d\n", greatest);

    return 0;
}

9. Write a program in C to input four numbers and find the smallest number. 

#include <stdio.h>

int main() {
    int num1, num2, num3, num4;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    printf("Enter the third number: ");
    scanf("%d", &num3);

    printf("Enter the fourth number: ");
    scanf("%d", &num4);

    int smallest = num1;

    if (num2 < smallest) {
        smallest = num2;
    }

    if (num3 < smallest) {
        smallest = num3;
    }

    if (num4 < smallest) {
        smallest = num4;
    }

    printf("The smallest number is: %d\n", smallest);

    return 0;
}

10. Write a program in C to input a number and find the number is zero, negative or positive.

#include <stdio.h>

int main() {
    int number;

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

    if (number == 0) {
        printf("The number is zero.\n");
    } else if (number < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is positive.\n");
    }

    return 0;
}

11. Write a program in C to input two numbers and find the sum if the first number is greater than the second number. Otherwise find their product.

#include <stdio.h>

int main() {
    int num1, num2, result;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    if (num1 > num2) {
        result = num1 + num2;
        printf("Sum: %d\n", result);
    } else {
        result = num1 * num2;
        printf("Product: %d\n", result);
    }

    return 0;
}

Switch Case Break Statements

12. Input two different numbers and print the sum, product, difference and  remainder of two numbers using switch case statements.

#include <stdio.h>

int main() {
    int num1, num2;
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);

    int choice;
    printf("Choose an operation:\n");
    printf("1. Sum\n");
    printf("2. Product\n");
    printf("3. Difference\n");
    printf("4. Remainder\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Sum: %d\n", num1 + num2);
            break;
        case 2:
            printf("Product: %d\n", num1 * num2);
            break;
        case 3:
            printf("Difference: %d\n", num1 - num2);
            break;
        case 4:
            if (num2 != 0) {
                printf("Remainder: %d\n", num1 % num2);
            } else {
                printf("Cannot divide by zero.\n");
            }
            break;
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}

13) Using switch case print the days of a week.

#include <stdio.h>

int main() {
    int day;
    printf("Enter a number (1-7): ");
    scanf("%d", &day);

    switch (day) {
        case 1:
            printf("Sunday\n");
            break;
        case 2:
            printf("Monday\n");
            break;
        case 3:
            printf("Tuesday\n");
            break;
        case 4:
            printf("Wednesday\n");
            break;
        case 5:
            printf("Thursday\n");
            break;
        case 6:
            printf("Friday\n");
            break;
        case 7:
            printf("Saturday\n");
            break;
        default:
            printf("Invalid day number.\n");
    }

    return 0;
}

14.  Using switch case print the months of a year.

#include <stdio.h>

int main() {
    int month;
    printf("Enter a number (1-12): ");
    scanf("%d", &month);

    switch (month) {
        case 1:
            printf("January\n");
            break;
        case 2:
            printf("February\n");
            break;
        case 3:
            printf("March\n");
            break;
        case 4:
            printf("April\n");
            break;
        case 5:
            printf("May\n");
            break;
        case 6:
            printf("June\n");
            break;
        case 7:
            printf("July\n");
            break;
        case 8:
            printf("August\n");
            break;
        case 9:
            printf("September\n");
            break;
        case 10:
            printf("October\n");
            break;
        case 11:
            printf("November\n");
            break;
        case 12:
            printf("December\n");
            break;
        default:
            printf("Invalid month number.\n");
    }

    return 0;
}

15.Write a program in C to print the numbers from 1 to 10.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; ++i) {
        printf("%d ", i);
    }

    return 0;
}

16. Write a program in C to print the numbers from 10 to 1.

#include <stdio.h>

int main() {
    for (int i = 10; i >= 1; --i) {
        printf("%d ", i);
    }

    return 0;
}

17.) Write a program in C to print the even numbers from 2 to 50.

#include <stdio.h>

int main() {
    for (int i = 2; i <= 50; i += 2) {
        printf("%d ", i);
    }

    return 0;
}

18. Write a program in C to print the odd numbers from 1 to 25. 

#include <stdio.h>

int main() {
    for (int i = 1; i <= 25; i += 2) {
        printf("%d ", i);
    }

    return 0;
}

19.  Write a program in C to print the even numbers from 2 to 50 using the continue statement.

#include <stdio.h>

int main() {
    for (int i = 2; i <= 50; ++i) {
        if (i % 2 != 0) {
            continue;
        }
        printf("%d ", i);
    }

    return 0;
}

20. Write a program in C to print the odd numbers from 1 to 25 using continue statement.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 25; ++i) {
        if (i % 2 == 0) {
            continue;
        }
        printf("%d ", i);
    }

    return 0;
}

21. Write a program in C to print the sum of numbers from 1 to 10.

#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 1; i <= 10; ++i) {
        sum += i;
    }
    printf("Sum: %d\n", sum);

    return 0;
}

22. Write a program in C to print the sum of even numbers from 2 to 50.

#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 2; i <= 50; i += 2) {
        sum += i;
    }
    printf("Sum: %d\n", sum);

    return 0;
}

23. Write a program in C to print the sum of odd numbers from 1 to 25.

#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 1; i <= 25; i += 2) {
        sum += i;
    }
    printf("Sum: %d\n", sum);

    return 0;
}

24.  Write a program in C to input a number and print the sum of 1 to N. 

#include <stdio.h>

int main() {
    int N, sum = 0;

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

    for (int i = 1; i <= N; ++i) {
        sum += i;
    }

    printf("Sum: %d\n", sum);

    return 0;
}

25. Write a program in C to print the factorial of a number.

#include <stdio.h>

int main() {
    int num, factorial = 1;

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

    for (int i = 1; i <= num; ++i) {
        factorial *= i;
    }

    printf("Factorial: %d\n", factorial);

    return 0;
}

26.  Write a program in C to print the factors of an input number.

#include <stdio.h>

int main() {
    int num;

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

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

    return 0;
}

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *