Problem Solving using C Language

0 of 57 lessons complete (0%)

Loops

Lab: for Loop

Program 1: Print Multiplication Table

Objective: Print the multiplication table of a number entered by the user.

#include <stdio.h>

void main() {
    int num,i,result;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    for (i = 1; i <= 10; i++) {
        result=num*i;
        printf("%d x %d = %d\n", num, i, result);
    }
}

Program 2: Print First N Natural Numbers

Objective: Input a number N and print the first N natural numbers.

#include <stdio.h>

void main() {
    int N;

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

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


}

Program 3: Print the Sum of the First N Natural Numbers

Objective: Input a number N and find the sum of all natural numbers from 1 to N

#include <stdio.h>

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

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

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

    printf("Sum of first %d natural numbers is: %d\n", N, sum);
    
}

Program 4: Sum of Digits of a Number

Objective: Input a number and find the sum of its digits.

#include <stdio.h>

void main() {
    int num, sum = 0, remainder;

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

    for (; num != 0; num /= 10) {
        remainder = num % 10;
        sum += remainder;
    }

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

}

Program 4: Sum of Even and Odd Numbers from 1 to N

Objective: Input a number N and find the sum of all even and odd numbers separately from 1 to N.

#include <stdio.h>

int main() {
    int N, sumEven = 0, sumOdd = 0;

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

    for (int i = 1; i <= N; i++) {
        if (i % 2 == 0) {
            sumEven += i;
        } 
        else 
        {
            sumOdd += i;
        }
    }
    printf("Sum of even numbers: %d\n", sumEven);
    printf("Sum of odd numbers: %d\n", sumOdd);
    return 0;
}

Complete Rest of the Programs

  1. Print first N natural numbers.
  2. Sum of first N natural numbers.
  3. Multiplication table.
  4. Find the factorial of a number.
  5. Fibonacci series.
  6. Check for a prime number.
  7. Reverse a number.
  8. Sum of digits.
  9. Print patterns.
  10. Sum of even and odd numbers from 1 to N.