Number-Based C Programs for Beginners

Welcome, aspiring programmers! 🚀 This tutorial will take you on a trip to use the C programming language to explain the world of numbers. If you’re just getting started, don’t worry; we’ll break down complex concepts into bite-sized pieces. This series is your first step into the fascinating world of number-based C programming, covering everything from fundamental arithmetic operations to managing larger numbers. Prepare to learn from real-world situations, acquire practical experience, and develop a strong foundation in coding. By the conclusion, you’ll have a strong understanding of the principles and enjoy creating your own C numerical solutions! 🌐💻

Number based c programs

1. Write a program to calculate the factorial of a given number.

The factorial of a given number is the product of all positive integers up to that number. It is denoted by the exclamation mark (!). For example, the factorial of 5 is written as 5! and is calculated as:

5! = 5×4×3×2×1 = 120

In general, the factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

n! = n×(n−1)! for n>1

Program 1: Using Loop

#include <stdio.h>

int main() {
    // Declare variables
    int number, i;
    unsigned long long factorial = 1;

    // Input: Get the number from the user
    printf("Enter a positive integer: ");
    scanf("%d", &number);

    // Check if the number is negative
    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        // Calculate factorial using a loop
        for (i = 1; i <= number; ++i) {
            factorial *= i;
        }

        // Output: Display the factorial
        printf("Factorial of %d = %llu\n", number, factorial);
    }

    return 0;
}

Output :

Enter a positive integer: 5
Factorial of 5 = 120

Program 1: Using Recursion

#include <stdio.h>

// Function to calculate factorial using recursion
int factorial(int n) {
    // Base case: factorial of 0 is 1
    if (n == 0 || n == 1) {
        return 1;
    } else {
        // Recursive case: n! = n * (n-1)!
        return n * factorial(n - 1);
    }
}

int main() {
    // Input from the user
    int num;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);

    // Check if the input is non-negative
    if (num < 0) {
        printf("Please enter a non-negative integer.\n");
    } else {
        // Calculate and display the factorial
        printf("Factorial of %d = %d\n", num, factorial(num));
    }

    return 0;
}

Output :

Enter a non-negative integer: 5
Factorial of 5 = 120

2. Write C program to determine if a given number is prime or not

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. In other words, a prime number has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers because they cannot be divided evenly by any other numbers except 1 and the number itself.

#include <stdio.h>

// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) {
        return 0;  // 0 and 1 are not prime numbers
    }
    for (int i = 2; i * i <= num; ++i) {
        if (num % i == 0) {
            return 0;  // Not a prime number
        }
    }
    return 1;  // Prime number
}

int main() {
    int number;

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

    // Check if the number is prime
    if (isPrime(number)) {
        printf("%d is a prime number.\n", number);
    } else {
        printf("%d is not a prime number.\n", number);
    }

    return 0;
}

Output :

Enter a number: 13
13 is a prime number.

Enter a number: 25
25 is not a prime number.

3. Write a C program to check if a number is a palindrome or not.

A palindrome number is one that remains same when its digits are reversed. In other words, it reads the same backward and forward. For example, the numbers 121, 1331, and 1221 are palindromes because, when read in reverse order, their digits stay the same.

#include <stdio.h>

int main() {
    int originalNumber, reversedNumber = 0, remainder, temp;

    // Input a number from the user
    printf("Enter a number: ");
    scanf("%d", &originalNumber);

    temp = originalNumber;  // Store the original number in a temporary variable

    // Reverse the digits of the number
    while (temp != 0) {
        remainder = temp % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        temp /= 10;
    }

    // Check if the original number is equal to its reversed version
    if (originalNumber == reversedNumber) {
        printf("%d is a palindrome number.\n", originalNumber);
    } else {
        printf("%d is not a palindrome number.\n", originalNumber);
    }

    return 0;
}

Output :

Enter a number: 121
121 is a palindrome number.

4. Write C program to check if a number is an Armstrong number.

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example, let’s take the 3-digit number 153:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

The general equation for an n-digit Armstrong number is:
a1n + a2n + …………….. + ann = Number

Where:

  • a1,a2,……….,an are the individual digits of the number.
  • n is the number of digits in the number.
#include <stdio.h>
#include <math.h>

// Function to calculate the number of digits in a given number
int countDigits(int num) {
    int count = 0;
    while (num != 0) {
        num /= 10;
        count++;
    }
    return count;
}

// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
    int originalNum = num;
    int sum = 0;
    int power = countDigits(num);

    while (num != 0) {
        int digit = num % 10;
        sum += pow(digit, power);
        num /= 10;
    }

    return (sum == originalNum);
}

int main() {
    int number;

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

    // Check if the number is Armstrong
    if (isArmstrong(number)) {
        printf("%d is an Armstrong number.\n", number);
    } else {
        printf("%d is not an Armstrong number.\n", number);
    }

    return 0;
}

Output :

Enter a number: 153
153 is an Armstrong number.

5. Write C program to Print Armstrong Numbers upto N.

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

// Function to calculate the number of digits in a given number
int countDigits(int num) {
    int count = 0;
    while (num != 0) {
        num /= 10;
        count++;
    }
    return count;
}

// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
    int originalNum = num;
    int n = countDigits(num);
    int sum = 0;

    while (num != 0) {
        int digit = num % 10;
        sum += pow(digit, n);
        num /= 10;
    }

    return (sum == originalNum);
}

int main() {
    int N;

    // Input the value of N
    printf("Enter the value of N: ");
    scanf("%d", &N);

    printf("Armstrong numbers up to %d are:\n", N);

    // Iterate through numbers up to N and check if they are Armstrong
    for (int i = 1; i <= N; i++) {
        if (isArmstrong(i)) {
            printf("%d\t", i);
        }
    }

    return 0;
}

Output :

Enter the value of N: 1000
Armstrong numbers up to 1000 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

6. Write program to generate the Fibonacci series up to a given number.

#include <stdio.h>

int main() {
    int n, firstTerm = 0, secondTerm = 1, nextTerm;

    // Input the number of terms
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series up to %d terms: ", n);

    for (int i = 0; i < n; ++i) {
        if (i <= 1) {
            nextTerm = i;
        } else {
            nextTerm = firstTerm + secondTerm;
            firstTerm = secondTerm;
            secondTerm = nextTerm;
        }
        printf("%d ", nextTerm);
    }

    return 0;
}

Output :

Enter the number of terms: 10
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34 

7. Write program to find the sum of digits of a given number.

#include <stdio.h>

int main() {
    // Declare variables
    int number, originalNumber, sum = 0, digit;

    // Input: Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Save the original number for reference
    originalNumber = number;

    // Calculate the sum of digits
    while (number != 0) {

        // Extract the last digit
        digit = number % 10;
        
        // Add the digit to the sum
        sum += digit;

        // Remove the last digit from the number
        number /= 10;
    }

    // Output: Display the result
    printf("The sum of digits of %d is: %d\n", originalNumber, sum);

    return 0;
}

Output :

Enter a number: 12345
The sum of digits of 12345 is: 15

8. Write a program to reverse the digits of a given number.

#include <stdio.h>

int main() {
    // Declare variables
    int number, reversedNumber = 0, remainder;

    // Get input from the user
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Reverse the digits
    while (number != 0) {
        remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }

    // Display the reversed number
    printf("Reversed Number: %d\n", reversedNumber);

    return 0;
}

Output :

Enter an integer: 125
Reversed Number: 521