Question 01

Write a C program that defines a function called max_of_three which takes three integers as parameters and returns the largest of the three integers. The program should prompt the user to enter three integers, call the max_of_three function, and display the result.

Answer 01

#include <stdio.h>

// Function declaration
int max_of_three(int a, int b, int c);

int main() {
    int x, y, z;
    
    // Get input from the user
    printf("Enter three integers: ");
    scanf("%d %d %d", &x, &y, &z);

    // Call the function and display the result
    int max = max_of_three(x, y, z);
    printf("The largest number is: %d\n", max);

    return 0;
}

// Function definition
int max_of_three(int a, int b, int c) {
    if (a >= b && a >= c) {
        return a;
    } else if (b >= a && b >= c) {
        return b;
    } else {
        return c;
    }
}

Question 02

Write a C program that defines a function called gcd which calculates the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. The program should prompt the user to enter two integers, call the gcd function, and display the result.

Answer 02

#include <stdio.h>

// Function declaration
int gcd(int a, int b);

int main() {
    int num1, num2;
    
    // Get input from the user
    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);

    // Call the function and display the result
    printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));

    return 0;
}

// Function definition (Euclidean algorithm)
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

Question 03

Write a C program that defines a function called sum_of_digits which calculates the sum of the digits of a given positive integer. The program should prompt the user to enter a number, call the sum_of_digits function, and display the sum of its digits.

Answer 03

#include <stdio.h>

// Function declaration
int sum_of_digits(int n);

int main() {
    int num;
    
    // Get input from the user
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    // Call the function and display the result
    printf("The sum of the digits of %d is: %d\n", num, sum_of_digits(num));

    return 0;
}

// Function definition
int sum_of_digits(int n) {
    int sum = 0;
    while (n > 0) {
        sum += n % 10;  // Add the last digit to the sum
        n /= 10;         // Remove the last digit
    }
    return sum;
}

Question 04

Write a C program that defines two functions:

  1. A function is_even which takes an integer and returns 1 if the number is even and 0 if the number is odd.

  2. A function print_even_or_odd that calls is_even and prints Even if the number is even or Odd if the number is odd.

The program should prompt the user to enter an integer, call print_even_or_odd, and display whether the number is even or odd.

Answer 04

#include <stdio.h>

// Function declarations
int is_even(int n);
void print_even_or_odd(int n);

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

    // Call the function to print whether the number is even or odd
    print_even_or_odd(num);

    return 0;
}

// Function to check if the number is even
int is_even(int n) {
    return n % 2 == 0;  // Return 1 if even, 0 if odd
}

// Function to print if the number is even or odd
void print_even_or_odd(int n) {
    if (is_even(n)) {
        printf("%d is Even\n", n);
    } else {
        printf("%d is Odd\n", n);
    }
}

Question 05

Write a C program that defines three functions:

  1. A function is_positive which checks if an integer is positive (returns 1 if positive, 0 otherwise).

  2. A function is_negative which checks if an integer is negative (returns 1 if negative, 0 otherwise).

  3. A function check_number that calls both is_positive and is_negative to determine if the number is positive, negative, or zero, and prints the appropriate message.

The program should prompt the user to enter an integer, call the check_number function, and display whether the number is positive, negative, or zero.

Answer 05

#include <stdio.h>

// Function declarations
int is_positive(int n);
int is_negative(int n);
void check_number(int n);

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

    // Call the check_number function
    check_number(num);

    return 0;
}

// Function to check if the number is positive
int is_positive(int n) {
    return n > 0;
}

// Function to check if the number is negative
int is_negative(int n) {
    return n < 0;
}

// Function to check the number and print the result
void check_number(int n) {
    if (is_positive(n)) {
        printf("%d is positive.\n", n);
    } else if (is_negative(n)) {
        printf("%d is negative.\n", n);
    } else {
        printf("%d is zero.\n", n);
    }
}
Last modified: Wednesday, 11 December 2024, 8:36 AM