11.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> // Function to find the greatest of three numbers int findGreatest(int num1, int num2, int num3) { if (num1 > num2 && num1 > num3) { return num1; } else if (num2 > num3) { return num2; } else { return num3; } } int main() { int num1, num2, num3; printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3); int greatest = findGreatest(num1, num2, num3); printf("The greatest number is: %d\n", greatest); return 0; } |
12.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <string.h> int main() { char str[100]; // Assuming the maximum length of the string is 100 characters printf("Enter a string: "); scanf("%[^\n]", str); // Read the string with spaces until newline int length = strlen(str); printf("Length of the string: %d\n", length); return 0; } |
13.
The main difference between a while loop and a do-while loop lies in when the loop condition is checked:
- While Loop:
- In a while loop, the condition is checked before the loop body is executed.
- If the condition is false initially, the loop body will not execute at all.
- Do-While Loop:
- In a do-while loop, the condition is checked after the loop body is executed.
- This guarantees that the loop body will execute at least once, even if the condition is false initially.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int num = 5; // Starting number for multiples printf("Multiples of 5 less than 100:\n"); // While loop to print multiples of 5 less than 100 while (num < 100) { printf("%d ", num); num += 5; // Increment by 5 for next multiple } return 0; } |
14.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int rows = 4; // Number of rows in the pattern // Outer loop for rows for (int i = 1; i <= rows; i++) { // Inner loop for numbers in each row for (int j = 1; j <= i; j++) { printf("%d\t", j); // Print the number } printf("\n"); // Move to the next line after each row } return 0; } |
15.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> // Function prototype unsigned long long factorial(int n); int main() { int N; unsigned long long fact; // Input from the user printf("Enter a non-negative integer: "); scanf("%d", &N); // Check if the number is negative if (N < 0) { printf("Factorial is not defined for negative numbers.\n"); } else { // Calculate factorial using recursive function fact = factorial(N); printf("Factorial of %d is %llu.\n", N, fact); } return 0; } // Recursive function to calculate factorial unsigned long long factorial(int n) { // Base case: factorial of 0 is 1 if (n == 0) { return 1; } else { // Recursive call to factorial function // n! = n * (n-1)! return n * factorial(n - 1); } } |