Best Of LK !

Q1

In C programming, both do...while and while loops are used for repetitive execution of a block of code based on a condition. The primary difference between them lies in when the condition is evaluated.

  • while loop: It first evaluates the condition. If the condition is true, it enters the loop and executes the code block. If the condition is false initially, the code inside the loop won’t execute at all.
while (condition) {
    // code block
}
  • do...while loop: This loop structure first executes the code block and then checks the condition. This guarantees that the code block is executed at least once, even if the condition is initially false.

Q2

Recursion is a programming technique where a function calls itself. It’s like breaking a big problem into smaller versions of the same problem until it’s simple enough to solve directly.

Here’s a short C program using recursion to calculate factorial:

#include <stdio.h>

unsigned int factorial(unsigned int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    unsigned int number = 5;
    printf("Factorial of %u is %u\n", number, factorial(number));
    return 0;
}

It calculates the factorial of a number by repeatedly multiplying it by the factorial of the number one less than it until it reaches 1.

Q3

In C programming, functions can be categorized into four types based on their return type and parameters:

  1. Functions with no return value and no parameters (void function):
    • These functions perform a task but do not return any value.
    • Example:
    void greet() { printf("Hello!\n"); }
  2. Functions with return value and no parameters:
    • These functions perform a task and return a value.
    • Example:
    int generateRandomNumber() { return rand(); }
  3. Functions with no return value and parameters:
    • These functions perform a task but do not return any value. They take parameters.
    • Example:
    void printSum(int a, int b) { printf("Sum: %d\n", a + b); }
  4. Functions with return value and parameters:
    • These functions perform a task and return a value. They take parameters.
    • Example:
    int calculateSquare(int num) { return num * num; }

Regarding actual and formal arguments in user-defined functions:

  • Formal Arguments: These are the parameters declared in the function definition.
    • Example: In void printSum(int a, int b), int a and int b are formal arguments.
  • Actual Arguments: These are the values passed to a function when it is called.
    • Example: In printSum(5, 3), 5 and 3 are actual arguments, and they get assigned to a and b respectively when the function is called.

The difference lies in their role within the function: formal arguments are placeholders in the function definition, while actual arguments are the values passed to the function when it’s called, and they are assigned to the formal arguments.

Q4.

Entry-Controlled Loops: These loops evaluate the condition before entering the loop body. If the condition is false initially, the loop body will not execute at all. Examples include while and for loops in C. In a while loop, the condition is checked before entering the loop block. Similarly, in a for loop, the initialization, condition checking, and increment/decrement are evaluated before entering the loop.

Exit-Controlled Loops: Contrary to entry-controlled loops, exit-controlled loops first execute the loop body and then check the condition. This ensures that the loop body is executed at least once, regardless of the condition’s initial value. The do...while loop in C is an exit-controlled loop. It executes the code block first and then evaluates the condition. If the condition is true, it continues to execute the loop; otherwise, it exits the loop after the first iteration.

Q5.


#include <stdio.h>

int main() {
    int rows = 4; // Number of rows for the pattern
    int i, j;

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}




B. Write a program to print the following pattern:
AAAA
BBB
CC
D



#include <stdio.h>

int main() {
    char ch = 'A';
    int rows = 5; // Number of rows for the pattern
    int i, j;

    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("%c", ch);
        }
        ch++;
        printf("\n");
    }

    return 0;
}

Q6. Write a program to print the following pattern:
1
2 3
4 5 6
7 8 9 10

#include <stdio.h>

int main() {
int rows = 4; // Number of rows for the pattern
int i, j;
int num = 1; // Starting number

for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf(“%d “, num);
num = (num % 9) + 1; // Reset to 1 after reaching 9
}
printf(“\n”);
}

return 0;
}

Q8. Write a C program to display first 100 prime nos.

#include <stdio.h>

int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i * i <= num; i++)
        if (num % i == 0) return 0;
    return 1;
}

int main() {
    int count = 0, num = 2;

    printf("First 100 prime numbers:\n");

    while (count < 100) {
        if (isPrime(num)) {
            printf("%d ", num);
            count++;
        }
        num++;
    }

    return 0;
}

Q9. Write a C program to convert decimal to binary

#include <stdio.h>

void decimalToBinary(int decimalNum) {
    int binaryNum[32]; // Array to store binary digits
    int i = 0;

    while (decimalNum > 0) {
        binaryNum[i] = decimalNum % 2; // Get the remainder (0 or 1)
        decimalNum = decimalNum / 2; // Update the number by dividing it by 2
        i++;
    }

    printf("Binary representation: ");
    // Display the binary number in reverse order
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binaryNum[j]);
    }
}

int main() {
    int decimalNumber;

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

    decimalToBinary(decimalNumber);

    return 0;
}

Q10. Write a C program to print the accepted no and its reverse no.

#include <stdio.h>

int reverseNumber(int num) {
    int reversedNum = 0;

    while (num != 0) {
        int digit = num % 10; // Get the last digit of the number
        reversedNum = reversedNum * 10 + digit; // Build the reversed number
        num = num / 10; // Remove the last digit from the original number
    }

    return reversedNum;
}

int main() {
    int number;

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

    printf("Accepted number: %d\n", number);
    printf("Reverse number: %d\n", reverseNumber(number));

    return 0;
}

Q11. Write a C program to find the sum of digits of accepted no

#include <stdio.h>

int sumOfDigits(int num) {
    int sum = 0;

    while (num != 0) {
        int digit = num % 10; // Get the last digit of the number
        sum += digit; // Add the digit to the sum
        num = num / 10; // Remove the last digit from the original number
    }

    return sum;
}

int main() {
    int number;

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

    printf("Accepted number: %d\n", number);
    printf("Sum of digits: %d\n", sumOfDigits(number));

    return 0;
}

Q12. Write a C program to print all the Factors of accepted number

#include <stdio.h>

void printFactors(int num) {
    printf("Factors of %d are: ", num);

    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
}

int main() {
    int number;

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

    printFactors(number);

    return 0;
}

Q13. Write a C program to find HCF or GCD of two given numbers, using recursion

#include <stdio.h>

// Function to find the GCD using recursion
int gcd(int a, int b) {
    if (b == 0) {
        return a; // GCD is the non-zero value of 'a'
    } else {
        return gcd(b, a % b); // Recursively call gcd with 'b' and 'a mod b'
    }
}

int main() {
    int num1, num2;

    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    printf("HCF/GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));

    return 0;
}

Q14. Write a C program to find LCM of two given numbers, using recursion

#include <stdio.h>

// Function to find the GCD using recursion (as used in LCM calculation)
int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

// Function to find the LCM using the GCD function
int lcm(int a, int b) {
    int gcdVal = gcd(a, b);
    int lcmVal = (a * b) / gcdVal; // LCM = (a * b) / GCD

    return lcmVal;
}

int main() {
    int num1, num2;

    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    printf("LCM of %d and %d is: %d\n", num1, num2, lcm(num1, num2));

    return 0;
}

Q15. Write a C program to display first 15 Fibonacci nos, using recursion

#include <stdio.h>

// Function to calculate the nth Fibonacci number using recursion
int fibonacci(int n) {
    if (n <= 1) {
        return n; // Base case: if n is 0 or 1, return n
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case: F(n) = F(n-1) + F(n-2)
    }
}

int main() {
    int count = 15; // Number of Fibonacci numbers to display

    printf("First %d Fibonacci numbers: \n", count);

    for (int i = 0; i < count; i++) {
        printf("%d ", fibonacci(i));
    }
    printf("\n");

    return 0;
}

Q16. Differentiate between Call by Value and Call by Reference. In support use example of
swapping of two numbers

Call by Value and Call by Reference are mechanisms to pass arguments to functions.

  1. Call by Value:
    • In Call by Value, copies of the actual parameters (values) are passed to the function.Modifications made to the parameters within the function do not affect the original values outside the function.It’s safer because the original values remain unchanged.
    Example of swapping two numbers using Call by Value:
#include <stdio.h>

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int num1 = 10, num2 = 20;

    printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);

    swap(num1, num2);

    printf("After swap: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

Output:

Before swap: num1 = 10, num2 = 20
After swap: num1 = 10, num2 = 20

2. Call by Reference:

  • In Call by Reference, addresses (references) of actual parameters are passed to the function.
  • Modifications made to the parameters within the function affect the original values outside the function.
  • It’s powerful for altering original values but requires caution.

Example of swapping two numbers using Call by Reference:.

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int num1 = 10, num2 = 20;

    printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);

    swap(&num1, &num2);

    printf("After swap: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

Output

Before swap: num1 = 10, num2 = 20
After swap: num1 = 20, num2 = 10

Q17. Write a recursive C Function to reverse given no

#include <stdio.h>

int reverseNumber(int num, int reversedNum) {
    if (num == 0) {
        return reversedNum; // Base case: when the number becomes 0, return reversed number
    } else {
        reversedNum = reversedNum * 10 + num % 10; // Build the reversed number
        return reverseNumber(num / 10, reversedNum); // Recursive call with updated number
    }
}

int main() {
    int number;

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

    int reversed = reverseNumber(number, 0);

    printf("Original number: %d\n", number);
    printf("Reversed number: %d\n", reversed);

    return 0;
}

Q18. Write a C program to check given number is Armstrong number.

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

int isArmstrong(int num) {
    int originalNum = num;
    int digits = 0, result = 0;

    // Count the number of digits in the number
    while (originalNum != 0) {
        originalNum /= 10;
        digits++;
    }

    originalNum = num; // Reset originalNum to the given number

    // Calculate the Armstrong number condition
    while (originalNum != 0) {
        int digit = originalNum % 10;
        result += pow(digit, digits);
        originalNum /= 10;
    }

    // Check if the given number is equal to the result
    if (result == num) {
        return 1; // It's an Armstrong number
    } else {
        return 0; // It's not an Armstrong number
    }
}

int main() {
    int number;

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

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

    return 0;
}
Posted in Uncategorized
Write a comment