0% found this document useful (0 votes)
6 views6 pages

C Progrms MCA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

C Progrms MCA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Reverse of a given number


c
Copy code
#include <stdio.h>

int main() {
int num, reversed = 0;
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}

printf("Reversed number: %d\n", reversed);


return 0;
}

2. Sum of individual digits of a positive integer


c
Copy code
#include <stdio.h>

int main() {
int num, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);

while (num != 0) {
sum += num % 10;
num /= 10;
}

printf("Sum of digits: %d\n", sum);


return 0;
}

3. Fibonacci Series
c
Copy code
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: ");


for (int i = 0; i < n; i++) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}

4. Generate all prime numbers between 1 and n


c
Copy code
#include <stdio.h>

int is_prime(int num) {


if (num <= 1)
return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return 0;
}
return 1;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

printf("Prime numbers between 1 and %d are: ", n);


for (int i = 2; i <= n; i++) {
if (is_prime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}

5. Roots of a quadratic equation


c
Copy code
#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, c: ");
scanf("%f %f %f", &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("Real and distinct roots: %.2f, %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Real and equal roots: %.2f\n", root1);
} else {
printf("Complex roots\n");
}

return 0;
}

6. Factorial of a given integer


c
Copy code
#include <stdio.h>

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial: %d\n", factorial(num));


return 0;
}

7. GCD of two given integers


c
Copy code
#include <stdio.h>

int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);

printf("GCD: %d\n", gcd(a, b));


return 0;
}

8. Calculator using switch statement


c
Copy code
#include <stdio.h>

int main() {
int a, b;
char op;

printf("Enter two integers: ");


scanf("%d %d", &a, &b);

printf("Enter operator (+, -, *, /, %%): ");


scanf(" %c", &op);

switch (op) {
case '+':
printf("Result: %d\n", a + b);
break;
case '-':
printf("Result: %d\n", a - b);
break;
case '*':
printf("Result: %d\n", a * b);
break;
case '/':
if (b != 0)
printf("Result: %.2f\n", (float)a / b);
else
printf("Error! Division by zero.\n");
break;
case '%':
printf("Result: %d\n", a % b);
break;
default:
printf("Invalid operator\n");
}

return 0;
}

9. Find both the largest and smallest number in a list of integers


c
Copy code
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int largest = arr[0], smallest = arr[0];


for (int i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
if (arr[i] < smallest)
smallest = arr[i];
}

printf("Largest: %d, Smallest: %d\n", largest, smallest);


return 0;
}

10. Addition and multiplication of two matrices using functions


c
Copy code
#include <stdio.h>

void add_matrices(int a[3][3], int b[3][3], int result[3][3]) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void multiply_matrices(int a[3][3], int b[3][3], int result[3][3]) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}

int main() {
int a[3][3], b[3][3], result[3][3];

printf("Enter elements of first matrix (3x3):\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of second matrix (3x3):\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}

add_matrices(a, b, result);
printf("Sum of matrices:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

multiply_matrices(a, b, result);
printf("Product of matrices:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

You might also like