Qno.1.
Write a C program to find the sum
and average of three numbers
#include <stdio.h>
int main()
{
float num1, num2, num3, sum, average;
printf("Enter three numbers: ");
scanf("%f %f %f", &um1, &um2,
&numa3);
sum = num71 + num2 + num3;
average = sum/ 3;
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", average);
return O;
Enter three numbers: 1,2,3
Sum =1.00
Average = 0.33
=== Code Execution Successful ===
Qno.2. Write a C program to find the sum
of individual digits of a given positive
integer.
#include <stdio.h>
int main()
{
int number, digit, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &humber);
while (number > 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
printf("Sum of the digits = %d\n", sum);
return O;
Enter a positive integer: 244
Sum of the digits = 10
=== Code Execution Successful ===
Qno.3. Write a C program to generate the
first n terms of the Fibonacci sequence.
#include <stdio.h>
int main()
{
int n, i, first =0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i=0;i<n;i++)
if (i<=1)
next =i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return O;
}
Enter the number of terms: 0,1,2
Fibonacci Series:
=== Code Execution Successful ===
Qno.4. Write a C program to generate
prime numbers between 1 to n.
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num)
{
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 ==0|| num % 3 == 0) return
false;
for(inti=5;i*i<=num;i+=6)
if (num % i==0|num
% (i +2) ==0)
return false;
}
return true;
}
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers between 1 and
%d are: ", n);
for (inti=2;i<=n;i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
return O;
}
Enter the value of n; 10
Prime numbers between 1 and 10 are; 2 3
57
===Code Execution Successful ===
Qno.5. Write a C program to check whether
a given number is an Armstrong number or
not.
#include <stdio.h>
#include <math.h>
int main()
{
int number, originalNumber, remainder,
result =0,n =0;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0) {
originalNumber/= 10;
} n++; ,
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber/= 10;
}
if (result == number)
{
printf("%d is an Armstrong number.\n’,
number);
} else {
printf("%d is not an Armstrong number.
\n", number);
}
return O;
}
Enter an integer: 10,10
10 is not an Armstrong number.
=== Code Execution Successful ===
Qno.6. Write a C program to evaluate the
algebraic expression (ax +b)/(ax - h)
#include <stdio.h>
int main()
{
float a, x, b, result;
printf("Enter the value of a: ");
scanf("%f", &a);
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of b: ");
scanf("%f", &b);
if (@*x-b==0){
printf("Error: Division by zero.\n");
}
else
{
result =(a*x+b)/(a*x-b);
printf("The result of the expression is:
%f\n", result);
}
return O;
}
Enter the value of a: 1
Enter the value of x: 2
Enter the value of b: 3
The result of the expression is: -5.000000
=== Code Execution Successful ===
Qno.7. Write a C program to check whether
a given number is a perfect number or not.
#include <stdio.h>
#include <stdbool.h>
bool isPerfectNumber(int num)
{
if (num<=1)
{
return false;
}
int sum=0;
for (inti=1;i<=num/2;i++) {
if (num% i ==0) {
sum +=i;
}
}
return sum == num;
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &humber);
if (isPerfectNumber(number)) {
printf("%d is a perfect number.\n’,
number);
}
else
{
printf("%d is not a perfect number.\n",
number);
}
return O;
}
Enter a number: 1,2,3
1 is not a perfect number.
=== Code Execution Successful ===
Qno.8. Write a C program to check
whether a given number is a strong
number or not.
#include <stdio.h>
int factorial(int n)
{
if(n==0]n==1)
return 1;
else
return n * factorial(n - 1);
}
int isStrong(int num)
{
int sum = 0;
int temp = num;
while (temp > 0)
{
int digit = temp % 10;
sum += factorial(digit);
temp /=10;
}
return (sum == num);
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &humber);
if (isStrong(number))
printf("%d is a strong number.\n’,
number);
else
printf("%d is not a strong number.\n"
number);
return O;
}
Enter a number: 24
24 is not a strong number.
=== Code Execution Successful ===
Qno.9. Write a C program to find the roots
of a quadratic equation.
Also, write a C program to perform
arithmetic operations using the switch
statement.
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, ¢, discriminant, root1, root2,
realPart, imaginaryPart;
printf("Enter coefficients a, b, and c¢: ");
scanf("%lIf %If %If", &a, &b, &c);
discriminant=b*b-4*a*c;
if (discriminant > 0)
root1 = (-b + sqrt(discriminant)) / (2 *
a);
root2 = (-b - sqrt(discriminant)) / (2 *
a);
printf("Roots are real and distinct:
%.2If and %.2If\n", root1, root2);
}
else if (discriminant == 0)
{
root1 =root2 =-b /(2 * a);
printf("Roots are real and equal: %.2If
and %.2If\n", root1, root2);
}
else
{
realPart=-b /(2 * a);
imaginaryPart = sqrt(-discriminant) /
(2 *a);
printf("Roots are complex: %.2If +
%.2lfi and %.2If - %.2Ifi\n", realPart,
imaginaryPart, realPart, imaginaryPart);
}
return O;
}
Enter coefficients a, b, and c: 2,5,8
Roots are real and equal: -0.00 and -0.00
=== Code Execution Successful ===
Qno.10. Write a C program to find the
factorial of a given integer using a non-
recursive function.
#include <stdio.h>
unsigned long long factorial(int n)
{
unsigned long long result = 1;
if (n<0){
return O;
}
for (inti=1;i<=n;i++) {
result *=i;
}
return result;
}
int main()
{
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &hum);
unsigned long long fact = factorial(num);
if (fact == 0)
{
printf("Factorial is not defined for
negative integers.\n");
}
else
{
printf("Factorial of %d = %llu\n", num,
fact);
}
return O;
}
Enter a non-negative integer: 25
Factorial of 25 = 7034535277573963776
=== Code Execution Successful ===
Qno.11. Write a C program to find the
factorial of a given integer using a
recursive function.
#include <stdio.h>
long factorial(int n)
{
if (n==0)
{
return 1; // Base case: factorial of 0 is 1
} else {
return n * factorial(n - 1);
}
}
int main()
{
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial is not defined for
negative numbers.\n");
}
else
{
long result = factorial(num);
printf("Factorial of %d = %Id\n", num,
result);
}
return O;
}
Enter a non-negative integer: 21
Factorial of 21 =-4249290049419214848
=== Code Execution Successful ===
Qno.12. Write a C program to perform the
addition of two matrices.
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and
columns for the matrices: ");
scanf("%d %d", &ows, &cols);
int matrix1[rows][cols];
int matrix2[rows][cols];
int resultMatrix[rows][cols];
printf("\nEnter elements of the first
matrix:\n");
for (inti=0;i< rows; i++)
{
for (intj = 0;j < cols; j++)
scanf("%d", &matrix1[i][j]);
}
}
printf("\nEnter elements of the second
matrix:\n");
for (inti=0;i<rows; i++) {
for (intj = 0; ] < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (inti=0; i< rows;i++)
{
for (intj=0;j < cols; j++)
{
resultMatrix[il[j] = matrix1[i][j] +
matrix2[i][jl;
}
}
printf("\nResultant matrix (A + B):\n");
for (inti=0; i< rows; i++)
{
for (int = 0; j < cols; j++)
{
printf(*%d ", resultMatrix[il[jl;
}
printf("\n");
}
return Q;
}
Enter the number of rows and columns for
the matrices; 2x2
Enter elements of the first matrix;
Enter elements of the second matrix;
Resultant matrix (A + B):
=== Code Execution Successful ===
Qno.13. Write a C program that uses
functions to perform multiplication of two
matrices.
#include <stdio.h>
void readMatrix(int matrix[][10], int rows,
int cols)
{
printf("Enter the elements of the matrix:
\nn);
for (inti=0;i<rows; i++)
{
for (intj=0;j < cols; j++)
{
scanf("%d", &matrix[il[j]);
}
}
}
void displayMatrix(int matrix[][10], int rows,
int cols)
{
for (inti=0;i<rows; i++)
{
for (lntJ = O'J < COlS; j++)
{
printf("%d ", matrixil[j]);
}
printf("\n");
}
}
void multiplyMatrices(int matrix1[][10], int
matrix2[][10], int result[][10], int rows1, int
cols1, int rows2, int cols2)
{
for (inti=0;i<rowst;i++)
{
for (intj=0; j < cols2; j++)
{
result[il[j] = 0; // Initialize element to
for (int k = 0; k < cols1; k++)
{
result[i][j] += matrix1[i][k] *
matrix2[K][j];
}
}
}
}
int main()
{
int matrix1[10][10], matrix2[10][10],
result[10][10];
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and
columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and
columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2)
{
printf("Matrices cannot be multiplied.
\n%);
return O;
}
printf("Enter elements of the first matrix:
\nll);
readMatrix(matrix1, rows1, cols1);
printf("Enter elements of the second
matrix:\n");
readMatrix(matrix2, rows2, cols2);
multiplyMatrices(matrix1, matrix2,
result, rows1, cols1, rows2, cols2);
printf("Resultant matrix:\n");
displayMatrix(result, rows1, cols2);
return O;
}
Resultant Matrix:
66 7278
156171 186
=== Code Execution Successful===
Qno.14. Write a C program using user-
defined functions to determine whether
the given string is a palindrome or not.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isPalindrome(char str[])
{
int left = O;
int right = strlen(str) - 1;
while (left < right)
{
if (strlleft] != str[right])
{
return false;
}
left++;
right-;
}
return true;
}
int main()
{
char str[100];
printf("Enter a string: *);
scanf("%s", str);
if (isPalindrome(str)) {
printf("\"%s\" is a palindrome.\n", str);
} else {
printf("\"%s\" is not a palindrome.\n",
str);
return O;
}
Enter a string: 65
"65" is not a palindrome.
=== Code Execution Successful ===