2. Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1.
Subsequent
terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n
terms of the sequence.
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones,
typically starting with 0 and 1. The sequence begins like this:
0,1,1,2,3,5,8,13,21,34,55,89,…
Formula:
For n≥2n:
F(n)=F(n−1)+F(n−2)
Where:
F(0)= 0
F(1)= 1
#include <stdio.h>
int main() {
int n, i;
long long first = 0, second = 1, next;
// Asking the user for the number of terms
prin ("Enter the number of terms: ");
scanf("%d", &n);
prin ("Fibonacci sequence:\n");
for (i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
prin ("%lld ", next);
}
prin ("\n");
return 0;
}
3. Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.
#include <stdio.h>
int main() {
int n, i, j, isPrime;
// Ask user for the upper limit n
prin ("Enter the value of n: ");
scanf("%d", &n);
prin ("Prime numbers between 1 and %d are:\n", n);
// Loop through each number from 2 to n
for (i = 2; i <= n; i++) {
isPrime = 1; // Assume the current number is prime
// Check if the current number is divisible by any number from 2 to i-1
for (j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = 0; // It's not prime if divisible
break;
}
}
// If isPrime is s ll 1, then the number is prime
if (isPrime) {
prin ("%d ", i);
}
}
prin ("\n");
return 0;
}
4. Write a C program to find the roots of a quadra c equa on.
#include <stdio.h>
#include <math.h> // Required for sqrt() func on
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
// Ask the user to enter coefficients a, b, and c
prin ("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of the discriminant to find the roots
if (discriminant > 0) {
// Two real and dis nct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
prin ("Roots are real and different.\n");
prin ("Root 1 = %.2f\n", root1);
prin ("Root 2 = %.2f\n", root2);
}
else if (discriminant == 0) {
// Two real and equal roots
root1 = root2 = -b / (2 * a);
prin ("Roots are real and the same.\n");
prin ("Root 1 = Root 2 = %.2f\n", root1);
}
else {
// Complex roots
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
prin ("Roots are complex and different.\n");
prin ("Root 1 = %.2f + %.2fi\n", realPart, imagPart);
prin ("Root 2 = %.2f - %.2fi\n", realPart, imagPart);
}
return 0;
}
For real and dis nct roots:
For real and equal roots:
For complex roots:
5. Write a C program to find the factorial of a given integer.
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1; // Factorial of large numbers can be big, so use unsigned long long
// Ask the user for an integer
prin ("Enter an integer: ");
scanf("%d", &n);
// Check if the user has entered a nega ve number
if (n < 0) {
prin ("Factorial of a nega ve number doesn't exist.\n");
} else {
// Calculate factorial
for (i = 1; i <= n; ++i) {
factorial *= i;
}
prin ("Factorial of %d = %llu\n", n, factorial);
}
return 0;
}
For a posi ve integer:
For a nega ve integer:
For 0 (since 0! = 1):
6. Write a C program to find the GCD (greatest common divisor) of two given integers.
The Euclidean algorithm is used to compute the GCD:
Replace num1 with num2, and num2 with the remainder of num1 % num2, until num2 becomes 0.
When num2 becomes 0, the value of num1 is the GCD.
#include <stdio.h>
int main() {
int num1, num2, gcd, remainder;
// Ask the user for two integers
prin ("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Applying the Euclidean algorithm
while (num2 != 0) {
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
gcd = num1; // The final value of num1 is the GCD
prin ("GCD of the given numbers is %d\n", gcd);
return 0;
}
7. Write a C program to solve Towers of Hanoi problem.
The function hanoi(n, source, destination, auxiliary) moves n disks from the source rod to the
destination rod using the auxiliary rod.
Base Case: If there's only one disk, move it directly from the source to the destination.
Recursive Step:
Move n-1 disks from the source to the auxiliary rod.
Move the nth disk from the source to the destination rod.
Finally, move the n-1 disks from the auxiliary rod to the destination.
#include <stdio.h>
// Recursive func on to solve Towers of Hanoi
void hanoi(int n, char source, char des na on, char auxiliary) {
if (n == 1) {
// Base case: move one disk directly from source to des na on
prin ("Move disk 1 from %c to %c\n", source, des na on);
return;
}
// Move n-1 disks from source to auxiliary using des na on as auxiliary
hanoi(n - 1, source, auxiliary, des na on);
// Move the nth disk from source to des na on
prin ("Move disk %d from %c to %c\n", n, source, des na on);
// Move the n-1 disks from auxiliary to des na on using source as auxiliary
hanoi(n - 1, auxiliary, des na on, source);
}
int main() {
int n; // Number of disks
// Input the number of disks
prin ("Enter the number of disks: ");
scanf("%d", &n);
// Call the hanoi func on to solve the problem
hanoi(n, 'A', 'C', 'B'); // A = source, C = des na on, B = auxiliary
return 0;
}
8. Write a C program, which takes two integer operands and one operator from the user, performs the opera on
and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)
#include<stdio.h>
int main() {
int num1, num2, result;
char operator;
// Input two integers
prin ("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Input an operator
prin ("Enter an operator (+, -, *, /, %%): ");
scanf(" %c", &operator); // The space before %c is to avoid newline issues
// Perform opera on based on the operator using switch case
switch(operator) {
case '+':
result = num1 + num2;
prin ("Result: %d + %d = %d\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
prin ("Result: %d - %d = %d\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
prin ("Result: %d * %d = %d\n", num1, num2, result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
prin ("Result: %d / %d = %d\n", num1, num2, result);
} else {
prin ("Error: Division by zero is not allowed.\n");
}
break;
case '%':
if (num2 != 0) {
result = num1 % num2;
prin ("Result: %d %% %d = %d\n", num1, num2, result);
} else {
prin ("Error: Division by zero is not allowed.\n");
}
break;
default:
prin ("Error: Invalid operator.\n");
}
return 0;
}
9. Write a C program to find both the largest and smallest number in a list of integers.
#include <stdio.h>
int main() {
int n, i, largest, smallest;
// Input the number of elements in the list
prin ("Enter the number of elements: ");
scanf("%d", &n);
// Array to hold the elements
int arr[n];
// Input the list of integers
prin ("Enter %d integers: \n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Ini alize largest and smallest with the first element
largest = smallest = arr[0];
// Loop through the array to find the largest and smallest numbers
for(i = 1; i < n; i++) {
if(arr[i] > largest) {
largest = arr[i];
}
if(arr[i] < smallest) {
smallest = arr[i];
}
}
// Output the results
prin ("Largest number: %d\n", largest);
prin ("Smallest number: %d\n", smallest);
return 0;
}
10. Write a C program that uses func ons to perform the following:
i) Addi on of Two Matrices
#include <stdio.h>
void addMatrices(int rows, int cols, int matrix1[100][100], int matrix2[100][100]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
prin ("%d ", matrix1[i][j] + matrix2[i][j]);
}
prin ("\n");
}
}
int main() {
int rows, cols, matrix1[100][100], matrix2[100][100];
prin ("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
prin ("Enter elements for the first matrix:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &matrix1[i][j]);
prin ("Enter elements for the second matrix:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &matrix2[i][j]);
prin ("Result of matrix addi on:\n");
addMatrices(rows, cols, matrix1, matrix2);
return 0;
}
ii) Mul plica on of Two Matrices
#include <stdio.h>
void mul plyMatrices(int rows1, int cols1, int matrix1[100][100], int rows2, int cols2, int matrix2[100][100]) {
int result[100][100] = {0}; // Ini alize the result matrix
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
prin ("%d ", result[i][j]);
}
prin ("\n");
}
}
int main() {
int rows1, cols1, rows2, cols2, matrix1[100][100], matrix2[100][100];
// Input size of first matrix
prin ("Enter rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
// Input size of second matrix
prin ("Enter rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
// Matrix mul plica on condi on
if (cols1 != rows2) {
prin ("Matrix mul plica on is not possible. Columns of first matrix must equal rows of second matrix.\n");
return 0;
}
// Input first matrix
prin ("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++)
for (int j = 0; j < cols1; j++)
scanf("%d", &matrix1[i][j]);
// Input second matrix
prin ("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++)
for (int j = 0; j < cols2; j++)
scanf("%d", &matrix2[i][j]);
// Perform matrix mul plica on
prin ("Result of matrix mul plica on:\n");
mul plyMatrices(rows1, cols1, matrix1, rows2, cols2, matrix2);
return 0;
}