0% found this document useful (0 votes)
33 views8 pages

01 Question

Uploaded by

Sanjay Kumar
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)
33 views8 pages

01 Question

Uploaded by

Sanjay Kumar
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/ 8

01Question.

md 2024-11-11

51. write a c programe to designe an arithmatic calculator using switch case statement

#include <stdio.h>

int main()
{
int num, output;
char op;
printf("Enter the first number: ");
scanf("%d", &num);
output = num;

while (1)
{
printf("Enter an operator (+, -, *, /) or '=' to exit: ");
scanf(" %c", &op);
if (op == '=')
{
break;
}
printf("Enter the next number: ");
scanf("%d", &num);
switch (op)
{
case '+':
output += num;
break;
case '-':
output -= num;
break;
case '*':
output *= num;
break;
case '/':
if (num != 0)
{
output /= num;
}
else
{
printf("Error: Division by zero!\n");
}
break;
default:
printf("Invalid operator!\n");
continue;
}
printf("Current result: %d\n", output);
}

printf("Final result: %d\n", output);

1/8
01Question.md 2024-11-11

return 0;
}

output
Enter the first number: 10
Enter an operator (+, -, *, /) or '=' to exit: +
Enter the next number: 20
Current result: 30
Enter an operator (+, -, *, /) or '=' to exit: *
Enter the next number: 2
Current result: 60
Enter an operator (+, -, *, /) or '=' to exit: =
Final result: 60

52. Write a c programe using menu driven function to matrix addition,subtraction,&


multiplication

#include <stdio.h>
#include <stdlib.h>

void add(int r1, int c1, int r2, int c2, int a[r1][c1], int b[r2][c2]){
if (r1 != r2 || c1 != c2){
printf("Error: Matrices must have the same dimensions!\n");
return;
}
int sum[r1][c1];
for (int i = 0; i < r1; i++){
for (int j = 0; j < c1; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of matrices:\n");
for (int i = 0; i < r1; i++){
for (int j = 0; j < c1; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
}

void subtract(int r1, int c1, int r2, int c2, int a[r1][c1], int b[r2][c2]){
if (r1 != r2 || c1 != c2){
printf("Error: Matrices must have the same dimensions!\n");
return;
}
int diff[r1][c1];
for (int i = 0; i < r1; i++){
for (int j = 0; j < c1; j++){
diff[i][j] = a[i][j] - b[i][j];
}
2/8
01Question.md 2024-11-11

}
printf("Difference of matrices:\n");
for (int i = 0; i < r1; i++){
for (int j = 0; j < c1; j++){
printf("%d ", diff[i][j]);
}
printf("\n");
}
}

void multiply(int r1, int c1, int r2, int c2, int a[r1][c1], int b[r2][c2]){
if (c1 != r2){
printf("Error: Number of columns in the first matrix must be equal to the
number of rows in the second matrix!\n");
return;
}
int prod[r1][c2];
for (int i = 0; i < r1; i++){
for (int j = 0; j < c2; j++){
prod[i][j] = 0;
for (int k = 0; k < c1; k++){
prod[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Product of matrices:\n");
for (int i = 0; i < r1; i++){
for (int j = 0; j < c2; j++){
printf("%d ", prod[i][j]);
}
printf("\n");
}
}

int main(){
int r1=3, c1=3, r2=3, c2=3;
int a[3][3]= {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int b[3][3]= {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}};
int choice;

printf("1. Add matrices\n");


printf("2. Subtract matrices\n");
printf("3. Multiply matrices\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice){
case 1:
3/8
01Question.md 2024-11-11

add(r1, c1, r2, c2, a, b);


break;
case 2:
subtract(r1, c1, r2, c2, a, b);
break;
case 3:
multiply(r1, c1, r2, c2, a, b);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
return 0;
}

output

1. Add matrices
2. Subtract matrices
3. Multiply matrices
4. Exit
Enter your choice: 1
Sum of matrices:
10 10 10
10 10 10
10 10 10

53. Write a c programe to print the digonal elements in a matrix

#include <stdio.h>
int main(){
int r = 3, c = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
printf("Diagonal elements of the matrix: ");
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
if (i == j){
printf("%d ", matrix[i][j]);
}
}
}
return 0;
}

4/8
01Question.md 2024-11-11

output
Diagonal elements of the matrix: 1 5 9

54. Write a c programe to print the upper triangular matrix

#include <stdio.h>
int main(){
int r = 3, c = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
printf("Upper triangular matrix:\n");
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
if (i <= j){
printf("%d ", matrix[i][j]);
}
else{
printf(" ");
}
}
printf("\n");
}
return 0;
}

output
Upper triangular matrix:
123
56
9

55. Write a c programe to print the transpose of a matrix

#include <stdio.h>
int main(){
int r = 3, c = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int transpose[3][3];
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
transpose[j][i] = matrix[i][j];
}
}
printf("Transpose of the matrix:\n");

5/8
01Question.md 2024-11-11

for (int i = 0; i < c; i++){


for (int j = 0; j < r; j++){
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

output
Transpose of the matrix:
147
258
369

56. Write a c programe to print the sum of the element of the matrix

#include <stdio.h>
int main(){
int r = 3, c = 3;
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int sum = 0;
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
sum += matrix[i][j];
}
}
printf("Sum of the elements of the matrix: %d\n", sum);
return 0;
}

output
Sum of the elements of the matrix: 45

57. Write a c programe to read and diplsy a 3D matrix

#include <stdio.h>
int main(){
int r = 2, c = 2, h = 2;
int matrix[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}};
printf("Elements of the 3D matrix:\n");
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){

6/8
01Question.md 2024-11-11

for (int k = 0; k < h; k++){


printf("%d ", matrix[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}

output
Elements of the 3D matrix:
12
34
56
78

58. Write a c programe to print the Fibonacci series using recursion

#include <stdio.h>
int fibonacci(int n){
if (n <= 1){
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main(){
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (int i = 0; i < n; i++){
printf("%d ", fibonacci(i));
}
return 0;
}

output
Enter the number of terms: 10
Fibonacci series: 0 1 1 2 3 5 8 13 21 34

59. Write a c programe to calculate the factorial of a number using recursion

#include <stdio.h>
int factorial(int n){
if (n == 0){
return 1;
}

7/8
01Question.md 2024-11-11

return n * factorial(n - 1);


}
int main(){
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d: %d\n", n, factorial(n));
return 0;
}

output
Enter a number: 5
Factorial of 5: 120

8/8

You might also like