Chapter 4 g 8

Ch 4  

Program 1: For Loop for Multiplication table 

#include <stdio.h>


int main() {

    int num, i;  // Ask user to enter a number

    printf("Enter a number: ");

    scanf("%d", &num);  // Print multiplication table

    printf("Multiplication table of %d:\n", num);

    for(i = 1; i <= 10; i++) {

        printf("%d x %d = %d\n", num, i, num * i);

    }

    return 0;

}

Program 2: Even Numbers 

#include <stdio.h>

int main() {
    int range, i = 2;
// Ask user for range
    printf("Enter the range of even numbers you want: ");
    scanf("%d", &range);

    printf("Even numbers from 1 to %d are:\n", range);

    // While loop to display even numbers
    while (i <= range) {
        printf("%d\n", i);
        i = i + 2; // Move to next even number
    }

    return 0;
}

Program 3: odd numbers 
#include <stdio.h>

int main() {
    int range, i = 1;

    // Ask user for range
    printf("Enter the range of odd numbers you want: ");
    scanf("%d", &range);

    printf("Odd numbers from 1 to %d are:\n", range);

    // While loop to display odd numbers
    while (i <= range) {
        printf("%d\n", i);
        i = i + 2; // Move to next odd number
    }

    return 0;
}

Program 4: Array Numbers 
#include <stdio.h>

int main() {
    int a[11];  // Array to store 11 numbers
    int i;

    // Store numbers in the array
    for(i = 0; i <= 10; i++) {
        a[i] = 4 * (i + 1);  // a[0]=4, a[1]=8, ..., a[10]=44
    }

    // Display numbers
    printf("The numbers in the array are:\n");
    for(i = 0; i <= 10; i++) {
        printf("a[%d] = %d\n", i, a[i]);
    }

    return 0;
}

Program 5: Multi Dimensional Array 
#include <stdio.h>

int main() {
    int a[3][4];  // 3 rows, 4 columns
    int i, j;
    int value = 4; // Starting value

    // Fill the 2D array
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 4; j++) {
            a[i][j] = value;
            value += 4; // Increment by 4
        }
    }

    // Display the 2D array
    printf("The 2D array is:\n");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 4; j++) {
            printf("a[%d][%d] = %d\n", i, j, a[i][j]);
        }
    }

    return 0;
}

Comments

Popular posts from this blog

Class 9 Part B Term 2

class 6 Term2

NCERT GRADE 10