01 Question
01 Question
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);
}
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
#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;
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
#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
#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
#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
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
#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
output
Elements of the 3D matrix:
12
34
56
78
#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
#include <stdio.h>
int factorial(int n){
if (n == 0){
return 1;
}
7/8
01Question.md 2024-11-11
output
Enter a number: 5
Factorial of 5: 120
8/8