Question 01

Write a C program that generates a multiplication table up to 10x10 using nested for loops. Each cell in the table should display the product of the row and column numbers.

Answer 01

#include <stdio.h>

void main() 
{
    for (int i = 1; i <= 10; i++) 
    {
        for (int j = 1; j <= 10; j++) 
        {
            printf("%d\t", i * j);
        }
        printf("\n");
    }
}

Question 02

Write a C program that prints a right-aligned triangle of asterisks (*). The triangle should have 5 rows, with the first row containing 1 asterisk, the second row containing 2 asterisks, and so on, up to the fifth row, which should contain 5 asterisks.

For example, if the number of rows is 5, the output should be:

    *
   **
  ***
 ****
*****

If the number of rows is 6, the output should be:

     *
    **
   ***
  ****
 *****
******

Answer 02

#include <stdio.h>

void main() 
{
    int rows = 5;

    for (int i = 1; i <= rows; i++) 
    {
        // Print spaces for alignment
        for (int j = 1; j <= rows - i; j++) 
        {
            printf(" ");
        }
        // Print asterisks
        for (int k = 1; k <= i; k++) 
        {
            printf("*");
        }
        printf("\n");
    }
}

Question 03

Write a C program that displays a pyramid of numbers. The pyramid should have 5 rows, with each row displaying the row number repeated. The first row should contain 1, the second row should contain 2 twice, and so on, up to the fifth row, which contains the number 5 five times.

The output should be:

    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5

Answer 03

#include <stdio.h>

void main() 
{
    int rows = 5;

    for (int i = 1; i <= rows; i++) 
    {
        // Print spaces for alignment
        for (int j = 1; j <= rows - i; j++) 
        {
            printf(" ");
        }
        // Print numbers
        for (int k = 1; k <= i; k++) 
        {
            printf("%d ", i);
        }
        printf("\n");
    }
}

Question 04

Write a C program that generates a checkerboard pattern of ‘*‘ and ‘#‘ characters. The pattern should be 8x8, with alternating symbols in each row, so that it resembles a checkerboard. The output should be:

* # * # * # * # 
# * # * # * # * 
* # * # * # * # 
# * # * # * # * 
* # * # * # * # 
# * # * # * # * 
* # * # * # * # 
# * # * # * # * 

Answer 04

#include <stdio.h>

void main() 
{
    int size = 8;

    for (int i = 0; i < size; i++) 
    {
        for (int j = 0; j < size; j++) 
        {
            if ((i + j) % 2 == 0) 
            {
                printf("* ");
            } 
            else 
            {
                printf("# ");
            }
        }
        printf("\n");
    }
}

Question 05

Write a C program that finds and prints all pairs of numbers in an array whose sum is equal to a given target value.

For example, if the array is {1, 2, 3, 4, 5, 6, 7}, and the target value is 7, the output should be:

Pairs with sum 7:
(1, 6)
(2, 5)
(3, 4)

Answer 05

#include <stdio.h>

void main() 
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7};
    int target = 7;
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Pairs with sum %d:\n", target);
    for (int i = 0; i < n; i++) 
    {
        for (int j = i + 1; j < n; j++) 
        {
            if (arr[i] + arr[j] == target) 
            {
                printf("(%d, %d)\n", arr[i], arr[j]);
            }
        }
    }
}
Last modified: Friday, 22 November 2024, 4:45 PM