Welcome to the most comprehensive 152 C Programming Questions and Solutions master guide. Designed specifically for B.Tech, BCA, MCA, and engineering students preparing for university semester examinations (like MAKAUT), lab vivas, and technical coding interviews (TCS, Wipro, Cognizant, Infosys, LTIMindtree).
All 152 programs in this guide are fully tested using GCC under C99/C11 standards. Each program includes full executable source code, explanations of key logic, and exam context.
---
Master Syllabus & Unit Quick Index
| Unit | Topic Coverage | Question Range | Key Focus Areas |
|---|---|---|---|
| Unit 1 | Basics, Data Types, Operators, Control Flow | Q1 – Q41 | GCC pipeline, Precedence, if-else, switch-case, leap year, roots, grading |
| Unit 2 | Loops, Series Summation, Pattern Printing | Q42 – Q83 | for/while/do-while, prime, Armstrong, Fibonacci, Krishnamurthy, 16+ Patterns |
| Unit 3 | Functions, Storage Classes, Pointers, Arrays | Q84 – Q112 | Call by value/ref, static/extern, array ops, 2nd max/min, Adam number |
| Unit 4 | Strings, 2D Matrices, 6 Sorting Algorithms | Q113 – Q143 | Matrix math, String without string.h, Bubble/Selection/Insertion/Quick/Merge/Shell Sort |
| Unit 5 | Recursion Mechanics & Call Stack Traces | Q144 – Q150 | Stack frames, tail recursion, recursive factorial/fib/GCD/string rev |
| Unit 6 | Structures, Unions & Marksheet System | Q151 – Q152 | Struct alignment, padding bytes, student database, MAKAUT grade generator |
---
Quick Reference Cheatsheets
1. C Operator Precedence Table
| Priority | Category | Operators | Associativity | ||
|---|---|---|---|---|---|
| 1 (Highest) | Postfix | () [] -> . ++ (post) -- (post) | Left to Right | ||
| 2 | Unary | + - ! ~ ++ (pre) -- (pre) (type) * & sizeof | Right to Left | ||
| 3 | Multiplicative | * / % | Left to Right | ||
| 4 | Additive | + - | Left to Right | ||
| 5 | Relational | < <= > >= | Left to Right | ||
| 6 | Equality | == != | Left to Right | ||
| 7 | Logical AND/OR | && ` | ` | Left to Right | |
| 8 (Lowest) | Assignment | = += -= *= /= %= | Right to Left |
2. Sorting Algorithms Performance Summary
| Algorithm | Best Time | Average Time | Worst Time | Auxiliary Space | Stability |
|---|---|---|---|---|---|
| Bubble Sort | O(N) | O(N^2) | O(N^2) | O(1) | Stable |
| Selection Sort | O(N^2) | O(N^2) | O(N^2) | O(1) | Unstable |
| Insertion Sort | O(N) | O(N^2) | O(N^2) | O(1) | Stable |
| Quick Sort | O(N \log N) | O(N \log N) | O(N^2) | O(\log N) | Unstable |
| Merge Sort | O(N \log N) | O(N \log N) | O(N \log N) | O(N) | Stable |
| Shell Sort | O(N \log N) | O(N^{1.3}) | O(N^2) | O(1) | Unstable |
---
Unit 1: Basics, Operators & Decision Control Flow (Q1 – Q41)
Question 1: Addition
Problem Statement: Write a C program to addition.
1/*2 * Program #01: Addition3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Standard arithmetic addition program.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b, sum;1213 printf("Enter two numbers: ");14 scanf("%d %d", &a, &b);1516 sum = a + b;1718 printf("Sum = %d\n", sum);1920 return 0;21}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_001_addition.c -o prog (add -lm if using <math.h>).
---
Question 2: Subtraction
Problem Statement: Write a C program to subtraction.
1/*2 * Program #02: Subtraction3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Standard arithmetic subtraction program. C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int a, b, diff;1011 printf("Enter two numbers: ");12 scanf("%d %d", &a, &b);1314 diff = a - b;1516 printf("Difference = %d\n", diff);1718 return 0;19}20 */2122"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2324int main() {25 int a, b, diff;2627 printf("Enter two numbers: ");28 scanf("%d %d", &a, &b);2930 diff = a - b;3132 printf("Difference = %d\n", diff);3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_002_subtraction.c -o prog (add -lm if using <math.h>).
---
Question 3: Multiplication
Problem Statement: Write a C program to multiplication.
1/*2 * Program #03: Multiplication3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Standard arithmetic multiplication program. C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int a, b, prod;1011 printf("Enter two numbers: ");12 scanf("%d %d", &a, &b);1314 prod = a * b;1516 printf("Product = %d\n", prod);1718 return 0;19}20 */2122"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2324int main() {25 int a, b, prod;2627 printf("Enter two numbers: ");28 scanf("%d %d", &a, &b);2930 prod = a * b;3132 printf("Product = %d\n", prod);3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_003_multiplication.c -o prog (add -lm if using <math.h>).
---
Question 4: Division
Problem Statement: Write a C program to division.
1/*2 * Program #04: Division3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Standard arithmetic division program. C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 float a, b, div;1011 printf("Enter two numbers: ");12 scanf("%f %f", &a, &b);1314 if (b != 0) {15 div = a / b;16 printf("Quotient = %.2f\n", div);17 } else {18 printf("Error: Division by zero is not allowed.\n");19 }2021 return 0;22}23 */2425"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2627int main() {28 float a, b, div;2930 printf("Enter two numbers: ");31 scanf("%f %f", &a, &b);3233 if (b != 0) {34 div = a / b;35 printf("Quotient = %.2f\n", div);36 } else {37 printf("Error: Division by zero is not allowed.\n");38 }3940 return 0;41}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_004_division.c -o prog (add -lm if using <math.h>).
---
Question 5: All mathematical operations consisting of 1 5
Problem Statement: Write a C program to all mathematical operations consisting of 1 5.
1/*2 * Program #05: All Mathematical Operations(consisting of 1-5)3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Combined operations program. C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int a, b;1011 printf("Enter two numbers: ");12 scanf("%d %d", &a, &b);1314 printf("Addition: %d + %d = %d\n", a, b, a + b);15 printf("Subtraction: %d - %d = %d\n", a, b, a - b);16 printf("Multiplication: %d * %d = %d\n", a, b, a * b);1718 if (b != 0) {19 printf("Division: %d / %d = %.2f\n", a, b, (float)a / b);20 } else {21 printf("Division: Cannot divide by zero\n");22 }2324 return 0;25}26 */2728"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2930int main() {31 int a, b;3233 printf("Enter two numbers: ");34 scanf("%d %d", &a, &b);3536 printf("Addition: %d + %d = %d\n", a, b, a + b);37 printf("Subtraction: %d - %d = %d\n", a, b, a - b);38 printf("Multiplication: %d * %d = %d\n", a, b, a * b);3940 if (b != 0) {41 printf("Division: %d / %d = %.2f\n", a, b, (float)a / b);42 } else {43 printf("Division: Cannot divide by zero\n");44 }4546 return 0;47}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_005_all_mathematical_operations_consisting_of_1_5.c -o prog (add -lm if using <math.h>).
---
Question 6: Swap 2 numbers with variable
Problem Statement: Write a C program to swap 2 numbers with variable.
1/*2 * Program #06: Swap 2 numbers with variable3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int a, b, temp;1011 printf("Enter a and b: ");12 scanf("%d %d", &a, &b);1314 printf("Before swap: a = %d, b = %d\n", a, b);1516 temp = a;17 a = b;18 b = temp;1920 printf("After swap: a = %d, b = %d\n", a, b);2122 return 0;23} int temp = a; a = b; b = temp;24 */2526"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2728int main() {29 int a, b, temp;3031 printf("Enter a and b: ");32 scanf("%d %d", &a, &b);3334 printf("Before swap: a = %d, b = %d\n", a, b);3536 temp = a;37 a = b;38 b = temp;3940 printf("After swap: a = %d, b = %d\n", a, b);4142 return 0;43}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_006_swap_2_numbers_with_variable.c -o prog (add -lm if using <math.h>).
---
Question 7: Swap 2 numbers without variable
Problem Statement: Write a C program to swap 2 numbers without variable.
1/*2 * Program #07: Swap 2 numbers without variable3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: a = a + b; b = a - b; a = a - b;6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b;1213 printf("Enter two numbers: ");14 scanf("%d %d", &a, &b);1516 printf("Before swap: a = %d, b = %d\n", a, b);1718 a = a + b;19 b = a - b;20 a = a - b;2122 printf("After swap: a = %d, b = %d\n", a, b);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_007_swap_2_numbers_without_variable.c -o prog (add -lm if using <math.h>).
---
Question 8: Farenheit to celcius
Problem Statement: Write a C program to farenheit to celcius.
1/*2 * Program #08: Farenheit to Celcius3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 float f, c;1011 printf("Enter temperature in Fahrenheit: ");12 scanf("%f", &f);1314 c = (f - 32) * 5.0 / 9.0;1516 printf("Temperature in Celsius = %.2f\n", c);1718 return 0;19} C = (F - 32) * 5.0 / 9.0;20 */2122"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2324int main() {25 float f, c;2627 printf("Enter temperature in Fahrenheit: ");28 scanf("%f", &f);2930 c = (f - 32) * 5.0 / 9.0;3132 printf("Temperature in Celsius = %.2f\n", c);3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_008_farenheit_to_celcius.c -o prog (add -lm if using <math.h>).
---
Question 9: Reverse 5 digit number without loop
Problem Statement: Write a C program to reverse 5 digit number without loop.
1/*2 * Program #09: Reverse 5 digit number without loop3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int n, rev;1011 printf("Enter a 5-digit number: ");12 scanf("%d", &n);1314 rev = (n % 10) * 10000 +15 (n / 10 % 10) * 1000 +16 (n / 100 % 10) * 100 +17 (n / 1000 % 10) * 10 +18 (n / 10000);1920 printf("Reversed number = %d\n", rev);2122 return 0;23} rev = (n%10)*10000 + (n/10%10)*1000 + (n/100%10)*100 + (n/1000%10)*10 + (n/10000);24 */2526"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2728int main() {29 int n, rev;3031 printf("Enter a 5-digit number: ");32 scanf("%d", &n);3334 rev = (n % 10) * 10000 +35 (n / 10 % 10) * 1000 +36 (n / 100 % 10) * 100 +37 (n / 1000 % 10) * 10 +38 (n / 10000);3940 printf("Reversed number = %d\n", rev);4142 return 0;43}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_009_reverse_5_digit_number_without_loop.c -o prog (add -lm if using <math.h>).
---
Question 10: Max and min of 2 numbers
Problem Statement: Write a C program to max and min of 2 numbers.
1/*2 * Program #10: Max and Min of 2 numbers3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Use basic if-else logic. C Program6"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>78int main() {9 int a, b;1011 printf("Enter two numbers: ");12 scanf("%d %d", &a, &b);1314 if (a > b) {15 printf("Max is %d, Min is %d\n", a, b);16 } else if (b > a) {17 printf("Max is %d, Min is %d\n", b, a);18 } else {19 printf("Both numbers are equal(%d)\n", a);20 }2122 return 0;23}24 */2526"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2728int main() {29 int a, b;3031 printf("Enter two numbers: ");32 scanf("%d %d", &a, &b);3334 if (a > b) {35 printf("Max is %d, Min is %d\n", a, b);36 } else if (b > a) {37 printf("Max is %d, Min is %d\n", b, a);38 } else {39 printf("Both numbers are equal(%d)\n", a);40 }4142 return 0;43}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_010_max_and_min_of_2_numbers.c -o prog (add -lm if using <math.h>).
---
Question 11: Max of 3 numbers
Problem Statement: Write a C program to max of 3 numbers.
1/*2 * Program #11: Max of 3 numbers3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Use nested if-else or logical AND operators.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b, c;1213 printf("Enter three numbers: ");14 scanf("%d %d %d", &a, &b, &c);1516 if (a >= b && a >= c) {17 printf("Max is %d\n", a);18 } else if (b >= a && b >= c) {19 printf("Max is %d\n", b);20 } else {21 printf("Max is %d\n", c);22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_011_max_of_3_numbers.c -o prog (add -lm if using <math.h>).
---
Question 12: If number is ve ve or 0
Problem Statement: Write a C program to if number is ve ve or 0.
1/*2 * Program #12: If number is +ve, -ve or 03 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Simple relational checking against zero.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;1213 printf("Enter a number: ");14 scanf("%d", &n);1516 if (n > 0) {17 printf("%d is Positive\n", n);18 } else if (n < 0) {19 printf("%d is Negative\n", n);20 } else {21 printf("The number is Zero\n");22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_012_if_number_is_ve_ve_or_0.c -o prog (add -lm if using <math.h>).
---
Question 13: If number is divisible by 5 and 11
Problem Statement: Write a C program to if number is divisible by 5 and 11.
1/*2 * Program #13: If number is divisible by 5 AND 113 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if (n % 5 == 0 && n % 11 == 0)6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;1213 printf("Enter a number: ");14 scanf("%d", &n);1516 if (n % 5 == 0 && n % 11 == 0) {17 printf("%d is divisible by 5 and 11\n", n);18 } else {19 printf("%d is not divisible by 5 and 11\n", n);20 }2122 return 0;23}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_013_if_number_is_divisible_by_5_and_11.c -o prog (add -lm if using <math.h>).
---
Question 14: If number is even or odd
Problem Statement: Write a C program to if number is even or odd.
1/*2 * Program #14: If number is even or odd3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if (n % 2 == 0)6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;1213 printf("Enter a number: ");14 scanf("%d", &n);1516 if (n % 2 == 0) {17 printf("%d is Even\n", n);18 } else {19 printf("%d is Odd\n", n);20 }2122 return 0;23}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_014_if_number_is_even_or_odd.c -o prog (add -lm if using <math.h>).
---
Question 15: Check if leap year or not
Problem Statement: Write a C program to check if leap year or not.
1/*2 * Program #15: Check if leap year or not3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int year;1213 printf("Enter a year: ");14 scanf("%d", &year);1516 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {17 printf("%d is a Leap Year\n", year);18 } else {19 printf("%d is not a Leap Year\n", year);20 }2122 return 0;23}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_015_check_if_leap_year_or_not.c -o prog (add -lm if using <math.h>).
---
Question 16: Check if alphabet or not
Problem Statement: Write a C program to check if alphabet or not.
1/*2 * Program #16: Check if alphabet or not3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;1213 printf("Enter a character: ");14 scanf(" %c", &c);1516 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {17 printf("'%c' is an Alphabet\n", c);18 } else {19 printf("'%c' is not an Alphabet\n", c);20 }2122 return 0;23}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_016_check_if_alphabet_or_not.c -o prog (add -lm if using <math.h>).
---
Question 17: Check is vowel or consonant ifelse
Problem Statement: Write a C program to check is vowel or consonant ifelse.
1/*2 * Program #17: Check is vowel or consonant - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Check if char is a, e, i, o, u(both cases).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;12 int isLowercaseVowel, isUppercaseVowel;1314 printf("Enter an alphabet: ");15 scanf(" %c", &c);1617 isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');18 isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');1920 if (isLowercaseVowel || isUppercaseVowel) {21 printf("'%c' is a Vowel\n", c);22 } else {23 printf("'%c' is a Consonant\n", c);24 }2526 return 0;27}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_017_check_is_vowel_or_consonant_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 18: Check is alphabet digit or special character
Problem Statement: Write a C program to check is alphabet digit or special character.
1/*2 * Program #18: Check is alphabet, digit or special character3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Combine alphabet check with digit check(c >= '0' && c <= '9').6 */789int main() {10 char c;1112 printf("Enter any character: ");13 scanf(" %c", &c);1415 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {16 printf("'%c' is an Alphabet\n", c);17 } else if (c >= '0' && c <= '9') {18 printf("'%c' is a Digit\n", c);19 } else {20 printf("'%c' is a Special Character\n", c);21 }2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_018_check_is_alphabet_digit_or_special_character.c -o prog (add -lm if using <math.h>).
---
Question 19: Check if character is uppercase or lowercase
Problem Statement: Write a C program to check if character is uppercase or lowercase.
1/*2 * Program #19: Check if character is uppercase or lowercase3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Check ASCII ranges.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;1213 printf("Enter an alphabet: ");14 scanf(" %c", &c);1516 if (c >= 'A' && c <= 'Z') {17 printf("'%c' is Uppercase\n", c);18 } else if (c >= 'a' && c <= 'z') {19 printf("'%c' is Lowercase\n", c);20 } else {21 printf("'%c' is not an alphabet\n", c);22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_019_check_if_character_is_uppercase_or_lowercase.c -o prog (add -lm if using <math.h>).
---
Question 20: Input week day number and print week day name ifelse
Problem Statement: Write a C program to input week day number and print week day name ifelse.
1/*2 * Program #20: Input week-day number and print week-day name - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Chain of if-else statements(1=Monday, etc).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int day;1213 printf("Enter weekday number(1-7): ");14 scanf("%d", &day);1516 if (day == 1) {17 printf("Monday\n");18 } else if (day == 2) {19 printf("Tuesday\n");20 } else if (day == 3) {21 printf("Wednesday\n");22 } else if (day == 4) {23 printf("Thursday\n");24 } else if (day == 5) {25 printf("Friday\n");26 } else if (day == 6) {27 printf("Saturday\n");28 } else if (day == 7) {29 printf("Sunday\n");30 } else {31 printf("Invalid input! Please enter a number between 1 and 7.\n");32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_020_input_week_day_number_and_print_week_day_name_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 21: Total number of different notes in a given amount
Problem Statement: Write a C program to total number of different notes in a given amount.
1/*2 * Program #21: Total number of different notes in a given amount3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Divide amount by 2000, 500, 100, etc., and take remainders.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int amount;1213 printf("Enter amount: ");14 scanf("%d", &amount);1516 int notes[] = {2000, 500, 200, 100, 50, 20, 10, 5, 2, 1};17 int count = 0;1819 printf("Notes breakdown:\n");20 for (int i = 0; i < 10; i++) {21 if (amount >= notes[i]) {22 count = amount / notes[i];23 printf("%d Rs notes : %d\n", notes[i], count);24 amount = amount % notes[i];25 }26 }2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_021_total_number_of_different_notes_in_a_given_amount.c -o prog (add -lm if using <math.h>).
---
Question 22: Check angles and verify if triangle is valid
Problem Statement: Write a C program to check angles and verify if triangle is valid.
1/*2 * Program #22: Check angles and verify if triangle is valid3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if (a + b + c == 180)6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b, c, sum;1213 printf("Enter three angles of triangle: ");14 scanf("%d %d %d", &a, &b, &c);1516 sum = a + b + c;1718 if (sum == 180 && a > 0 && b > 0 && c > 0) {19 printf("The triangle is valid.\n");20 } else {21 printf("The triangle is not valid.\n");22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_022_check_angles_and_verify_if_triangle_is_valid.c -o prog (add -lm if using <math.h>).
---
Question 23: Check if triangle is equilateral or isosceles
Problem Statement: Write a C program to check if triangle is equilateral or isosceles.
1/*2 * Program #23: Check if triangle is equilateral or isosceles3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Equilateral: a==b && b==c. Isosceles: a==b || b==c || a==c.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b, c;1213 printf("Enter three sides of triangle: ");14 scanf("%d %d %d", &a, &b, &c);1516 if (a == b && b == c) {17 printf("Triangle is Equilateral.\n");18 } else if (a == b || b == c || a == c) {19 printf("Triangle is Isosceles.\n");20 } else {21 printf("Triangle is Scalene.\n");22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_023_check_if_triangle_is_equilateral_or_isosceles.c -o prog (add -lm if using <math.h>).
---
Question 24: Find roots of quadratic equation
Problem Statement: Write a C program to find roots of quadratic equation.
1/*2 * Program #24: Find roots of quadratic equation3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Calculate discriminant(b*b - 4*a*c).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>1011int main() {12 float a, b, c, discriminant, root1, root2;1314 printf("Enter coefficients a, b and c: ");15 scanf("%f %f %f", &a, &b, &c);1617 discriminant = b * b - 4 * a * c;1819 if (discriminant > 0) {20 root1 = (-b + sqrt(discriminant)) / (2 * a);21 root2 = (-b - sqrt(discriminant)) / (2 * a);22 printf("Roots are real and distinct: %.2f and %.2f\n", root1, root2);23 } else if (discriminant == 0) {24 root1 = root2 = -b / (2 * a);25 printf("Roots are real and equal: %.2f and %.2f\n", root1, root2);26 } else {27 float realPart = -b / (2 * a);28 float imagPart = sqrt(-discriminant) / (2 * a);29 printf("Roots are complex: %.2f+%.2fi and %.2f-%.2fi\n", realPart, imagPart, realPart, imagPart);30 }3132 return 0;33}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_024_find_roots_of_quadratic_equation.c -o prog (add -lm if using <math.h>).
---
Question 25: Find gross salary given basic salary
Problem Statement: Write a C program to find gross salary given basic salary.
1/*2 * Program #25: Find gross salary given basic salary3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Salary <= 10K, HRA 10%, DA=15%; Salary <= 20K, HRA=15%, DA=20%; Salary > 20K, HRA 20%, DA=30%;6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 float basic, hra, da, gross;1213 printf("Enter basic salary: ");14 scanf("%f", &basic);1516 if (basic <= 10000) {17 hra = basic * 0.10;18 da = basic * 0.15;19 } else if (basic <= 20000) {20 hra = basic * 0.15;21 da = basic * 0.20;22 } else {23 hra = basic * 0.20;24 da = basic * 0.30;25 }2627 gross = basic + hra + da;28 printf("Gross Salary = %.2f\n", gross);2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_025_find_gross_salary_given_basic_salary.c -o prog (add -lm if using <math.h>).
---
Question 26: Input marks and print grade as per makaut regulations
Problem Statement: Write a C program to input marks and print grade as per makaut regulations.
1/*2 * Program #26: Input marks and print grade as per MAKAUT regulations3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: 100-90 = O; 80-89 = E; 70-79 = A; 60-69 = B; 50-59 = C; 40-49 = D; <40 = F6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int marks;1213 printf("Enter marks: ");14 scanf("%d", &marks);1516 if (marks >= 90 && marks <= 100) {17 printf("Grade: O\n");18 } else if (marks >= 80 && marks <= 89) {19 printf("Grade: E\n");20 } else if (marks >= 70 && marks <= 79) {21 printf("Grade: A\n");22 } else if (marks >= 60 && marks <= 69) {23 printf("Grade: B\n");24 } else if (marks >= 50 && marks <= 59) {25 printf("Grade: C\n");26 } else if (marks >= 40 && marks <= 49) {27 printf("Grade: D\n");28 } else if (marks < 40 && marks >= 0) {29 printf("Grade: F\n");30 } else {31 printf("Invalid marks entered.\n");32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_026_input_marks_and_print_grade_as_per_makaut_regulations.c -o prog (add -lm if using <math.h>).
---
Question 27: Take symbol from user and perform operation basic calculator ifelse
Problem Statement: Write a C program to take symbol from user and perform operation basic calculator ifelse.
1/*2 * Program #27: Take symbol from user and perform operation(basic calculator) - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Chain if-else against '+', '-', '*', '/'.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char op;12 float num1, num2;1314 printf("Enter operator(+, -, *, /): ");15 scanf(" %c", &op);1617 printf("Enter two numbers: ");18 scanf("%f %f", &num1, &num2);1920 if (op == '+') {21 printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);22 } else if (op == '-') {23 printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);24 } else if (op == '*') {25 printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);26 } else if (op == '/') {27 if (num2 != 0)28 printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);29 else30 printf("Error: Division by zero\n");31 } else {32 printf("Invalid operator\n");33 }3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_027_take_symbol_from_user_and_perform_operation_basic_calculator_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 28: Take symbol from user and perform operation basic calculator switch
Problem Statement: Write a C program to take symbol from user and perform operation basic calculator switch.
1/*2 * Program #28: Take symbol from user and perform operation(basic calculator) - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Use switch(char).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char op;12 float num1, num2;1314 printf("Enter operator(+, -, *, /): ");15 scanf(" %c", &op);1617 printf("Enter two numbers: ");18 scanf("%f %f", &num1, &num2);1920 switch(op) {21 case '+':22 printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);23 break;24 case '-':25 printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);26 break;27 case '*':28 printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);29 break;30 case '/':31 if (num2 != 0)32 printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);33 else34 printf("Error: Division by zero\n");35 break;36 default:37 printf("Invalid operator\n");38 }3940 return 0;41}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_028_take_symbol_from_user_and_perform_operation_basic_calculator_switch.c -o prog (add -lm if using <math.h>).
---
Question 29: Check whether vowel or consonant switch
Problem Statement: Write a C program to check whether vowel or consonant switch.
1/*2 * Program #29: Check whether vowel or consonant - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Cases for 'a','e','i','o','u'.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;1213 printf("Enter an alphabet: ");14 scanf(" %c", &c);1516 switch(c) {17 case 'a': case 'e': case 'i': case 'o': case 'u':18 case 'A': case 'E': case 'I': case 'O': case 'U':19 printf("'%c' is a Vowel\n", c);20 break;21 default:22 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))23 printf("'%c' is a Consonant\n", c);24 else25 printf("'%c' is not an alphabet\n", c);26 }2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_029_check_whether_vowel_or_consonant_switch.c -o prog (add -lm if using <math.h>).
---
Question 30: Input week day number and print week day name switch
Problem Statement: Write a C program to input week day number and print week day name switch.
1/*2 * Program #30: Input week-day number and print week-day name - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Cases 1 to 7.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int day;1213 printf("Enter weekday number(1-7): ");14 scanf("%d", &day);1516 switch(day) {17 case 1: printf("Monday\n"); break;18 case 2: printf("Tuesday\n"); break;19 case 3: printf("Wednesday\n"); break;20 case 4: printf("Thursday\n"); break;21 case 5: printf("Friday\n"); break;22 case 6: printf("Saturday\n"); break;23 case 7: printf("Sunday\n"); break;24 default: printf("Invalid input! Please enter a number between 1 and 7.\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_030_input_week_day_number_and_print_week_day_name_switch.c -o prog (add -lm if using <math.h>).
---
Question 31: Input m or f and print male of female ifelse
Problem Statement: Write a C program to input m or f and print male of female ifelse.
1/*2 * Program #31: Input M or F and print Male of Female - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: if (c == 'M') ... else if (c == 'F') ...6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char gender;1213 printf("Enter gender(M/F): ");14 scanf(" %c", &gender);1516 if (gender == 'M' || gender == 'm') {17 printf("Male\n");18 } else if (gender == 'F' || gender == 'f') {19 printf("Female\n");20 } else {21 printf("Invalid input\n");22 }2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_031_input_m_or_f_and_print_male_of_female_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 32: Input m or f and print male of female switch
Problem Statement: Write a C program to input m or f and print male of female switch.
1/*2 * Program #32: Input M or F and print Male of Female - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: switch(c) { case 'M': ... case 'F': ... }6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char gender;1213 printf("Enter gender(M/F): ");14 scanf(" %c", &gender);1516 switch(gender) {17 case 'M': case 'm':18 printf("Male\n");19 break;20 case 'F': case 'f':21 printf("Female\n");22 break;23 default:24 printf("Invalid input\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_032_input_m_or_f_and_print_male_of_female_switch.c -o prog (add -lm if using <math.h>).
---
Question 33: Input month and print number of days ifelse
Problem Statement: Write a C program to input month and print number of days ifelse.
1/*2 * Program #33: Input month and print number of days - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Handle leap year logic for February.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int month, year;1213 printf("Enter month(1-12): ");14 scanf("%d", &month);1516 if (month == 1 || month == 3 || month == 5 || month == 7 ||17 month == 8 || month == 10 || month == 12) {18 printf("31 Days\n");19 } else if (month == 4 || month == 6 || month == 9 || month == 11) {20 printf("30 Days\n");21 } else if (month == 2) {22 printf("Enter year to check leap year: ");23 scanf("%d", &year);24 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {25 printf("29 Days(Leap Year)\n");26 } else {27 printf("28 Days\n");28 }29 } else {30 printf("Invalid month!\n");31 }3233 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_033_input_month_and_print_number_of_days_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 34: Input month and print number of days switch
Problem Statement: Write a C program to input month and print number of days switch.
1/*2 * Program #34: Input month and print number of days - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Group cases(e.g., case 1: case 3: case 5... print 31).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int month, year;1213 printf("Enter month(1-12): ");14 scanf("%d", &month);1516 switch(month) {17 case 1: case 3: case 5: case 7: case 8: case 10: case 12:18 printf("31 Days\n");19 break;20 case 4: case 6: case 9: case 11:21 printf("30 Days\n");22 break;23 case 2:24 printf("Enter year to check leap year: ");25 scanf("%d", &year);26 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {27 printf("29 Days(Leap Year)\n");28 } else {29 printf("28 Days\n");30 }31 break;32 default:33 printf("Invalid month!\n");34 }3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_034_input_month_and_print_number_of_days_switch.c -o prog (add -lm if using <math.h>).
---
Question 35: Electricity bill ifelse
Problem Statement: Write a C program to electricity bill ifelse.
1/*2 * Program #35: Electricity bill - IFELSE3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: <= 50 units - 50 paise/unit; >50 and <= 150 - 70p/u; >150 and <= 250 - 1.2rs/u; >250 and <= 350 - 1.5rs/u; >350 - 175p/u; NOTE = 20% extra surcharge6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 float units, bill, total_bill;1213 printf("Enter electricity units consumed: ");14 scanf("%f", &units);1516 if (units <= 50) {17 bill = units * 0.50;18 } else if (units <= 150) {19 bill = (50 * 0.50) + ((units - 50) * 0.70);20 } else if (units <= 250) {21 bill = (50 * 0.50) + (100 * 0.70) + ((units - 150) * 1.20);22 } else if (units <= 350) {23 bill = (50 * 0.50) + (100 * 0.70) + (100 * 1.20) + ((units - 250) * 1.50);24 } else {25 bill = (50 * 0.50) + (100 * 0.70) + (100 * 1.20) + (100 * 1.50) + ((units - 350) * 1.75);26 }2728 total_bill = bill + (bill * 0.20); // 20% surcharge29 printf("Total Electricity Bill = Rs. %.2f\n", total_bill);3031 return 0;32}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_035_electricity_bill_ifelse.c -o prog (add -lm if using <math.h>).
---
Question 36: Electricity bill switch
Problem Statement: Write a C program to electricity bill switch.
1/*2 * Program #36: Electricity bill - SWITCH3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Switch isn't ideal for ranges, so we create slab identifiers first using if-else.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 float units, bill, total_bill;12 int slab;1314 printf("Enter electricity units consumed: ");15 scanf("%f", &units);1617 // Assign slabs since switch requires integers18 if (units <= 50) slab = 1;19 else if (units <= 150) slab = 2;20 else if (units <= 250) slab = 3;21 else if (units <= 350) slab = 4;22 else slab = 5;2324 switch(slab) {25 case 1:26 bill = units * 0.50;27 break;28 case 2:29 bill = 25 + ((units - 50) * 0.70);30 break;31 case 3:32 bill = 95 + ((units - 150) * 1.20);33 break;34 case 4:35 bill = 215 + ((units - 250) * 1.50);36 break;37 case 5:38 bill = 365 + ((units - 350) * 1.75);39 break;40 }4142 total_bill = bill * 1.20; // adding 20% surcharge43 printf("Total Electricity Bill = Rs. %.2f\n", total_bill);4445 return 0;46}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_036_electricity_bill_switch.c -o prog (add -lm if using <math.h>).
---
Question 37: Print all natural numbers in a range
Problem Statement: Write a C program to print all natural numbers in a range.
1/*2 * Program #37: Print all natural numbers in a range3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Use a while loop.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int start, end;1213 printf("Enter starting number: ");14 scanf("%d", &start);15 printf("Enter ending number: ");16 scanf("%d", &end);1718 int i = start;19 while (i <= end) {20 printf("%d ", i);21 i++;22 }23 printf("\n");2425 return 0;26}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_037_print_all_natural_numbers_in_a_range.c -o prog (add -lm if using <math.h>).
---
Question 38: Print factorial of a given number while loop
Problem Statement: Write a C program to print factorial of a given number while loop.
1/*2 * Program #38: Print factorial of a given number(While Loop)3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: while(n > 0) { fact *= n; n--; }6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, i = 1;12 unsigned long long fact = 1; // using larger capacity type for factorials1314 printf("Enter a positive integer: ");15 scanf("%d", &n);1617 while (i <= n) {18 fact *= i;19 i++;20 }2122 printf("Factorial of %d = %llu\n", n, fact);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_038_print_factorial_of_a_given_number_while_loop.c -o prog (add -lm if using <math.h>).
---
Question 39: Print sum of all numbers in a range
Problem Statement: Write a C program to print sum of all numbers in a range.
1/*2 * Program #39: Print sum of all numbers in a range3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Loop and accumulate sum.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int start, end, sum = 0;1213 printf("Enter starting number: ");14 scanf("%d", &start);15 printf("Enter ending number: ");16 scanf("%d", &end);1718 int i = start;19 while (i <= end) {20 sum += i;21 i++;22 }2324 printf("Sum of numbers from %d to %d = %d\n", start, end, sum);2526 return 0;27}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_039_print_sum_of_all_numbers_in_a_range.c -o prog (add -lm if using <math.h>).
---
Question 40: Find gross salary given basic salary switch
Problem Statement: Write a C program to find gross salary given basic salary switch.
1/*2 * Program #40: Find gross salary given basic salary(SWITCH)3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: Salary <= 10K, HRA=10%, DA=15%; Salary <= 20K, HRA=15%, DA= 20%; Salary > 20K, HRA=20%, DA= 30%6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 float basic, hra, da, gross;12 int category;1314 printf("Enter basic salary: ");15 scanf("%f", &basic);1617 // Determine category for switch18 if (basic <= 10000) category = 1;19 else if (basic <= 20000) category = 2;20 else category = 3;2122 switch(category) {23 case 1:24 hra = basic * 0.10;25 da = basic * 0.15;26 break;27 case 2:28 hra = basic * 0.15;29 da = basic * 0.20;30 break;31 case 3:32 hra = basic * 0.20;33 da = basic * 0.30;34 break;35 }3637 gross = basic + hra + da;38 printf("Gross Salary = %.2f\n", gross);3940 return 0;41}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_040_find_gross_salary_given_basic_salary_switch.c -o prog (add -lm if using <math.h>).
---
Question 41: Input marks and print grade as per makaut regulations switch
Problem Statement: Write a C program to input marks and print grade as per makaut regulations switch.
1/*2 * Program #41: Input marks and print grade as per MAKAUT regulations(SWITCH)3 * Unit 1: Basics, Operators & Control Flow4 *5 * Explanation: 100-90=O; 80-89=E; 70-79=A; 60-69=B; 50-59=C; 40-49=D; <40=F6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int marks;1213 printf("Enter marks(0-100): ");14 scanf("%d", &marks);1516 // Using division by 10 to group ranges into cases17 switch(marks / 10) {18 case 10:19 case 9:20 printf("Grade: O\n");21 break;22 case 8:23 printf("Grade: E\n");24 break;25 case 7:26 printf("Grade: A\n");27 break;28 case 6:29 printf("Grade: B\n");30 break;31 case 5:32 printf("Grade: C\n");33 break;34 case 4:35 printf("Grade: D\n");36 break;37 case 3: case 2: case 1: case 0:38 if(marks >= 0)39 printf("Grade: F\n");40 else41 printf("Invalid marks\n");42 break;43 default:44 printf("Invalid marks\n");45 }4647 return 0;48}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_041_input_marks_and_print_grade_as_per_makaut_regulations_switch.c -o prog (add -lm if using <math.h>).
---
Unit 2: Loops, Series Summation & Pattern Printing (Q42 – Q83)
Question 42: Write a program to print the numbers 1 to 10 using for loop
Problem Statement: Write a C program to write a program to print the numbers 1 to 10 using for loop.
1/*2 * Program #42: Write a program to print the numbers 1 to 10 using for loop3 * Unit 2: Loops, Series & Number Patterns4 *5 *6 */78for(int i=1; i<=10; i++) printf("%d ", i);
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_042_write_a_program_to_print_the_numbers_1_to_10_using_for_loop.c -o prog (add -lm if using <math.h>).
---
Question 43: Factorial of a number using for loop
Problem Statement: Write a C program to factorial of a number using for loop.
1/*2 * Program #43: Factorial of a number using for loop3 * Unit 2: Loops, Series & Number Patterns4 *5 *6 */78for(int i=1; i<=10; i++) printf("%d ", i);
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_043_factorial_of_a_number_using_for_loop.c -o prog (add -lm if using <math.h>).
---
Question 44: Sum of 10 numbers entered by user using for loop
Problem Statement: Write a C program to sum of 10 numbers entered by user using for loop.
1/*2 * Program #44: Sum of 10 numbers entered by user using for loop3 * Unit 2: Loops, Series & Number Patterns4 *5 *6 */78Loop 10 times, scanf, and add to sum.
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_044_sum_of_10_numbers_entered_by_user_using_for_loop.c -o prog (add -lm if using <math.h>).
---
Question 45: Write a program to print the product of 10 numbers using for loop
Problem Statement: Write a C program to write a program to print the product of 10 numbers using for loop.
1/*2 * Program #45: Write a program to print the product of 10 numbers using for loop3 * Unit 2: Loops, Series & Number Patterns4 *5 *6 */78Loop 10 times, scanf, and multiply to prod.
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_045_write_a_program_to_print_the_product_of_10_numbers_using_for_loop.c -o prog (add -lm if using <math.h>).
---
Question 46: Write a program to print the sum of series 1 x x 2 x n
Problem Statement: Write a C program to write a program to print the sum of series 1 x x 2 x n.
1/*2 * Program #46: Write a program to print the sum of series 1 + x + x^2 + ... + x^n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Maintain a term variable multiplied by x each iteration.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int x, n;12 long long sum = 1, term = 1;1314 printf("Enter values for x and n: ");15 scanf("%d %d", &x, &n);1617 for (int i = 1; i <= n; i++) {18 term *= x; // computes x^i19 sum += term;20 }2122 printf("Sum of the series = %lld\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_046_write_a_program_to_print_the_sum_of_series_1_x_x_2_x_n.c -o prog (add -lm if using <math.h>).
---
Question 47: Write a program to print the sum of series x x 2 x 3 x n
Problem Statement: Write a C program to write a program to print the sum of series x x 2 x 3 x n.
1/*2 * Program #47: Write a program to print the sum of series -x + x^2 - x^3 + ... + x^n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Multiply term by -x each iteration.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int x, n;12 long long sum = 0, term = 1;1314 printf("Enter values for x and n: ");15 scanf("%d %d", &x, &n);1617 for (int i = 1; i <= n; i++) {18 term *= -x; // Alternate signs and compute power19 sum += term;20 }2122 printf("Sum of the alternating series = %lld\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_047_write_a_program_to_print_the_sum_of_series_x_x_2_x_3_x_n.c -o prog (add -lm if using <math.h>).
---
Question 48: Write a program to print the sum of series 1 x 1 x 2 2 x 3 3 x n n
Problem Statement: Write a C program to write a program to print the sum of series 1 x 1 x 2 2 x 3 3 x n n.
1/*2 * Program #48: Write a program to print the sum of series 1 + x/1! + x^2/2! + x^3/3! + ... + x^n/n!3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: e^x series.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int x, n;12 double sum = 1.0, term = 1.0;1314 printf("Enter values for x and n: ");15 scanf("%d %d", &x, &n);1617 for (int i = 1; i <= n; i++) {18 term = term * x / i; // Compute next term dynamically19 sum += term;20 }2122 printf("Sum of the e^x series = %.4lf\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_048_write_a_program_to_print_the_sum_of_series_1_x_1_x_2_2_x_3_3_x_n_n.c -o prog (add -lm if using <math.h>).
---
Question 49: Write a program to print the sum of series 1 x 1 x 2 2 x 3 3 x n n
Problem Statement: Write a C program to write a program to print the sum of series 1 x 1 x 2 2 x 3 3 x n n.
1/*2 * Program #49: Write a program to print the sum of series 1 - x/1! + x^2/2! - x^3/3! + ... + x^n/n!3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Alternating sign e^x series.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int x, n;12 double sum = 1.0, term = 1.0;1314 printf("Enter values for x and n: ");15 scanf("%d %d", &x, &n);1617 for (int i = 1; i <= n; i++) {18 term = term * (-x) / i; // Compute next term dynamically with alternating sign19 sum += term;20 }2122 printf("Sum of the alternating series = %.4lf\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_049_write_a_program_to_print_the_sum_of_series_1_x_1_x_2_2_x_3_3_x_n_n.c -o prog (add -lm if using <math.h>).
---
Question 50: Calculate result of series 1 1 2 1 2 3 1 2 n
Problem Statement: Write a C program to calculate result of series 1 1 2 1 2 3 1 2 n.
1/*2 * Program #50: Calculate result of series 1 + (1+2) + (1+2+3) + ... + (1+2+...+n)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Triangular numbers sum.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, inner_sum = 0, total_sum = 0;1213 printf("Enter the value of n: ");14 scanf("%d", &n);1516 for (int i = 1; i <= n; i++) {17 inner_sum += i; // Sum up to current i (e.g., 1+2+3)18 total_sum += inner_sum; // Add to the grand total19 }2021 printf("Sum of the series = %d\n", total_sum);2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_050_calculate_result_of_series_1_1_2_1_2_3_1_2_n.c -o prog (add -lm if using <math.h>).
---
Question 51: Write a program to print the sum of series 1 11 111 1111
Problem Statement: Write a C program to write a program to print the sum of series 1 11 111 1111.
1/*2 * Program #51: Write a program to print the sum of series 1 + 11 + 111 + 1111 + ...3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: term = term * 10 + 1.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;12 long long term = 0, sum = 0;1314 printf("Enter the number of terms: ");15 scanf("%d", &n);1617 for (int i = 1; i <= n; i++) {18 term = term * 10 + 1; // Generates 1, 11, 111, etc.19 sum += term;20 }2122 printf("Sum of the series = %lld\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_051_write_a_program_to_print_the_sum_of_series_1_11_111_1111.c -o prog (add -lm if using <math.h>).
---
Question 52: Write a program to check whether a number is prime number or not
Problem Statement: Write a C program to write a program to check whether a number is prime number or not.
1/*2 * Program #52: Write a program to check whether a number is prime number or not3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Loop up to n/2 or sqrt(n) and check divisibility.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, isPrime = 1;1213 printf("Enter a positive integer: ");14 scanf("%d", &n);1516 if (n <= 1) {17 isPrime = 0;18 } else {19 for (int i = 2; i <= n / 2; i++) {20 if (n % i == 0) {21 isPrime = 0;22 break;23 }24 }25 }2627 if (isPrime) {28 printf("%d is a Prime Number\n", n);29 } else {30 printf("%d is not a Prime Number\n", n);31 }3233 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_052_write_a_program_to_check_whether_a_number_is_prime_number_or_not.c -o prog (add -lm if using <math.h>).
---
Question 53: Write a program to print the total numbers of odd and even numbers in range and add them separately
Problem Statement: Write a C program to write a program to print the total numbers of odd and even numbers in range and add them separately.
1/*2 * Program #53: Write a program to print the total numbers of odd and even numbers in range and add them separately3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Use modulo 2 in a loop to increment respective counters.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int start, end, evenCount = 0, oddCount = 0, evenSum = 0, oddSum = 0;1213 printf("Enter start and end of range: ");14 scanf("%d %d", &start, &end);1516 for (int i = start; i <= end; i++) {17 if (i % 2 == 0) {18 evenCount++;19 evenSum += i;20 } else {21 oddCount++;22 oddSum += i;23 }24 }2526 printf("Even Numbers: Total = %d, Sum = %d\n", evenCount, evenSum);27 printf("Odd Numbers: Total = %d, Sum = %d\n", oddCount, oddSum);2829 return 0;30}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_053_write_a_program_to_print_the_total_numbers_of_odd_and_even_numbers_in_range_and_add_them_separately.c -o prog (add -lm if using <math.h>).
---
Question 54: Write a program to calculate if a number is armstrong or not within a range
Problem Statement: Write a C program to write a program to calculate if a number is armstrong or not within a range.
1/*2 * Program #54: Write a program to calculate if a number is armstrong or not within a range3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 153 = 1^3 + 5^3 + 3^36 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>1011int main() {12 int start, end, temp, rem, sum;1314 printf("Enter start and end of range: ");15 scanf("%d %d", &start, &end);1617 printf("Armstrong numbers between %d and %d are:\n", start, end);18 for (int i = start; i <= end; i++) {19 temp = i;20 sum = 0;2122 while (temp > 0) {23 rem = temp % 10;24 sum += rem * rem * rem; // Assuming standard 3-digit Armstrong for simplicity25 temp /= 10;26 }2728 if (sum == i) {29 printf("%d ", i);30 }31 }32 printf("\n");3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_054_write_a_program_to_calculate_if_a_number_is_armstrong_or_not_within_a_range.c -o prog (add -lm if using <math.h>).
---
Question 55: Write a program to find out the fibonacci series within a range
Problem Statement: Write a C program to write a program to find out the fibonacci series within a range.
1/*2 * Program #55: Write a program to find out the fibonacci series within a range3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 1, 1, 2, 3, 5, 8, 13, 21,...6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int start, end;12 int a = 0, b = 1, next = 0;1314 printf("Enter start and end of range: ");15 scanf("%d %d", &start, &end);1617 printf("Fibonacci numbers between %d and %d:\n", start, end);1819 while (next <= end) {20 if (next >= start) {21 printf("%d ", next);22 }23 a = b;24 b = next;25 next = a + b;26 }27 printf("\n");2829 return 0;30}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_055_write_a_program_to_find_out_the_fibonacci_series_within_a_range.c -o prog (add -lm if using <math.h>).
---
Question 56: Write a program to find out the non fibonacci nos within a range
Problem Statement: Write a C program to write a program to find out the non fibonacci nos within a range.
1/*2 * Program #56: Write a program to find out the non fibonacci nos within a range3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Generate fibonacci numbers and print the gaps between them.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int start, end;12 int a = 0, b = 1, c = 0;1314 printf("Enter start and end of range: ");15 scanf("%d %d", &start, &end);1617 printf("Non-Fibonacci numbers between %d and %d:\n", start, end);1819 for (int i = start; i <= end; i++) {20 // Advance fibonacci until it meets or exceeds current number i21 while (c < i) {22 a = b;23 b = c;24 c = a + b;25 }2627 // If current number i is not equal to fibonacci number c, it's non-fibonacci28 if (c != i) {29 printf("%d ", i);30 }31 }32 printf("\n");3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_056_write_a_program_to_find_out_the_non_fibonacci_nos_within_a_range.c -o prog (add -lm if using <math.h>).
---
Question 57: Write a program to find out if a number is palindrome or not
Problem Statement: Write a C program to write a program to find out if a number is palindrome or not.
1/*2 * Program #57: Write a program to find out if a number is palindrome or not3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 585, 134316 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, reversed = 0, remainder, original;1213 printf("Enter an integer: ");14 scanf("%d", &n);1516 original = n;1718 while (n != 0) {19 remainder = n % 10;20 reversed = reversed * 10 + remainder;21 n /= 10;22 }2324 if (original == reversed) {25 printf("%d is a Palindrome.\n", original);26 } else {27 printf("%d is not a Palindrome.\n", original);28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_057_write_a_program_to_find_out_if_a_number_is_palindrome_or_not.c -o prog (add -lm if using <math.h>).
---
Question 58: Write a program to reverse a number for loop
Problem Statement: Write a C program to write a program to reverse a number for loop.
1/*2 * Program #58: Write a program to reverse a number(for loop)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Extract digits using % 10.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, reversed = 0;1213 printf("Enter an integer: ");14 scanf("%d", &n);1516 for (int temp = n; temp != 0; temp /= 10) {17 reversed = reversed * 10 + (temp % 10);18 }1920 printf("Reversed Number = %d\n", reversed);2122 return 0;23}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_058_write_a_program_to_reverse_a_number_for_loop.c -o prog (add -lm if using <math.h>).
---
Question 59: Write a program to find out if a number is krishnamurty number or not
Problem Statement: Write a C program to write a program to find out if a number is krishnamurty number or not.
1/*2 * Program #59: Write a program to find out if a number is Krishnamurty Number or not3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 145 = 1! + 4! + 5!6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, temp, rem, sum = 0, fact;1213 printf("Enter a number: ");14 scanf("%d", &n);1516 temp = n;17 while (temp > 0) {18 rem = temp % 10;19 fact = 1;20 // Calculate factorial of the digit21 for (int i = 1; i <= rem; i++) {22 fact *= i;23 }24 sum += fact;25 temp /= 10;26 }2728 if (sum == n) {29 printf("%d is a Krishnamurthy Number\n", n);30 } else {31 printf("%d is not a Krishnamurthy Number\n", n);32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_059_write_a_program_to_find_out_if_a_number_is_krishnamurty_number_or_not.c -o prog (add -lm if using <math.h>).
---
Question 60: Write a program to find out the binary to decimal and vice versa
Problem Statement: Write a C program to write a program to find out the binary to decimal and vice versa.
1/*2 * Program #60: Write a program to find out the binary to decimal and vice versa3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Use base 2 multiplication and modulo operations.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>1011int main() {12 int choice;13 long long n;1415 printf("1. Binary to Decimal\n2. Decimal to Binary\nEnter choice: ");16 scanf("%d", &choice);1718 printf("Enter the number: ");19 scanf("%lld", &n);2021 if (choice == 1) {22 int dec = 0, i = 0, rem;23 while (n != 0) {24 rem = n % 10;25 n /= 10;26 dec += rem * pow(2, i);27 ++i;28 }29 printf("Decimal equivalent = %d\n", dec);30 }31 else if (choice == 2) {32 long long bin = 0;33 int rem, i = 1;34 while (n != 0) {35 rem = n % 2;36 n /= 2;37 bin += rem * i;38 i *= 10;39 }40 printf("Binary equivalent = %lld\n", bin);41 } else {42 printf("Invalid choice!\n");43 }4445 return 0;46}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_060_write_a_program_to_find_out_the_binary_to_decimal_and_vice_versa.c -o prog (add -lm if using <math.h>).
---
Question 61: Write a program to calculate the gcd of two numbers
Problem Statement: Write a C program to write a program to calculate the gcd of two numbers.
1/*2 * Program #61: Write a program to calculate the GCD of two numbers3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Euclidean algorithm.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int a, b, temp;1213 printf("Enter two numbers: ");14 scanf("%d %d", &a, &b);1516 printf("GCD of %d and %d is ", a, b);1718 // Euclidean algorithm19 while (b != 0) {20 temp = b;21 b = a % b;22 a = temp;23 }2425 printf("%d\n", a);2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_061_write_a_program_to_calculate_the_gcd_of_two_numbers.c -o prog (add -lm if using <math.h>).
---
Question 62: Write a program to print the sum of series 1 1 2 1 3 1 n
Problem Statement: Write a C program to write a program to print the sum of series 1 1 2 1 3 1 n.
1/*2 * Program #62: Write a program to print the sum of series 1 + 1/2 + 1/3 + ... + 1/n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Harmonic series sum.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;12 float sum = 0.0;1314 printf("Enter the value of n: ");15 scanf("%d", &n);1617 for (int i = 1; i <= n; i++) {18 sum += 1.0 / i;19 }2021 printf("Sum of the harmonic series = %f\n", sum);2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_062_write_a_program_to_print_the_sum_of_series_1_1_2_1_3_1_n.c -o prog (add -lm if using <math.h>).
---
Question 63: Write a program to print the sum of series 1 2 2 3 3 n n
Problem Statement: Write a C program to write a program to print the sum of series 1 2 2 3 3 n n.
1/*2 * Program #63: Write a program to print the sum of series 1 + 2^2 + 3^3 + ... + n^n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Requires pow() function from math.h.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>1011int main() {12 int n;13 long long sum = 0;1415 printf("Enter the value of n: ");16 scanf("%d", &n);1718 for (int i = 1; i <= n; i++) {19 sum += pow(i, i);20 }2122 printf("Sum of the series = %lld\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_063_write_a_program_to_print_the_sum_of_series_1_2_2_3_3_n_n.c -o prog (add -lm if using <math.h>).
---
Question 64: Write a program to print the sum of series 1 2 2 3 3 4 n 1 n
Problem Statement: Write a C program to write a program to print the sum of series 1 2 2 3 3 4 n 1 n.
1/*2 * Program #64: Write a program to print the sum of series 1/2 + 2/3 + 3/4 + ... + (n-1)/n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Fractional series addition.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;12 float sum = 0.0;1314 printf("Enter the value of n: ");15 scanf("%d", &n);1617 for (int i = 1; i < n; i++) {18 sum += (float)i / (i + 1);19 }2021 printf("Sum of the series = %f\n", sum);2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_064_write_a_program_to_print_the_sum_of_series_1_2_2_3_3_4_n_1_n.c -o prog (add -lm if using <math.h>).
---
Question 65: Write a program to print the sum of series 1 2 2 2 3 3 3 n n n
Problem Statement: Write a C program to write a program to print the sum of series 1 2 2 2 3 3 3 n n n.
1/*2 * Program #65: Write a program to print the sum of series 1 + 2^2/2 + 3^3/3 + ... + n^n/n3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Simplify to 1 + 2 + 3^2 + ... + n^(n-1).6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>1011int main() {12 int n;13 double sum = 0.0;1415 printf("Enter the value of n: ");16 scanf("%d", &n);1718 for (int i = 1; i <= n; i++) {19 sum += pow(i, i) / i; // Equivalent to pow(i, i-1)20 }2122 printf("Sum of the series = %.2lf\n", sum);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_065_write_a_program_to_print_the_sum_of_series_1_2_2_2_3_3_3_n_n_n.c -o prog (add -lm if using <math.h>).
---
Question 66: Write a program to print the sum of series x x 3 3 x 5 5 x 7 7
Problem Statement: Write a C program to write a program to print the sum of series x x 3 3 x 5 5 x 7 7.
1/*2 * Program #66: Write a program to print the sum of series x - x^3/3! + x^5/5! - x^7/7! + ...3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: sin(x) Maclaurin series approximation.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int x, n;12 double sum, term;1314 printf("Enter value of x and n: ");15 scanf("%d %d", &x, &n);1617 term = x;18 sum = x;1920 for (int i = 1; i < n; i++) {21 term = term * -1 * x * x / ((2 * i) * (2 * i + 1));22 sum += term;23 }2425 printf("Sum of the sin(x) series = %.4lf\n", sum);2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_066_write_a_program_to_print_the_sum_of_series_x_x_3_3_x_5_5_x_7_7.c -o prog (add -lm if using <math.h>).
---
Question 67: Write a program to repeatedly divide a number by 2 and print the quotient
Problem Statement: Write a C program to write a program to repeatedly divide a number by 2 and print the quotient.
1/*2 * Program #67: Write a program to repeatedly divide a number by 2 and print the quotient3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Use a while loop until n == 0.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;1213 printf("Enter a number: ");14 scanf("%d", &n);1516 while (n > 0) {17 n = n / 2;18 printf("Quotient = %d\n", n);19 }2021 return 0;22}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_067_write_a_program_to_repeatedly_divide_a_number_by_2_and_print_the_quotient.c -o prog (add -lm if using <math.h>).
---
Question 68: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #68: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 162 374 5 687 8 9 109 */1011"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1213int main() {14 int rows, num = 1;1516 printf("Enter the number of rows: ");17 scanf("%d", &rows);1819 for (int i = 1; i <= rows; i++) {20 for (int j = 1; j <= i; j++) {21 printf("%d ", num);22 num++;23 }24 printf("\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_068_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 69: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #69: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 162 374 5 687 8 9 109 */1011"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1213int main() {14 int rows, i = 1, num = 1;1516 printf("Enter the number of rows: ");17 scanf("%d", &rows);1819 while (i <= rows) {20 int j = 1;21 while (j <= i) {22 printf("%d ", num);23 num++;24 j++;25 }26 printf("\n");27 i++;28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_069_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 70: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #70: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 162273338444495555510 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = 1; i <= rows; i++) {21 for (int j = 1; j <= i; j++) {22 printf("%d", i);23 }24 printf("\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_070_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 71: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #71: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 1622273333384444444955555555510 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = 1; i <= rows; i++) {21 // Prints (2*i - 1) times for each row22 for (int j = 1; j <= (2 * i - 1); j++) {23 printf("%d", i);24 }25 printf("\n");26 }2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_071_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 72: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #72: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 555556444473338229110 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = rows; i >= 1; i--) {21 for (int j = 1; j <= i; j++) {22 printf("%d", i);23 }24 printf("\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_072_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 73: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #73: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: *6***7*****8*******9 */1011"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1213int main() {14 int rows;1516 printf("Enter the number of rows: ");17 scanf("%d", &rows);1819 for (int i = 1; i <= rows; i++) {20 for (int j = 1; j <= (2 * i - 1); j++) {21 printf("*");22 }23 printf("\n");24 }2526 return 0;27}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_073_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 74: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #74: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 16017101801019101011001010111 */1213"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1415int main() {16 int rows;1718 printf("Enter the number of rows: ");19 scanf("%d", &rows);2021 for (int i = 1; i <= rows; i++) {22 for (int j = 1; j <= i; j++) {23 // Alternates 1 and 0 based on position indices24 if ((i + j) % 2 == 0) {25 printf("1");26 } else {27 printf("0");28 }29 }30 printf("\n");31 }3233 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_074_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 75: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #75: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: A6BB7CCC8DDDD9EEEE10 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = 1; i <= rows; i++) {21 for (int j = 1; j <= i; j++) {22 printf("%c", 'A' + (i - 1));23 }24 printf("\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_075_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 76: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #76: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 565475438543295432110 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = rows; i >= 1; i--) {21 for (int j = rows; j >= i; j--) {22 printf("%d", j);23 }24 printf("\n");25 }2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_076_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 77: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #77: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 16 227 3338 4444955555 (Right-Aligned Variation)10 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = 1; i <= rows; i++) {21 for (int space = 1; space <= rows - i; space++) {22 printf(" ");23 }24 for (int j = 1; j <= i; j++) {25 printf("%d", i);26 }27 printf("\n");28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_077_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 78: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #78: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 555556 44447 3338 229 1 (Right-Aligned Inverted Variation)10 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = rows; i >= 1; i--) {21 for (int space = 1; space <= rows - i; space++) {22 printf(" ");23 }24 for (int j = 1; j <= i; j++) {25 printf("%d", i);26 }27 printf("\n");28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_078_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 79: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #79: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 5645473454382345432912345432110 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = rows; i >= 1; i--) {21 // Print ascending part22 for (int j = i; j <= rows; j++) {23 printf("%d", j);24 }25 // Print descending part26 for (int j = rows - 1; j >= i; j--) {27 printf("%d", j);28 }29 printf("\n");30 }3132 return 0;33}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_079_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 80: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #80: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: 564 5 473 4 5 4 382 3 4 5 4 3 291 2 3 4 5 4 3 2 1 (Spaced version of Pattern 11)10 */1112"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1314int main() {15 int rows;1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = rows; i >= 1; i--) {21 for (int j = i; j <= rows; j++) {22 printf("%d ", j);23 }24 for (int j = rows - 1; j >= i; j--) {25 printf("%d ", j);26 }27 printf("\n");28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_080_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 81: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #81: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: (1,1)(1,2)(1,3)...(1,n)6(2,1)(2,2)(2,3)...(2,n)7(n,1)(n,2)(n,3)...(n,n)8 */910"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1112int main() {13 int n;1415 printf("Enter the value of n: ");16 scanf("%d", &n);1718 for (int i = 1; i <= n; i++) {19 for (int j = 1; j <= n; j++) {20 printf("(%d,%d)", i, j);21 }22 printf("\n");23 }2425 return 0;26}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_081_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 82: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #82: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: A6BC7DEF8GHIJ9 */1011"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>1213int main() {14 int rows;15 char ch = 'A';1617 printf("Enter the number of rows: ");18 scanf("%d", &rows);1920 for (int i = 1; i <= rows; i++) {21 for (int j = 1; j <= i; j++) {22 printf("%c", ch);23 ch++; // Increment character for next position24 }25 printf("\n");26 }2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_082_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Question 83: Pattern where the row is given by the user
Problem Statement: Write a C program to pattern where the row is given by the user.
1/*2 * Program #83: Pattern(where the row is given by the user)3 * Unit 2: Loops, Series & Number Patterns4 *5 * Explanation: Varied nested loop structure(e.g., Generic Diamond Pattern)6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n;1213 printf("Enter half the number of rows for diamond: ");14 scanf("%d", &n);1516 // Upper half17 for (int i = 1; i <= n; i++) {18 for (int space = 1; space <= n - i; space++)19 printf(" ");20 for (int j = 1; j <= 2 * i - 1; j++)21 printf("*");22 printf("\n");23 }2425 // Lower half26 for (int i = n - 1; i >= 1; i--) {27 for (int space = 1; space <= n - i; space++)28 printf(" ");29 for (int j = 1; j <= 2 * i - 1; j++)30 printf("*");31 printf("\n");32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_083_pattern_where_the_row_is_given_by_the_user.c -o prog (add -lm if using <math.h>).
---
Unit 3: Functions, Storage Classes, Pointers & 1D Arrays (Q84 – Q112)
Question 84: Gcd of two numbers
Problem Statement: Write a C program to gcd of two numbers.
1/*2 * Program #84: GCD of two numbers3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Extract GCD calculation into a separate function.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int findGCD(int a, int b) {11 while (b != 0) {12 int temp = b;13 b = a % b;14 a = temp;15 }16 return a;17}1819int main() {20 int num1, num2;2122 printf("Enter two numbers: ");23 scanf("%d %d", &num1, &num2);2425 printf("GCD of %d and %d is %d\n", num1, num2, findGCD(num1, num2));2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_084_gcd_of_two_numbers.c -o prog (add -lm if using <math.h>).
---
Question 85: Max and min of 2 numbers
Problem Statement: Write a C program to max and min of 2 numbers.
1/*2 * Program #85: Max and Min of 2 numbers3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Pass two variables to a function to compute bounds.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void findMaxMin(int a, int b) {11 if (a > b) {12 printf("Max: %d, Min: %d\n", a, b);13 } else if (b > a) {14 printf("Max: %d, Min: %d\n", b, a);15 } else {16 printf("Both numbers are equal to %d\n", a);17 }18}1920int main() {21 int x, y;2223 printf("Enter two numbers: ");24 scanf("%d %d", &x, &y);2526 findMaxMin(x, y);2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_085_max_and_min_of_2_numbers.c -o prog (add -lm if using <math.h>).
---
Question 86: Fibonacci series
Problem Statement: Write a C program to fibonacci series.
1/*2 * Program #86: Fibonacci Series3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Pass the number of terms to the function.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void printFibonacci(int terms) {11 int a = 0, b = 1, next;1213 printf("Fibonacci Series: ");14 for (int i = 1; i <= terms; i++) {15 printf("%d ", a);16 next = a + b;17 a = b;18 b = next;19 }20 printf("\n");21}2223int main() {24 int n;2526 printf("Enter the number of terms: ");27 scanf("%d", &n);2829 printFibonacci(n);3031 return 0;32}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_086_fibonacci_series.c -o prog (add -lm if using <math.h>).
---
Question 87: Perform the following activities in a given range count sum of odd even numbers
Problem Statement: Write a C program to perform the following activities in a given range count sum of odd even numbers.
1/*2 * Program #87: Perform the following activities in a given range: Count/Sum of Odd/Even numbers3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Calculate and print aggregations inside the function.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void analyzeRange(int start, int end) {11 int evenCount = 0, oddCount = 0;12 int evenSum = 0, oddSum = 0;1314 for (int i = start; i <= end; i++) {15 if (i % 2 == 0) {16 evenCount++;17 evenSum += i;18 } else {19 oddCount++;20 oddSum += i;21 }22 }2324 printf("Even Numbers: Count = %d, Sum = %d\n", evenCount, evenSum);25 printf("Odd Numbers: Count = %d, Sum = %d\n", oddCount, oddSum);26}2728int main() {29 int start, end;3031 printf("Enter start and end of range: ");32 scanf("%d %d", &start, &end);3334 analyzeRange(start, end);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_087_perform_the_following_activities_in_a_given_range_count_sum_of_odd_even_numbers.c -o prog (add -lm if using <math.h>).
---
Question 88: Factorial of a number
Problem Statement: Write a C program to factorial of a number.
1/*2 * Program #88: Factorial of a number3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Function returns the factorial value to main.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910unsigned long long calculateFactorial(int n) {11 unsigned long long fact = 1;12 for (int i = 1; i <= n; i++) {13 fact *= i;14 }15 return fact;16}1718int main() {19 int num;2021 printf("Enter a positive integer: ");22 scanf("%d", &num);2324 if (num < 0) {25 printf("Factorial of a negative number doesn't exist.\n");26 } else {27 printf("Factorial of %d = %llu\n", num, calculateFactorial(num));28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_088_factorial_of_a_number.c -o prog (add -lm if using <math.h>).
---
Question 89: Prove whether a number is adam number or not
Problem Statement: Write a C program to prove whether a number is adam number or not.
1/*2 * Program #89: Prove whether a number is Adam Number or Not3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: A number is adam number if its squared then reversed then again we find it's square root and again reverse the result number and see if it is equal to the original number... EXAMPLE: 126 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910// Function to reverse a number11int reverse(int n) {12 int rev = 0;13 while (n > 0) {14 rev = rev * 10 + n % 10;15 n /= 10;16 }17 return rev;18}1920// Function to check Adam number21// Alternatively: Square of num == Reverse of the square of the reverse of num22int isAdam(int n) {23 int sq = n * n;24 int rev = reverse(n);25 int revSq = rev * rev;2627 if (sq == reverse(revSq))28 return 1;29 return 0;30}3132int main() {33 int num;34 printf("Enter a number: ");35 scanf("%d", &num);3637 if (isAdam(num)) {38 printf("%d is an Adam Number.\n", num);39 } else {40 printf("%d is not an Adam Number.\n", num);41 }42 return 0;43}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_089_prove_whether_a_number_is_adam_number_or_not.c -o prog (add -lm if using <math.h>).
---
Question 90: Print pascal s triangle use function where n is entered by user
Problem Statement: Write a C program to print pascal s triangle use function where n is entered by user.
1/*2 * Program #90: Print Pascal's Triangle(USE FUNCTION) [where n is entered by user]3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Function to compute nCr and print elements in triangle shape.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int factorial(int n) {11 int fact = 1;12 for (int i = 1; i <= n; i++)13 fact *= i;14 return fact;15}1617int nCr(int n, int r) {18 return factorial(n) / (factorial(r) * factorial(n - r));19}2021int main() {22 int rows;23 printf("Enter number of rows: ");24 scanf("%d", &rows);2526 for (int i = 0; i < rows; i++) {27 for (int space = 0; space < rows - i - 1; space++) {28 printf(" ");29 }30 for (int j = 0; j <= i; j++) {31 printf("%4d", nCr(i, j));32 }33 printf("\n");34 }35 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_090_print_pascal_s_triangle_use_function_where_n_is_entered_by_user.c -o prog (add -lm if using <math.h>).
---
Question 91: Increment values and print result in main and in function
Problem Statement: Write a C program to increment values and print result in main and in function.
1/*2 * Program #91: Increment values and print result in main() and in function3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Pass addresses to modify values inside the function context.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void incrementValues(int *a, int *b) {11 (*a)++;12 (*b)++;13 printf("Inside function: a = %d, b = %d\n", *a, *b);14}1516int main() {17 int x = 10, y = 20;1819 printf("Before function call in main: x = %d, y = %d\n", x, y);2021 incrementValues(&x, &y);2223 printf("After function call in main: x = %d, y = %d\n", x, y);24 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_091_increment_values_and_print_result_in_main_and_in_function.c -o prog (add -lm if using <math.h>).
---
Question 92: Interchange two variables using call by value and call by reference
Problem Statement: Write a C program to interchange two variables using call by value and call by reference.
1/*2 * Program #92: Interchange two variables using call by value and call by reference3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: Demonstrate difference in mutability.6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void swapByValue(int a, int b) {11 int temp = a;12 a = b;13 b = temp;14 printf("Inside swapByValue: a = %d, b = %d\n", a, b);15}1617void swapByReference(int *a, int *b) {18 int temp = *a;19 *a = *b;20 *b = temp;21 printf("Inside swapByReference: a = %d, b = %d\n", *a, *b);22}2324int main() {25 int x = 5, y = 10;2627 printf("Original values: x = %d, y = %d\n\n", x, y);2829 swapByValue(x, y);30 printf("After swapByValue in main: x = %d, y = %d(Unchanged)\n\n", x, y);3132 swapByReference(&x, &y);33 printf("After swapByReference in main: x = %d, y = %d(Changed)\n", x, y);3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_092_interchange_two_variables_using_call_by_value_and_call_by_reference.c -o prog (add -lm if using <math.h>).
---
Question 93: Take a 5 digit number reverse it and check whether palindrome or not
Problem Statement: Write a C program to take a 5 digit number reverse it and check whether palindrome or not.
1/*2 * Program #93: Take a 5 digit number, reverse it, and check whether palindrome or not3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void checkPalindrome(int *num) {11 int temp = *num;12 int reversed = 0;1314 while (temp > 0) {15 reversed = reversed * 10 + (temp % 10);16 temp /= 10;17 }1819 printf("Reversed Number = %d\n", reversed);20 if (*num == reversed) {21 printf("%d is a Palindrome.\n", *num);22 } else {23 printf("%d is not a Palindrome.\n", *num);24 }25}2627int main() {28 int n;29 printf("Enter a 5-digit number: ");30 scanf("%d", &n);3132 checkPalindrome(&n);33 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_093_take_a_5_digit_number_reverse_it_and_check_whether_palindrome_or_not.c -o prog (add -lm if using <math.h>).
---
Question 94: Update the value of a variable using call by value and call by reference
Problem Statement: Write a C program to update the value of a variable using call by value and call by reference.
1/*2 * Program #94: Update the value of a variable using call by value and call by reference3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void updateByValue(int val) {11 val += 50;12}1314void updateByReference(int *val) {15 *val += 50;16}1718int main() {19 int num = 100;2021 printf("Initial value: %d\n", num);2223 updateByValue(num);24 printf("After updateByValue: %d\n", num);2526 updateByReference(&num);27 printf("After updateByReference: %d\n", num);2829 return 0;30}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_094_update_the_value_of_a_variable_using_call_by_value_and_call_by_reference.c -o prog (add -lm if using <math.h>).
---
Question 95: Sum the elements of an array and print the sum in the main program
Problem Statement: Write a C program to sum the elements of an array and print the sum in the main program.
1/*2 * Program #95: Sum the elements of an array and print the sum in the main program3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void updateByValue(int val) {11 val += 50;12}1314void updateByReference(int *val) {15 *val += 50;16}1718int main() {19 int num = 100;2021 printf("Initial value: %d\n", num);2223 updateByValue(num);24 printf("After updateByValue: %d\n", num);2526 updateByReference(&num);27 printf("After updateByReference: %d\n", num);2829 return 0;30}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_095_sum_the_elements_of_an_array_and_print_the_sum_in_the_main_program.c -o prog (add -lm if using <math.h>).
---
Question 96: Increment 10 elements in an array by 1 in separate function
Problem Statement: Write a C program to increment 10 elements in an array by 1 in separate function.
1/*2 * Program #96: Increment 10 elements in an array by 1 in separate function3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void incrementArray(int *arr, int size) {11 for (int i = 0; i < size; i++) {12 *(arr + i) += 1;13 }14}1516int main() {17 int arr[10];1819 printf("Enter 10 elements:\n");20 for (int i = 0; i < 10; i++) {21 scanf("%d", &arr[i]);22 }2324 incrementArray(arr, 10);2526 printf("Array after incrementing by 1:\n");27 for (int i = 0; i < 10; i++) {28 printf("%d ", arr[i]);29 }30 printf("\n");3132 return 0;33}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_096_increment_10_elements_in_an_array_by_1_in_separate_function.c -o prog (add -lm if using <math.h>).
---
Question 97: To find the max and min of 10 elements in an array
Problem Statement: Write a C program to to find the max and min of 10 elements in an array.
1/*2 * Program #97: To find the max and min of 10 elements in an array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void findMaxMin(int *arr, int size, int *max, int *min) {11 *max = *arr;12 *min = *arr;1314 for (int i = 1; i < size; i++) {15 if (*(arr + i) > *max)16 *max = *(arr + i);17 if (*(arr + i) < *min)18 *min = *(arr + i);19 }20}2122int main() {23 int arr[10], max, min;2425 printf("Enter 10 elements:\n");26 for (int i = 0; i < 10; i++) {27 scanf("%d", &arr[i]);28 }2930 findMaxMin(arr, 10, &max, &min);3132 printf("Maximum Element = %d\n", max);33 printf("Minimum Element = %d\n", min);3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_097_to_find_the_max_and_min_of_10_elements_in_an_array.c -o prog (add -lm if using <math.h>).
---
Question 98: Count odd and even numbers and sum them separately
Problem Statement: Write a C program to count odd and even numbers and sum them separately.
1/*2 * Program #98: Count odd and even numbers and sum them separately3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void analyzeArray(int *arr, int size) {11 int evenCount = 0, oddCount = 0;12 int evenSum = 0, oddSum = 0;1314 for (int i = 0; i < size; i++) {15 if (*(arr + i) % 2 == 0) {16 evenCount++;17 evenSum += *(arr + i);18 } else {19 oddCount++;20 oddSum += *(arr + i);21 }22 }2324 printf("Even Elements: Count = %d, Sum = %d\n", evenCount, evenSum);25 printf("Odd Elements: Count = %d, Sum = %d\n", oddCount, oddSum);26}2728int main() {29 int n, arr[50];3031 printf("Enter number of elements: ");32 scanf("%d", &n);3334 printf("Enter %d elements: ", n);35 for (int i = 0; i < n; i++) {36 scanf("%d", &arr[i]);37 }3839 analyzeArray(arr, n);4041 return 0;42}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_098_count_odd_and_even_numbers_and_sum_them_separately.c -o prog (add -lm if using <math.h>).
---
Question 99: Reverse the elements in an array and print them
Problem Statement: Write a C program to reverse the elements in an array and print them.
1/*2 * Program #99: Reverse the elements in an array and print them3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void reverseArray(int *arr, int size) {11 int temp;12 for (int i = 0; i < size / 2; i++) {13 temp = *(arr + i);14 *(arr + i) = *(arr + size - 1 - i);15 *(arr + size - 1 - i) = temp;16 }17}1819int main() {20 int n, arr[50];2122 printf("Enter number of elements: ");23 scanf("%d", &n);2425 printf("Enter %d elements: ", n);26 for (int i = 0; i < n; i++) {27 scanf("%d", &arr[i]);28 }2930 reverseArray(arr, n);3132 printf("Reversed Array: ");33 for (int i = 0; i < n; i++) {34 printf("%d ", *(arr + i));35 }36 printf("\n");3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_099_reverse_the_elements_in_an_array_and_print_them.c -o prog (add -lm if using <math.h>).
---
Question 100: Copy the elements of an array from one array to another
Problem Statement: Write a C program to copy the elements of an array from one array to another.
1/*2 * Program #100: Copy the elements of an array from one array to another3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void copyArray(int *source, int *dest, int size) {11 for (int i = 0; i < size; i++) {12 *(dest + i) = *(source + i);13 }14}1516int main() {17 int n, source[50], dest[50];1819 printf("Enter number of elements: ");20 scanf("%d", &n);2122 printf("Enter %d elements: ", n);23 for (int i = 0; i < n; i++) {24 scanf("%d", &source[i]);25 }2627 copyArray(source, dest, n);2829 printf("Elements in destination array: ");30 for (int i = 0; i < n; i++) {31 printf("%d ", *(dest + i));32 }33 printf("\n");3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_100_copy_the_elements_of_an_array_from_one_array_to_another.c -o prog (add -lm if using <math.h>).
---
Question 101: Sort an array in ascending and descending order
Problem Statement: Write a C program to sort an array in ascending and descending order.
1/*2 * Program #101: Sort an array in ascending and descending order3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void sortAscending(int *arr, int size) {11 int temp;12 for (int i = 0; i < size - 1; i++) {13 for (int j = 0; j < size - i - 1; j++) {14 if (*(arr + j) > *(arr + j + 1)) {15 temp = *(arr + j);16 *(arr + j) = *(arr + j + 1);17 *(arr + j + 1) = temp;18 }19 }20 }21}2223void sortDescending(int *arr, int size) {24 int temp;25 for (int i = 0; i < size - 1; i++) {26 for (int j = 0; j < size - i - 1; j++) {27 if (*(arr + j) < *(arr + j + 1)) {28 temp = *(arr + j);29 *(arr + j) = *(arr + j + 1);30 *(arr + j + 1) = temp;31 }32 }33 }34}3536int main() {37 int n, arr[50];3839 printf("Enter number of elements: ");40 scanf("%d", &n);4142 printf("Enter %d elements: ", n);43 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);4445 sortAscending(arr, n);46 printf("Ascending Order: ");47 for (int i = 0; i < n; i++) printf("%d ", *(arr + i));4849 sortDescending(arr, n);50 printf("\nDescending Order: ");51 for (int i = 0; i < n; i++) printf("%d ", *(arr + i));52 printf("\n");5354 return 0;55}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_101_sort_an_array_in_ascending_and_descending_order.c -o prog (add -lm if using <math.h>).
---
Question 102: Merge two sorted arrays
Problem Statement: Write a C program to merge two sorted arrays.
1/*2 * Program #102: Merge two sorted arrays3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void mergeSortedArrays(int *arr1, int size1, int *arr2, int size2, int *merged) {11 int i = 0, j = 0, k = 0;1213 while (i < size1 && j < size2) {14 if (*(arr1 + i) < *(arr2 + j)) {15 *(merged + k++) = *(arr1 + i++);16 } else {17 *(merged + k++) = *(arr2 + j++);18 }19 }2021 while (i < size1) *(merged + k++) = *(arr1 + i++);22 while (j < size2) *(merged + k++) = *(arr2 + j++);23}2425int main() {26 int n1, n2, arr1[50], arr2[50], merged[100];2728 printf("Enter size of 1st sorted array: ");29 scanf("%d", &n1);30 printf("Enter elements: ");31 for (int i = 0; i < n1; i++) scanf("%d", &arr1[i]);3233 printf("Enter size of 2nd sorted array: ");34 scanf("%d", &n2);35 printf("Enter elements: ");36 for (int i = 0; i < n2; i++) scanf("%d", &arr2[i]);3738 mergeSortedArrays(arr1, n1, arr2, n2, merged);3940 printf("Merged Sorted Array: ");41 for (int i = 0; i < n1 + n2; i++) {42 printf("%d ", *(merged + i));43 }44 printf("\n");4546 return 0;47}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_102_merge_two_sorted_arrays.c -o prog (add -lm if using <math.h>).
---
Question 103: Print the unique elements in an array
Problem Statement: Write a C program to print the unique elements in an array.
1/*2 * Program #103: Print the unique elements in an array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void printUnique(int *arr, int size) {11 printf("Unique elements: ");12 for (int i = 0; i < size; i++) {13 int count = 0;14 for (int j = 0; j < size; j++) {15 if (*(arr + i) == *(arr + j)) {16 count++;17 }18 }19 if (count == 1) {20 printf("%d ", *(arr + i));21 }22 }23 printf("\n");24}2526int main() {27 int n, arr[50];2829 printf("Enter number of elements: ");30 scanf("%d", &n);3132 printf("Enter %d elements: ", n);33 for (int i = 0; i < n; i++) {34 scanf("%d", &arr[i]);35 }3637 printUnique(arr, n);3839 return 0;40}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_103_print_the_unique_elements_in_an_array.c -o prog (add -lm if using <math.h>).
---
Question 104: Find out the 2nd largest element in the array
Problem Statement: Write a C program to find out the 2nd largest element in the array.
1/*2 * Program #104: Find out the 2nd largest element in the array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: POINTERS6USE FUNCTION and POINTERS and ARRAY7"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>89int secondLargest(int *arr, int size) {10 int max1 = -999999, max2 = -999999;1112 for (int i = 0; i < size; i++) {13 if (*(arr + i) > max1) {14 max2 = max1;15 max1 = *(arr + i);16 } else if (*(arr + i) > max2 && *(arr + i) < max1) {17 max2 = *(arr + i);18 }19 }20 return max2;21}2223int main() {24 int n, arr[50];2526 printf("Enter number of elements: ");27 scanf("%d", &n);2829 printf("Enter %d elements: ", n);30 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);3132 int result = secondLargest(arr, n);33 printf("2nd Largest Element: %d\n", result);3435 return 0;36} USE FUNCTION and POINTERS and ARRAY37 */3839"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>4041int secondLargest(int *arr, int size) {42 int max1 = -999999, max2 = -999999;4344 for (int i = 0; i < size; i++) {45 if (*(arr + i) > max1) {46 max2 = max1;47 max1 = *(arr + i);48 } else if (*(arr + i) > max2 && *(arr + i) < max1) {49 max2 = *(arr + i);50 }51 }52 return max2;53}5455int main() {56 int n, arr[50];5758 printf("Enter number of elements: ");59 scanf("%d", &n);6061 printf("Enter %d elements: ", n);62 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);6364 int result = secondLargest(arr, n);65 printf("2nd Largest Element: %d\n", result);6667 return 0;68}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_104_find_out_the_2nd_largest_element_in_the_array.c -o prog (add -lm if using <math.h>).
---
Question 105: Find out the 2nd smallest element in the array
Problem Statement: Write a C program to find out the 2nd smallest element in the array.
1/*2 * Program #105: Find out the 2nd smallest element in the array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int secondSmallest(int *arr, int size) {11 int min1 = 999999, min2 = 999999;1213 for (int i = 0; i < size; i++) {14 if (*(arr + i) < min1) {15 min2 = min1;16 min1 = *(arr + i);17 } else if (*(arr + i) < min2 && *(arr + i) > min1) {18 min2 = *(arr + i);19 }20 }21 return min2;22}2324int main() {25 int n, arr[50];2627 printf("Enter number of elements: ");28 scanf("%d", &n);2930 printf("Enter %d elements: ", n);31 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);3233 int result = secondSmallest(arr, n);34 printf("2nd Smallest Element: %d\n", result);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_105_find_out_the_2nd_smallest_element_in_the_array.c -o prog (add -lm if using <math.h>).
---
Question 106: Add two arrays and print the 3rd array
Problem Statement: Write a C program to add two arrays and print the 3rd array.
1/*2 * Program #106: Add two arrays and print the 3rd array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int secondSmallest(int *arr, int size) {11 int min1 = 999999, min2 = 999999;1213 for (int i = 0; i < size; i++) {14 if (*(arr + i) < min1) {15 min2 = min1;16 min1 = *(arr + i);17 } else if (*(arr + i) < min2 && *(arr + i) > min1) {18 min2 = *(arr + i);19 }20 }21 return min2;22}2324int main() {25 int n, arr[50];2627 printf("Enter number of elements: ");28 scanf("%d", &n);2930 printf("Enter %d elements: ", n);31 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);3233 int result = secondSmallest(arr, n);34 printf("2nd Smallest Element: %d\n", result);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_106_add_two_arrays_and_print_the_3rd_array.c -o prog (add -lm if using <math.h>).
---
Question 107: Print values and address of two variables
Problem Statement: Write a C program to print values and address of two variables.
1/*2 * Program #107: Print values and address of two variables3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int secondSmallest(int *arr, int size) {11 int min1 = 999999, min2 = 999999;1213 for (int i = 0; i < size; i++) {14 if (*(arr + i) < min1) {15 min2 = min1;16 min1 = *(arr + i);17 } else if (*(arr + i) < min2 && *(arr + i) > min1) {18 min2 = *(arr + i);19 }20 }21 return min2;22}2324int main() {25 int n, arr[50];2627 printf("Enter number of elements: ");28 scanf("%d", &n);2930 printf("Enter %d elements: ", n);31 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);3233 int result = secondSmallest(arr, n);34 printf("2nd Smallest Element: %d\n", result);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_107_print_values_and_address_of_two_variables.c -o prog (add -lm if using <math.h>).
---
Question 108: Swapping without a 3rd variable
Problem Statement: Write a C program to swapping without a 3rd variable.
1/*2 * Program #108: Swapping without a 3rd variable3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void swapPointers(int *p1, int *p2) {11 *p1 = *p1 + *p2;12 *p2 = *p1 - *p2;13 *p1 = *p1 - *p2;14}1516int main() {17 int x, y;1819 printf("Enter two numbers: ");20 scanf("%d %d", &x, &y);2122 printf("Before Swapping: x = %d, y = %d\n", x, y);2324 swapPointers(&x, &y);2526 printf("After Swapping: x = %d, y = %d\n", x, y);2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_108_swapping_without_a_3rd_variable.c -o prog (add -lm if using <math.h>).
---
Question 109: Fahrenheit to celcius
Problem Statement: Write a C program to fahrenheit to celcius.
1/*2 * Program #109: Fahrenheit to celcius3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void convertToCelsius(float *f, float *c) {11 *c = (*f - 32) * 5.0 / 9.0;12}1314int main() {15 float fahrenheit, celsius;1617 printf("Enter temperature in Fahrenheit: ");18 scanf("%f", &fahrenheit);1920 convertToCelsius(&fahrenheit, &celsius);2122 printf("Temperature in Celsius: %.2f\n", celsius);2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_109_fahrenheit_to_celcius.c -o prog (add -lm if using <math.h>).
---
Question 110: Factorial of a number
Problem Statement: Write a C program to factorial of a number.
1/*2 * Program #110: Factorial of a number3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE POINTERS6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void findFactorial(int *n, unsigned long long *fact) {11 *fact = 1;12 for (int i = 1; i <= *n; i++) {13 *fact *= i;14 }15}1617int main() {18 int num;19 unsigned long long result;2021 printf("Enter a number: ");22 scanf("%d", &num);2324 findFactorial(&num, &result);2526 printf("Factorial of %d = %llu\n", num, result);2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_110_factorial_of_a_number.c -o prog (add -lm if using <math.h>).
---
Question 111: Count the numbers of duplicate numbers in an array
Problem Statement: Write a C program to count the numbers of duplicate numbers in an array.
1/*2 * Program #111: Count the numbers of duplicate numbers in an array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int countDuplicates(int *arr, int size) {11 int count = 0;12 for (int i = 0; i < size; i++) {13 for (int j = i + 1; j < size; j++) {14 if (*(arr + i) == *(arr + j)) {15 count++;16 break; // Prevent double counting the same duplicate element pair17 }18 }19 }20 return count;21}2223int main() {24 int n, arr[50];2526 printf("Enter number of elements: ");27 scanf("%d", &n);2829 printf("Enter %d elements: ", n);30 for (int i = 0; i < n; i++) scanf("%d", &arr[i]);3132 int duplicates = countDuplicates(arr, n);3334 printf("Total duplicate elements = %d\n", duplicates);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_111_count_the_numbers_of_duplicate_numbers_in_an_array.c -o prog (add -lm if using <math.h>).
---
Question 112: Take two arrays merge them sort the resultant array and eliminate duplicate elements in another array
Problem Statement: Write a C program to take two arrays merge them sort the resultant array and eliminate duplicate elements in another array.
1/*2 * Program #112: Take two arrays, merge them, sort the resultant array and eliminate duplicate elements in another array3 * Unit 3: Functions, Pointers & Arrays4 *5 * Explanation: USE FUNCTION and POINTERS and ARRAY6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void processArrays(int *a, int n1, int *b, int n2, int *res, int *n3) {11 int total = n1 + n2, temp;1213 // Merge14 for (int i = 0; i < n1; i++) *(res + i) = *(a + i);15 for (int i = 0; i < n2; i++) *(res + n1 + i) = *(b + i);1617 // Sort18 for (int i = 0; i < total - 1; i++) {19 for (int j = 0; j < total - i - 1; j++) {20 if (*(res + j) > *(res + j + 1)) {21 temp = *(res + j);22 *(res + j) = *(res + j + 1);23 *(res + j + 1) = temp;24 }25 }26 }2728 // Eliminate Duplicates in place29 int k = 0;30 for (int i = 0; i < total; i++) {31 if (i == 0 || *(res + i) != *(res + i - 1)) {32 *(res + k++) = *(res + i);33 }34 }35 *n3 = k; // Update final size36}3738int main() {39 int n1, n2, n3, arr1[50], arr2[50], result[100];4041 printf("Enter size and elements of 1st array: ");42 scanf("%d", &n1);43 for (int i = 0; i < n1; i++) scanf("%d", &arr1[i]);4445 printf("Enter size and elements of 2nd array: ");46 scanf("%d", &n2);47 for (int i = 0; i < n2; i++) scanf("%d", &arr2[i]);4849 processArrays(arr1, n1, arr2, n2, result, &n3);5051 printf("Merged, Sorted, and Unique Array: ");52 for (int i = 0; i < n3; i++) printf("%d ", result[i]);53 printf("\n");5455 return 0;56}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_112_take_two_arrays_merge_them_sort_the_resultant_array_and_eliminate_duplicate_elements_in_another_array.c -o prog (add -lm if using <math.h>).
---
Unit 4: Strings, 2D Matrices & 6 Sorting Algorithms (Q113 – Q143)
Question 113: Input two matrixes and add them
Problem Statement: Write a C program to input two matrixes and add them.
1/*2 * Program #113: input two matrixes and add them3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int r, c, mat1[10][10], mat2[10][10], sum[10][10];1213 printf("Enter number of rows and columns: ");14 scanf("%d %d", &r, &c);1516 printf("Enter elements of 1st matrix:\n");17 for (int i = 0; i < r; i++)18 for (int j = 0; j < c; j++)19 scanf("%d", &mat1[i][j]);2021 printf("Enter elements of 2nd matrix:\n");22 for (int i = 0; i < r; i++)23 for (int j = 0; j < c; j++)24 scanf("%d", &mat2[i][j]);2526 printf("Sum of matrices:\n");27 for (int i = 0; i < r; i++) {28 for (int j = 0; j < c; j++) {29 sum[i][j] = mat1[i][j] + mat2[i][j];30 printf("%d ", sum[i][j]);31 }32 printf("\n");33 }3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_113_input_two_matrixes_and_add_them.c -o prog (add -lm if using <math.h>).
---
Question 114: Input two matrixes and subtract them
Problem Statement: Write a C program to input two matrixes and subtract them.
1/*2 * Program #114: input two matrixes and subtract them3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int r, c, mat1[10][10], mat2[10][10], sum[10][10];1213 printf("Enter number of rows and columns: ");14 scanf("%d %d", &r, &c);1516 printf("Enter elements of 1st matrix:\n");17 for (int i = 0; i < r; i++)18 for (int j = 0; j < c; j++)19 scanf("%d", &mat1[i][j]);2021 printf("Enter elements of 2nd matrix:\n");22 for (int i = 0; i < r; i++)23 for (int j = 0; j < c; j++)24 scanf("%d", &mat2[i][j]);2526 printf("Sum of matrices:\n");27 for (int i = 0; i < r; i++) {28 for (int j = 0; j < c; j++) {29 sum[i][j] = mat1[i][j] + mat2[i][j];30 printf("%d ", sum[i][j]);31 }32 printf("\n");33 }3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_114_input_two_matrixes_and_subtract_them.c -o prog (add -lm if using <math.h>).
---
Question 115: Input two matrixes and multiply them
Problem Statement: Write a C program to input two matrixes and multiply them.
1/*2 * Program #115: input two matrixes and multiply them3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int r1, c1, r2, c2, mat1[10][10], mat2[10][10], product[10][10];1213 printf("Enter rows and cols of 1st matrix: ");14 scanf("%d %d", &r1, &c1);1516 printf("Enter rows and cols of 2nd matrix: ");17 scanf("%d %d", &r2, &c2);1819 if (c1 != r2) {20 printf("Multiplication not possible!\n");21 return 0;22 }2324 printf("Enter elements of 1st matrix:\n");25 for (int i = 0; i < r1; i++)26 for (int j = 0; j < c1; j++)27 scanf("%d", &mat1[i][j]);2829 printf("Enter elements of 2nd matrix:\n");30 for (int i = 0; i < r2; i++)31 for (int j = 0; j < c2; j++)32 scanf("%d", &mat2[i][j]);3334 printf("Product of matrices:\n");35 for (int i = 0; i < r1; i++) {36 for (int j = 0; j < c2; j++) {37 product[i][j] = 0;38 for (int k = 0; k < c1; k++) {39 product[i][j] += mat1[i][k] * mat2[k][j];40 }41 printf("%d ", product[i][j]);42 }43 printf("\n");44 }4546 return 0;47}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_115_input_two_matrixes_and_multiply_them.c -o prog (add -lm if using <math.h>).
---
Question 116: Input two matrixes and transpose them
Problem Statement: Write a C program to input two matrixes and transpose them.
1/*2 * Program #116: input two matrixes and transpose them3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int r, c, mat[10][10], transpose[10][10];1213 printf("Enter number of rows and columns: ");14 scanf("%d %d", &r, &c);1516 printf("Enter elements of matrix:\n");17 for (int i = 0; i < r; i++) {18 for (int j = 0; j < c; j++) {19 scanf("%d", &mat[i][j]);20 }21 }2223 // Finding transpose24 for (int i = 0; i < r; i++) {25 for (int j = 0; j < c; j++) {26 transpose[j][i] = mat[i][j];27 }28 }2930 printf("Transpose of the matrix:\n");31 for (int i = 0; i < c; i++) {32 for (int j = 0; j < r; j++) {33 printf("%d ", transpose[i][j]);34 }35 printf("\n");36 }3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_116_input_two_matrixes_and_transpose_them.c -o prog (add -lm if using <math.h>).
---
Question 117: Input two matrixes and add the column and rows values separately
Problem Statement: Write a C program to input two matrixes and add the column and rows values separately.
1/*2 * Program #117: input two matrixes and add the column and rows values separately3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int r, c, mat[10][10];1213 printf("Enter number of rows and columns: ");14 scanf("%d %d", &r, &c);1516 printf("Enter elements of matrix:\n");17 for (int i = 0; i < r; i++) {18 for (int j = 0; j < c; j++) {19 scanf("%d", &mat[i][j]);20 }21 }2223 // Row Sum24 for (int i = 0; i < r; i++) {25 int rSum = 0;26 for (int j = 0; j < c; j++) {27 rSum += mat[i][j];28 }29 printf("Sum of Row %d = %d\n", i + 1, rSum);30 }3132 // Column Sum33 for (int j = 0; j < c; j++) {34 int cSum = 0;35 for (int i = 0; i < r; i++) {36 cSum += mat[i][j];37 }38 printf("Sum of Column %d = %d\n", j + 1, cSum);39 }4041 return 0;42}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_117_input_two_matrixes_and_add_the_column_and_rows_values_separately.c -o prog (add -lm if using <math.h>).
---
Question 118: Input two matrixes and find the sum of the diagonal elements
Problem Statement: Write a C program to input two matrixes and find the sum of the diagonal elements.
1/*2 * Program #118: input two matrixes and find the sum of the diagonal elements3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: 2D Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int n, mat[10][10], sum = 0;1213 printf("Enter size of square matrix(n x n): ");14 scanf("%d", &n);1516 printf("Enter elements of matrix:\n");17 for (int i = 0; i < n; i++) {18 for (int j = 0; j < n; j++) {19 scanf("%d", &mat[i][j]);20 }21 }2223 // Calculating the sum of principal diagonal elements24 for (int i = 0; i < n; i++) {25 sum += mat[i][i];26 }2728 printf("Sum of the diagonal elements = %d\n", sum);2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_118_input_two_matrixes_and_find_the_sum_of_the_diagonal_elements.c -o prog (add -lm if using <math.h>).
---
Question 119: Find out the length of a string pointer
Problem Statement: Write a C program to find out the length of a string pointer.
1/*2 * Program #119: find out the length of a string(pointer)3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100];12 char *ptr = str;13 int length = 0;1415 printf("Enter a string: ");16 scanf("%[^\n]", str);1718 while (*ptr != '\0') {19 length++;20 ptr++;21 }2223 printf("Length of the string = %d\n", length);2425 return 0;26}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_119_find_out_the_length_of_a_string_pointer.c -o prog (add -lm if using <math.h>).
---
Question 120: Find out the length of a string without pointer
Problem Statement: Write a C program to find out the length of a string without pointer.
1/*2 * Program #120: find out the length of a string(without pointer)3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100];12 int length = 0;1314 printf("Enter a string: ");15 scanf("%[^\n]", str);1617 while (str[length] != '\0') {18 length++;19 }2021 printf("Length of the string = %d\n", length);2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_120_find_out_the_length_of_a_string_without_pointer.c -o prog (add -lm if using <math.h>).
---
Question 121: Copy an array of string
Problem Statement: Write a C program to copy an array of string.
1/*2 * Program #121: copy an array of string3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char source[100], destination[100];12 int i = 0;1314 printf("Enter a string: ");15 scanf("%[^\n]", source);1617 while (source[i] != '\0') {18 destination[i] = source[i];19 i++;20 }21 destination[i] = '\0'; // Null-terminate the destination string2223 printf("Copied String: %s\n", destination);2425 return 0;26}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_121_copy_an_array_of_string.c -o prog (add -lm if using <math.h>).
---
Question 122: Copy the string in reverse order pointer
Problem Statement: Write a C program to copy the string in reverse order pointer.
1/*2 * Program #122: copy the string in reverse order(pointer)3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100], rev[100];12 char *sptr = str, *rptr = rev;13 int len = 0;1415 printf("Enter a string: ");16 scanf("%[^\n]", str);1718 // Find length using pointer19 while (*sptr != '\0') {20 len++;21 sptr++;22 }2324 sptr--; // Point to the last character before null2526 // Copy in reverse27 while (len > 0) {28 *rptr = *sptr;29 rptr++;30 sptr--;31 len--;32 }33 *rptr = '\0'; // Null-terminate the reversed string3435 printf("Reversed String: %s\n", rev);3637 return 0;38}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_122_copy_the_string_in_reverse_order_pointer.c -o prog (add -lm if using <math.h>).
---
Question 123: To check palindrome or not
Problem Statement: Write a C program to to check palindrome or not.
1/*2 * Program #123: to check Palindrome or not3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100];12 int len = 0, isPalindrome = 1;1314 printf("Enter a string: ");15 scanf("%s", str);1617 while (str[len] != '\0') {18 len++;19 }2021 for (int i = 0; i < len / 2; i++) {22 if (str[i] != str[len - 1 - i]) {23 isPalindrome = 0;24 break;25 }26 }2728 if (isPalindrome) {29 printf("The string is a Palindrome.\n");30 } else {31 printf("The string is not a Palindrome.\n");32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_123_to_check_palindrome_or_not.c -o prog (add -lm if using <math.h>).
---
Question 124: Input of a string using s c gets ch and print them separately
Problem Statement: Write a C program to input of a string using s c gets ch and print them separately.
1/*2 * Program #124: Input of a string using %s,%c,gets(ch) and print them separately3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;12 char str1[50], str2[50];1314 printf("Enter a single character(using %%c): ");15 scanf("%c", &c);1617 printf("Enter a word(using %%s): ");18 scanf("%s", str1);1920 // Clear input buffer before using gets21 while(getchar() != '\n');2223 printf("Enter a full sentence(using gets): ");24 gets(str2); // Note: gets() is deprecated in modern C but often used in syllabus2526 printf("\n--- Outputs ---\n");27 printf("Character: %c\n", c);28 printf("Word: %s\n", str1);29 printf("Sentence: %s\n", str2);3031 return 0;32}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_124_input_of_a_string_using_s_c_gets_ch_and_print_them_separately.c -o prog (add -lm if using <math.h>).
---
Question 125: No of words and characters in a string
Problem Statement: Write a C program to no of words and characters in a string.
1/*2 * Program #125: no of words and characters in a string3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[200];12 int chars = 0, words = 1;1314 printf("Enter a string: ");15 scanf("%[^\n]", str);1617 for (int i = 0; str[i] != '\0'; i++) {18 // Count non-space characters19 if (str[i] != ' ') {20 chars++;21 }22 // Count words based on transitions from space to character23 if (str[i] == ' ' && str[i + 1] != ' ' && str[i + 1] != '\0') {24 words++;25 }26 }2728 if (chars == 0) words = 0; // Handle empty or space-only input2930 printf("Total characters(excluding spaces) = %d\n", chars);31 printf("Total words = %d\n", words);3233 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_125_no_of_words_and_characters_in_a_string.c -o prog (add -lm if using <math.h>).
---
Question 126: No of vowels consonants digits and whitespace
Problem Statement: Write a C program to no of vowels consonants digits and whitespace.
1/*2 * Program #126: no of vowels consonants digits and whitespace3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[200];12 int v = 0, c = 0, d = 0, s = 0;1314 printf("Enter a string: ");15 scanf("%[^\n]", str);1617 for (int i = 0; str[i] != '\0'; i++) {18 char ch = str[i];1920 if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||21 ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {22 v++;23 }24 else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {25 c++;26 }27 else if (ch >= '0' && ch <= '9') {28 d++;29 }30 else if (ch == ' ' || ch == '\t') {31 s++;32 }33 }3435 printf("Vowels: %d\nConsonants: %d\nDigits: %d\nWhite spaces: %d\n", v, c, d, s);3637 return 0;38}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_126_no_of_vowels_consonants_digits_and_whitespace.c -o prog (add -lm if using <math.h>).
---
Question 127: Ascii value of a character
Problem Statement: Write a C program to ascii value of a character.
1/*2 * Program #127: ASCII value of a character3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char c;1213 printf("Enter a character: ");14 scanf("%c", &c);1516 printf("ASCII value of '%c' is %d\n", c, c);1718 return 0;19}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_127_ascii_value_of_a_character.c -o prog (add -lm if using <math.h>).
---
Question 128: Concatenate two strings without strcat
Problem Statement: Write a C program to concatenate two strings without strcat.
1/*2 * Program #128: concatenate two strings without strcat()3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str1[200], str2[100];12 int i = 0, j = 0;1314 printf("Enter first string: ");15 scanf("%s", str1);1617 printf("Enter second string: ");18 scanf("%s", str2);1920 // Find the end of the first string21 while (str1[i] != '\0') {22 i++;23 }2425 // Append the second string26 while (str2[j] != '\0') {27 str1[i] = str2[j];28 i++;29 j++;30 }31 str1[i] = '\0'; // Null terminate the final string3233 printf("Concatenated String: %s\n", str1);3435 return 0;36}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_128_concatenate_two_strings_without_strcat.c -o prog (add -lm if using <math.h>).
---
Question 129: Reverse without strrev
Problem Statement: Write a C program to reverse without strrev.
1/*2 * Program #129: reverse(without strrev())3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100], temp;12 int len = 0;1314 printf("Enter a string: ");15 scanf("%[^\n]", str);1617 // Calculate length18 while (str[len] != '\0') {19 len++;20 }2122 // Swap characters from both ends23 for (int i = 0; i < len / 2; i++) {24 temp = str[i];25 str[i] = str[len - i - 1];26 str[len - i - 1] = temp;27 }2829 printf("Reversed String: %s\n", str);3031 return 0;32}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_129_reverse_without_strrev.c -o prog (add -lm if using <math.h>).
---
Question 130: To copy a string
Problem Statement: Write a C program to to copy a string.
1/*2 * Program #130: to copy a string3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char source[100], target[100];12 int i = 0;1314 printf("Enter a string: ");15 scanf("%[^\n]", source);1617 // Copying character by character18 while (source[i] != '\0') {19 target[i] = source[i];20 i++;21 }22 target[i] = '\0'; // Append null character2324 printf("Original String: %s\n", source);25 printf("Copied String: %s\n", target);2627 return 0;28}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_130_to_copy_a_string.c -o prog (add -lm if using <math.h>).
---
Question 131: Remove characters in a string without alphabets
Problem Statement: Write a C program to remove characters in a string without alphabets.
1/*2 * Program #131: remove characters in a string without alphabets3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[150];12 int j = 0;1314 printf("Enter a string with special characters/digits: ");15 scanf("%[^\n]", str);1617 for (int i = 0; str[i] != '\0'; i++) {18 // Keep only if it is an alphabet19 if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {20 str[j] = str[i];21 j++;22 }23 }24 str[j] = '\0'; // Terminate the updated string2526 printf("String after removing non-alphabets: %s\n", str);2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_131_remove_characters_in_a_string_without_alphabets.c -o prog (add -lm if using <math.h>).
---
Question 132: Sort string in dictionary order
Problem Statement: Write a C program to sort string in dictionary order.
1/*2 * Program #132: sort string in dictionary order3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><string.h>1011int main() {12 char str[100], temp;13 int len;1415 printf("Enter a string: ");16 scanf("%s", str);1718 len = strlen(str);1920 // Bubble sort characters21 for (int i = 0; i < len - 1; i++) {22 for (int j = i + 1; j < len; j++) {23 if (str[i] > str[j]) {24 temp = str[i];25 str[i] = str[j];26 str[j] = temp;27 }28 }29 }3031 printf("String in dictionary order: %s\n", str);3233 return 0;34}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_132_sort_string_in_dictionary_order.c -o prog (add -lm if using <math.h>).
---
Question 133: Compare two string strcmp
Problem Statement: Write a C program to compare two string strcmp.
1/*2 * Program #133: compare two string(strcmp())3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><string.h>1011int main() {12 char str1[100], str2[100];1314 printf("Enter first string: ");15 scanf("%s", str1);1617 printf("Enter second string: ");18 scanf("%s", str2);1920 int result = strcmp(str1, str2);2122 if (result == 0) {23 printf("Strings are identical.\n");24 } else if (result > 0) {25 printf("First string is greater than second.\n");26 } else {27 printf("First string is smaller than second.\n");28 }2930 return 0;31}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_133_compare_two_string_strcmp.c -o prog (add -lm if using <math.h>).
---
Question 134: Compare two string without strcmp
Problem Statement: Write a C program to compare two string without strcmp.
1/*2 * Program #134: compare two string(without strcmp())3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str1[100], str2[100];12 int i = 0, flag = 0;1314 printf("Enter first string: ");15 scanf("%s", str1);1617 printf("Enter second string: ");18 scanf("%s", str2);1920 while (str1[i] != '\0' || str2[i] != '\0') {21 if (str1[i] != str2[i]) {22 flag = 1;23 break;24 }25 i++;26 }2728 if (flag == 0) {29 printf("Strings are identical.\n");30 } else {31 printf("Strings are not identical.\n");32 }3334 return 0;35}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_134_compare_two_string_without_strcmp.c -o prog (add -lm if using <math.h>).
---
Question 135: Substring inside a given string
Problem Statement: Write a C program to substring inside a given string.
1/*2 * Program #135: substring inside a given string3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100], sub[50];12 int flag = 0, j;1314 printf("Enter the main string: ");15 scanf("%s", str);1617 printf("Enter the substring: ");18 scanf("%s", sub);1920 for (int i = 0; str[i] != '\0'; i++) {21 j = 0;22 // Check window match23 while (sub[j] != '\0' && str[i + j] == sub[j]) {24 j++;25 }26 // If end of substring is reached27 if (sub[j] == '\0') {28 flag = 1;29 printf("Substring found starting at index %d\n", i);30 break;31 }32 }3334 if (flag == 0) {35 printf("Substring not found.\n");36 }3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_135_substring_inside_a_given_string.c -o prog (add -lm if using <math.h>).
---
Question 136: Convert string to opposite case
Problem Statement: Write a C program to convert string to opposite case.
1/*2 * Program #136: convert string to opposite case3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 char str[100];1213 printf("Enter a string: ");14 scanf("%[^\n]", str);1516 for (int i = 0; str[i] != '\0'; i++) {17 // Convert lower to upper18 if (str[i] >= 'a' && str[i] <= 'z') {19 str[i] = str[i] - 32;20 }21 // Convert upper to lower22 else if (str[i] >= 'A' && str[i] <= 'Z') {23 str[i] = str[i] + 32;24 }25 }2627 printf("Toggled Case String: %s\n", str);2829 return 0;30}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_136_convert_string_to_opposite_case.c -o prog (add -lm if using <math.h>).
---
Question 137: Passing string to function
Problem Statement: Write a C program to passing string to function.
1/*2 * Program #137: passing string to function3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: String6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void displayString(char *str) {11 printf("String passed to function: %s\n", str);12}1314int main() {15 char myStr[100];1617 printf("Enter a string: ");18 scanf("%[^\n]", myStr);1920 // Passing array name which acts as base address pointer21 displayString(myStr);2223 return 0;24}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_137_passing_string_to_function.c -o prog (add -lm if using <math.h>).
---
Question 138: Bubble sort
Problem Statement: Write a C program to bubble sort.
1/*2 * Program #138: Bubble sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int arr[50], n, temp;1213 printf("Enter number of elements: ");14 scanf("%d", &n);1516 printf("Enter %d integers:\n", n);17 for (int i = 0; i < n; i++) {18 scanf("%d", &arr[i]);19 }2021 // Bubble Sort Logic22 for (int i = 0; i < n - 1; i++) {23 for (int j = 0; j < n - i - 1; j++) {24 if (arr[j] > arr[j + 1]) {25 temp = arr[j];26 arr[j] = arr[j + 1];27 arr[j + 1] = temp;28 }29 }30 }3132 printf("Sorted array in ascending order: ");33 for (int i = 0; i < n; i++) {34 printf("%d ", arr[i]);35 }36 printf("\n");3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_138_bubble_sort.c -o prog (add -lm if using <math.h>).
---
Question 139: Insertion sort
Problem Statement: Write a C program to insertion sort.
1/*2 * Program #139: insertion sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int arr[50], n, key, j;1213 printf("Enter number of elements: ");14 scanf("%d", &n);1516 printf("Enter %d integers:\n", n);17 for (int i = 0; i < n; i++) {18 scanf("%d", &arr[i]);19 }2021 // Insertion Sort Logic22 for (int i = 1; i < n; i++) {23 key = arr[i];24 j = i - 1;2526 while (j >= 0 && arr[j] > key) {27 arr[j + 1] = arr[j];28 j = j - 1;29 }30 arr[j + 1] = key;31 }3233 printf("Sorted array in ascending order: ");34 for (int i = 0; i < n; i++) {35 printf("%d ", arr[i]);36 }37 printf("\n");3839 return 0;40}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_139_insertion_sort.c -o prog (add -lm if using <math.h>).
---
Question 140: Selection sort
Problem Statement: Write a C program to selection sort.
1/*2 * Program #140: selection sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int main() {11 int arr[50], n, min_idx, temp;1213 printf("Enter number of elements: ");14 scanf("%d", &n);1516 printf("Enter %d integers:\n", n);17 for (int i = 0; i < n; i++) {18 scanf("%d", &arr[i]);19 }2021 // Selection Sort Logic22 for (int i = 0; i < n - 1; i++) {23 min_idx = i;24 for (int j = i + 1; j < n; j++) {25 if (arr[j] < arr[min_idx]) {26 min_idx = j;27 }28 }29 // Swap30 if (min_idx != i) {31 temp = arr[i];32 arr[i] = arr[min_idx];33 arr[min_idx] = temp;34 }35 }3637 printf("Sorted array in ascending order: ");38 for (int i = 0; i < n; i++) {39 printf("%d ", arr[i]);40 }41 printf("\n");4243 return 0;44}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_140_selection_sort.c -o prog (add -lm if using <math.h>).
---
Question 141: Quick sort
Problem Statement: Write a C program to quick sort.
1/*2 * Program #141: quick sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void swap(int *a, int *b) {11 int t = *a;12 *a = *b;13 *b = t;14}1516int partition(int arr[], int low, int high) {17 int pivot = arr[high];18 int i = (low - 1);1920 for (int j = low; j <= high - 1; j++) {21 if (arr[j] < pivot) {22 i++;23 swap(&arr[i], &arr[j]);24 }25 }26 swap(&arr[i + 1], &arr[high]);27 return (i + 1);28}2930void quickSort(int arr[], int low, int high) {31 if (low < high) {32 int pi = partition(arr, low, high);3334 quickSort(arr, low, pi - 1);35 quickSort(arr, pi + 1, high);36 }37}3839int main() {40 int arr[50], n;4142 printf("Enter number of elements: ");43 scanf("%d", &n);4445 printf("Enter %d integers:\n", n);46 for (int i = 0; i < n; i++) {47 scanf("%d", &arr[i]);48 }4950 quickSort(arr, 0, n - 1);5152 printf("Sorted array: ");53 for (int i = 0; i < n; i++) {54 printf("%d ", arr[i]);55 }56 printf("\n");5758 return 0;59}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_141_quick_sort.c -o prog (add -lm if using <math.h>).
---
Question 142: Merge sort
Problem Statement: Write a C program to merge sort.
1/*2 * Program #142: merge sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void merge(int arr[], int l, int m, int r) {11 int i, j, k;12 int n1 = m - l + 1;13 int n2 = r - m;1415 int L[n1], R[n2];1617 for (i = 0; i < n1; i++) L[i] = arr[l + i];18 for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];1920 i = 0; j = 0; k = l;21 while (i < n1 && j < n2) {22 if (L[i] <= R[j]) {23 arr[k++] = L[i++];24 } else {25 arr[k++] = R[j++];26 }27 }2829 while (i < n1) arr[k++] = L[i++];30 while (j < n2) arr[k++] = R[j++];31}3233void mergeSort(int arr[], int l, int r) {34 if (l < r) {35 int m = l + (r - l) / 2;36 mergeSort(arr, l, m);37 mergeSort(arr, m + 1, r);38 merge(arr, l, m, r);39 }40}4142int main() {43 int arr[50], n;4445 printf("Enter number of elements: ");46 scanf("%d", &n);4748 printf("Enter %d integers:\n", n);49 for (int i = 0; i < n; i++) {50 scanf("%d", &arr[i]);51 }5253 mergeSort(arr, 0, n - 1);5455 printf("Sorted array: ");56 for (int i = 0; i < n; i++) {57 printf("%d ", arr[i]);58 }59 printf("\n");6061 return 0;62}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_142_merge_sort.c -o prog (add -lm if using <math.h>).
---
Question 143: Shell sort
Problem Statement: Write a C program to shell sort.
1/*2 * Program #143: shell sort3 * Unit 4: Strings, 2D Arrays & Sorting Algorithms4 *5 * Explanation: Array6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910void shellSort(int arr[], int n) {11 for (int gap = n / 2; gap > 0; gap /= 2) {12 for (int i = gap; i < n; i += 1) {13 int temp = arr[i];14 int j;15 for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {16 arr[j] = arr[j - gap];17 }18 arr[j] = temp;19 }20 }21}2223int main() {24 int arr[50], n;2526 printf("Enter number of elements: ");27 scanf("%d", &n);2829 printf("Enter %d integers:\n", n);30 for (int i = 0; i < n; i++) {31 scanf("%d", &arr[i]);32 }3334 shellSort(arr, n);3536 printf("Sorted array: ");37 for (int i = 0; i < n; i++) {38 printf("%d ", arr[i]);39 }40 printf("\n");4142 return 0;43}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_143_shell_sort.c -o prog (add -lm if using <math.h>).
---
Unit 5: Recursion Mechanics & Call Stack Traces (Q144 – Q150)
Question 144: Factorial of a number
Problem Statement: Write a C program to factorial of a number.
1/*2 * Program #144: factorial of a number3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910unsigned long long fact(int n) {11 if (n == 0 || n == 1)12 return 1;13 else14 return n * fact(n - 1);15}1617int main() {18 int num;1920 printf("Enter a positive integer: ");21 scanf("%d", &num);2223 if (num < 0)24 printf("Factorial of negative number doesn't exist.\n");25 else26 printf("Factorial of %d = %llu\n", num, fact(num));2728 return 0;29}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_144_factorial_of_a_number.c -o prog (add -lm if using <math.h>).
---
Question 145: Decrease number indirect recursion
Problem Statement: Write a C program to decrease number indirect recursion.
1/*2 * Program #145: decrease number(indirect recursion)3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910// Function prototypes needed for indirect recursion11void functionA(int n);12void functionB(int n);1314void functionA(int n) {15 if (n > 0) {16 printf("%d ", n);17 functionB(n - 1);18 }19}2021void functionB(int n) {22 if (n > 1) {23 printf("%d ", n);24 functionA(n / 2);25 }26}2728int main() {29 int n;3031 printf("Enter a number to start decreasing pattern: ");32 scanf("%d", &n);3334 printf("Generated Pattern: ");35 functionA(n);36 printf("\n");3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_145_decrease_number_indirect_recursion.c -o prog (add -lm if using <math.h>).
---
Question 146: Sum of n natural numbers
Problem Statement: Write a C program to sum of n natural numbers.
1/*2 * Program #146: sum of n natural numbers3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int sumOfNatural(int n) {11 if (n == 0)12 return 0;13 else14 return n + sumOfNatural(n - 1);15}1617int main() {18 int num;1920 printf("Enter a positive integer: ");21 scanf("%d", &num);2223 printf("Sum of first %d natural numbers = %d\n", num, sumOfNatural(num));2425 return 0;26}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_146_sum_of_n_natural_numbers.c -o prog (add -lm if using <math.h>).
---
Question 147: Reverse of a number
Problem Statement: Write a C program to reverse of a number.
1/*2 * Program #147: reverse of a number3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int reverseNumber(int n) {11 static int rev = 0;1213 if (n == 0)14 return rev;1516 rev = rev * 10 + (n % 10);17 return reverseNumber(n / 10);18}1920int main() {21 int num, reversed;2223 printf("Enter an integer: ");24 scanf("%d", &num);2526 // Store sign if negative and make positive for reversal27 int isNegative = num < 0 ? 1 : 0;28 if (isNegative) num = -num;2930 reversed = reverseNumber(num);3132 if (isNegative) reversed = -reversed;3334 printf("Reversed Number = %d\n", reversed);3536 return 0;37}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_147_reverse_of_a_number.c -o prog (add -lm if using <math.h>).
---
Question 148: Gcd of two numbers
Problem Statement: Write a C program to gcd of two numbers.
1/*2 * Program #148: GCD of two numbers3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int gcd(int a, int b) {11 if (b == 0)12 return a;13 return gcd(b, a % b);14}1516int main() {17 int num1, num2;1819 printf("Enter two numbers: ");20 scanf("%d %d", &num1, &num2);2122 printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));2324 return 0;25}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_148_gcd_of_two_numbers.c -o prog (add -lm if using <math.h>).
---
Question 149: Reverse of a string using strrev
Problem Statement: Write a C program to reverse of a string using strrev.
1/*2 * Program #149: reverse of a string using strrev3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>9"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><string.h>1011void reverseString(char str[], int start, int end) {12 char temp;13 if (start >= end) {14 return; // Base case: middle of the string is reached15 }1617 // Swap characters18 temp = str[start];19 str[start] = str[end];20 str[end] = temp;2122 // Recursive call moving towards the middle23 reverseString(str, start + 1, end - 1);24}2526int main() {27 char str[100];2829 printf("Enter a string: ");30 scanf("%[^\n]", str);3132 int len = strlen(str);3334 reverseString(str, 0, len - 1);3536 printf("Reversed string: %s\n", str);3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_149_reverse_of_a_string_using_strrev.c -o prog (add -lm if using <math.h>).
---
Question 150: Fibonacci series
Problem Statement: Write a C program to fibonacci series.
1/*2 * Program #150: Fibonacci Series3 * Unit 5: Recursion4 *5 * Explanation: Recursion6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910int fibonacci(int n) {11 if (n == 0)12 return 0;13 else if (n == 1)14 return 1;15 else16 return (fibonacci(n - 1) + fibonacci(n - 2));17}1819int main() {20 int n;2122 printf("Enter the number of terms: ");23 scanf("%d", &n);2425 printf("Fibonacci Series: ");26 for (int i = 0; i < n; i++) {27 printf("%d ", fibonacci(i));28 }29 printf("\n");3031 return 0;32}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_150_fibonacci_series.c -o prog (add -lm if using <math.h>).
---
Unit 6: Structures, Memory Alignment & Marksheet System (Q151 – Q152)
Question 151: Student database roll name address
Problem Statement: Write a C program to student database roll name address.
1/*2 * Program #151: student database(roll,name, address)3 * Unit 6: Structures4 *5 * Explanation: Structure6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910struct Student {11 int roll;12 char name[50];13 char address[100];14};1516int main() {17 struct Student s;1819 printf("Enter roll number: ");20 scanf("%d", &s.roll);2122 // Clear input buffer23 while(getchar() != '\n');2425 printf("Enter name: ");26 scanf("%[^\n]", s.name);2728 while(getchar() != '\n');2930 printf("Enter address: ");31 scanf("%[^\n]", s.address);3233 printf("\n--- Student Details ---\n");34 printf("Roll Number: %d\n", s.roll);35 printf("Name: %s\n", s.name);36 printf("Address: %s\n", s.address);3738 return 0;39}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_151_student_database_roll_name_address.c -o prog (add -lm if using <math.h>).
---
Question 152: Scan roll no and marks of each sub per student print avg of marks and the grade of each student print the grade in totality and also according to each subject
Problem Statement: Write a C program to scan roll no and marks of each sub per student print avg of marks and the grade of each student print the grade in totality and also according to each subject.
1/*2 * Program #152: Scan roll no, and marks of each sub per student. Print avg of marks and the grade of each student. Print the grade in totality and also according to each subject3 * Unit 6: Structures4 *5 * Explanation: Structure6 */78"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>910struct Student {11 int roll;12 float marks[5];13 float avg;14 char totalGrade;15 char subGrades[5];16};1718// Function to calculate grade as per standard MAKAUT slab19char calculateGrade(float marks) {20 if (marks >= 90) return 'O';21 else if (marks >= 80) return 'E';22 else if (marks >= 70) return 'A';23 else if (marks >= 60) return 'B';24 else if (marks >= 50) return 'C';25 else if (marks >= 40) return 'D';26 else return 'F';27}2829int main() {30 int n;3132 printf("Enter number of students: ");33 scanf("%d", &n);3435 struct Student s[n];3637 for (int i = 0; i < n; i++) {38 printf("\nEnter details for student %d\n", i + 1);39 printf("Enter Roll Number: ");40 scanf("%d", &s[i].roll);4142 float sum = 0;43 printf("Enter marks for 5 subjects(out of 100):\n");44 for (int j = 0; j < 5; j++) {45 printf("Subject %d: ", j + 1);46 scanf("%f", &s[i].marks[j]);47 s[i].subGrades[j] = calculateGrade(s[i].marks[j]);48 sum += s[i].marks[j];49 }5051 s[i].avg = sum / 5.0;52 s[i].totalGrade = calculateGrade(s[i].avg);53 }5455 printf("\n============= RESULT SHEET =============\n");56 for (int i = 0; i < n; i++) {57 printf("Roll No: %d | Avg Marks: %.2f | Overall Grade: %c\n",58 s[i].roll, s[i].avg, s[i].totalGrade);59 printf("Subject Grades: ");60 for (int j = 0; j < 5; j++) {61 printf("Sub%d:%c ", j + 1, s[i].subGrades[j]);62 }63 printf("\n----------------------------------------\n");64 }6566 return 0;67}
Key Takeaway: Always handle edge cases like invalid input or zero values. Compile with gcc prog_152_scan_roll_no_and_marks_of_each_sub_per_student_print_avg_of_marks_and_the_grade_of_each_student_print_the_grade_in_totality_and_also_according_to_each_subject.c -o prog (add -lm if using <math.h>).
---
Frequently Asked Questions (FAQs)
Q1. How should I prepare C programming for university semester exams?
Start with Unit 1 basic syntax and operator precedence rules, practice all loop series and pattern programs in Unit 2, master call-by-reference and pointer arithmetic in Unit 3, implement all 6 sorting algorithms in Unit 4, trace stack activation frames in Unit 5, and write struct-based database programs in Unit 6.
Q2. Which sorting algorithms are most frequently asked in exams?
Quick Sort and Merge Sort (Divide & Conquer) carry 10–15 marks in long questions, while Bubble Sort and Insertion Sort are common 5-mark items.
Q3. Why is % (modulus) operator restricted to integer operands?
The % operator computes the integer remainder. Floating point division yields continuous quotient fractions without integer remainders. For float remainders, use fmod() from <math.h>.
Q4. What is the difference between call by value and call by reference in C?
Call by value passes a copy of the argument variable to the function, so modifications do not affect the caller. Call by reference passes memory addresses (pointers), allowing the function to directly modify the caller variables in memory.
Q5. How does structure padding affect memory size in C?
Compilers insert unused padding bytes between struct fields to align members with CPU word boundaries (4-byte or 8-byte boundaries). Use #pragma pack(1) to force packed memory allocation without padding.