C Programming
- Write a program to find largest of n numbers taken from user. Answer: Store the n numbers in an array, take max = a[0] and replace it whenever a larger element is found. Logic: after one pass through th... 2
- Write recursive function to display factorial of an inputted number. Answer: factorial(n) returns 1 when n ≤ 1, otherwise n × factorial(n − 1). Logic: 5! = 5 × 4! = 5 × 4 × 3! = dots = 120. The base case n ≤ 1... 2
- Write a function to display n Fibonacci elements. Answer: A function fibonacci(n) starts with a = 0, b = 1 and repeatedly prints a and moves forward (c = a + b; a = b; b = c). Logic: each Fi... 2
- Write a program to demonstrate the use of break and continue branching structures. Answer: break exits the loop immediately; continue skips the remaining statements of the current iteration and goes to the next one. break:... 2
- Write a program to find LCM of two integer numbers. Answer: Find GCD by Euclid's method, then LCM = (a × b)/GCD. Logic (12, 18): 18 % 12 = 6, 12 % 6 = 0, so GCD = 6 and LCM = 12 × 18 / 6 = 36.... 2
- Write menu driven program to perform arithmetic calculations Answer: Show a menu, read the choice and use a switch statement for +, −, ×, ÷ inside a do-while loop until the user chooses Exit. Logic: ea... 2
- Write a program to check whether an inputted number is Armstrong or not. Answer: An n-digit number is Armstrong if the sum of each digit raised to the power n equals the number (e.g. 153 = 1³ + 5³ + 3³ ). Logic: f... 2
- Write a program to reverse digits of an imputed integer number. Answer: Repeatedly take the last digit (n % 10), append it to rev (rev = rev × 10 + digit) and drop it from n (n = n / 10). Logic (n = 123):... 2
- Write a program to check whether an inputted number is prime or not. Answer: A number n ≥ 2 is prime if no number from 2 to √(n) divides it. Logic: numbers less than 2 are not prime. Otherwise test divisors i... 2
- Write a program to compute sum of series. s = 1+ (1 / 2) + ( 1 / 3 )+ ( 1 / 4 )+ ( 1 / 5 )+………+ ( 1 / n ) Answer: Add 1.0/i for i = 1 to n; for the modified series keep a running factorial (fact = fact × i) and add 1.0/fact. Logic: Series 1: s =... 2
- Write a program to display patterns by accepting row number as an input. Pattern 1 1 1 2 1 2 3 Answer: Pattern 1 prints the column number j in row i; Pattern 2 (the modification) prints the row number i instead of j. Logic: the outer l... 2
- Write a program to find permutation and combination values. Answer: Use a factorial function: ⁿP_r = n!/((n-r)!) and ⁿC_r = n!/(r! (n-r)!). Logic: a function fact() returns n! using a loop. The progra... 2
- Write a program to check type of triangle and to evaluate its area. Answer: Check the triangle inequality, classify by comparing sides (equilateral/isosceles/scalene) and find area with Heron's formula. Logic... 2
- Write a program to compare two integer numbers using if-else statement Answer: Read two integers and compare them with an if – else if – else ladder. Logic: if a > b, a is greater; else if a < b, b is greater; o... 2
- Write a program to swap two inputted integer numbers using third variable without using third variable Answer: Swap with a temporary variable (t = a; a = b; b = t;) or without one (a = a + b; b = a - b; a = a - b;). Logic: Using a third variab... 2