0% found this document useful (0 votes)
8 views11 pages

Exercise

Uploaded by

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

Exercise

Uploaded by

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

Find sum of digits of a number

#include <stdio.h>
int main() {
int n, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
while(n > 0) {
digit = n % 10; // last digit
sum += digit; // add to sum
n = n / 10; // remove last digit
}
printf("Sum of digits = %d", sum);
return 0;
}
Take input until user enters a positive number
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while(num <= 0);
printf("You entered: %d", num);
return 0;
}
Reverse the digits of a number
#include <stdio.h>
int main() {
int n, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
while(n > 0) {
digit = n % 10; // extract last digit
rev = rev * 10 + digit; // build reverse number
n = n / 10; // remove last digit
}
printf("Reversed number = %d", rev);
return 0;
}
Find factorial of a number
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
int i = 1;
while(i <= n) {
fact = fact * i; // multiply
i++;
}
printf("Factorial of %d = %d", n, fact);
return 0;
}
Count digits in a number
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
n = n / 10;
count++;
}
printf("Total digits = %d", count);
return 0;
}
Check if a number is palindrome
#include <stdio.h>
int main() {
int n, rev = 0, digit, temp;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(n > 0) {
digit = n % 10;
rev = rev * 10 + digit;
n = n / 10;
}
if(temp == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
Keep entering numbers until user enters 0, then print sum
#include <stdio.h>
int main() {
int num, sum = 0;
do {
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
sum += num;
} while(num != 0);
printf("Total sum = %d", sum);
return 0;
}
Write a program in C to perform basic operations such as addition of two
matrices // Input elements of second matrix
printf("\nEnter elements of second matrix (B):\n");
#include <stdio.h> for(int i = 0; i < rows; i++) {
int main() { for(int j = 0; j < cols; j++) {
int rows, cols; printf("B[%d][%d] = ", i+1, j+1);
// Input matrix size scanf("%d", &b[i][j]);
printf("Enter number of rows: "); }
}
scanf("%d", &rows);
// Perform matrix addition
printf("Enter number of columns: "); for(int i = 0; i < rows; i++) {
scanf("%d", &cols); for(int j = 0; j < cols; j++) {
int a[rows][cols], b[rows][cols], sum[rows][cols]; sum[i][j] = a[i][j] + b[i][j];
}
// Input elements of first matrix }
printf("\nEnter elements of first matrix (A):\n"); // Display result
for(int i = 0; i < rows; i++) { printf("\nResultant Matrix (A + B):\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
for(int j = 0; j < cols; j++) {
printf("A[%d][%d] = ", i+1, j+1); printf("%d\t", sum[i][j]);
scanf("%d", &a[i][j]); }
} printf("\n");
} }
return 0;
}
Write a program in C to perform basic operations such as addition of two
matrices (Using do while loop with Switch)
switch(choice) {
#include <stdio.h>
case 1:
#define SIZE 10 // maximum size of matrix printf("Enter number of rows: ");
int main() { scanf("%d", &rows);
int a[SIZE][SIZE], b[SIZE][SIZE], sum[SIZE] printf("Enter number of columns: ");
[SIZE]; scanf("%d", &cols);
int rows, cols, i, j; printf("Enter elements of Matrix A:\n");
int choice; for(i = 0; i < rows; i++) {
do { for(j = 0; j < cols; j++) {
printf("\n====== Matrix Menu ======\n"); scanf("%d", &a[i][j]);
}
printf("1. Enter matrices\n");
}
printf("2. Add matrices\n"); printf("Enter elements of Matrix B:\n");
printf("3. Display result\n"); for(i = 0; i < rows; i++) {
printf("4. Exit\n"); for(j = 0; j < cols; j++) {
printf("Enter your choice: "); scanf("%d", &b[i][j]);
scanf("%d", &choice); }
}
printf("Matrices entered successfully!\n");
break;
Write a program in C to perform basic operations such as addition of two
matrices (Using do while loop with Switch)
case 2: case 4:
for(i = 0; i < rows; i++) { printf("Exiting program...\n");
for(j = 0; j < cols; j++) { break;
sum[i][j] = a[i][j] + b[i][j]; default:
} printf("Invalid choice! Please try again.\n");
} }
printf("Matrices added successfully!\n"); } while(choice != 4);
break; return 0;
case 3:
printf("Resultant Matrix (A + B):\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
break;
Take input until user enters a positive number

You might also like