Write a c program to find whether a number is Armstrong number or not.
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum
of its digits raised to the power of the number of digits.
For example:
153= 1^3 + 5^3 + 3^3 → Armstrong number
9474=9^4 + 4^4 + 7^4 + 4^4 → Armstrong number
#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int originalNum = num;
int sum = 0;
int digits = 0;
// Count the number of digits
while (originalNum != 0) {
digits++;
originalNum /= 10;
}
originalNum = num;
// Calculate the sum of digits raised to the power of the number of digits
while (originalNum != 0) {
int digit = originalNum % 10;
sum += pow(digit, digits);
originalNum /= 10;
}
return (sum == num); // Check if the number is an Armstrong number
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}
Output
Enter a number: 153
153 is an Armstrong number.
Enter a number: 123
123 is not an Armstrong number.
Write a c program to find whether a number is a prime number or not.
A prime number is a number greater than 1 that has no divisors other than 1 and itself. For
example, 2, 3, 5, 7, and 11 are prime numbers.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1)
return 0; // Numbers less than or equal to 1 are not prime
for (int i = 2; i * i <= num; i++) { // Check divisors up to square root of num
if (num % i == 0)
return 0; // Not a prime number
}
return 1; // Prime number
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
Output
Enter a number: 29
29 is a prime number.
Enter a number: 18
18 is not a prime number.
Write an algorithm and c programm for the reverse of a number taken as an input.
Algorithm to Reverse a Number
1. Start
2. Input: Read the number (num) from the user.
3. Initialize: Set rev = 0 to store the reversed number.
4. Loop:
While num is not equal to 0:
Extract the last digit of num using digit = num % 10.
Update rev = rev * 10 + digit.
Remove the last digit from num using num = num / 10.
5. Output: Display the reversed number stored in rev.
6. End
#include <stdio.h>
int main() {
int num, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
int originalNum = num; // Store original number for display
while (num != 0) {
digit = num % 10; // Extract the last digit
rev = rev * 10 + digit; // Update the reversed number
num = num / 10; // Remove the last digit
}
printf("The reverse of %d is %d\n", originalNum, rev);
return 0;
}
Output
Enter a number: 12345
The reverse of 12345 is 54321
Write a c program using a switch case to check whether a character the user enters is a vowel or
consonant.
#include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf("%c", &ch);
// Convert to lowercase for case-insensitive comparison
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A': // Handles uppercase vowels
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
// Check if it is an alphabet
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is a consonant.\n", ch);
} else {
printf("%c is not a valid alphabet.\n", ch);
}
break;
}
return 0;
}
Output
Enter an alphabet: E
E is a vowel.
Write a c program to print the pattern:
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Pattern 1: Left-aligned triangle of '*'
printf("\nPattern 1:\n");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
// Pattern 2: Left-aligned triangle of numbers
printf("\nPattern 2:\n");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
*
**
***
****
*****
Pattern 2: Number Triangle
1
12
123
1234
12345
Write a program in C to read 10 numbers from the keyboard and find their sum and average.
#include <stdio.h>
int main() {
int numbers[10];
int sum = 0;
float average;
printf("Enter 10 numbers:\n");
// Loop to read 10 numbers
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
sum += numbers[i]; // Add each number to the sum
}
// Calculate the average
average = sum / 10.0; // Use 10.0 to ensure floating-point division
// Display the results
printf("\nSum of the numbers: %d\n", sum);
printf("Average of the numbers: %.2f\n", average);
return 0;
}
Output
Enter 10 numbers:
Enter number 1: 5
Enter number 2: 10
Enter number 3: 15
Enter number 4: 20
Enter number 5: 25
Enter number 6: 30
Enter number 7: 35
Enter number 8: 40
Enter number 9: 45
Enter number 10: 50
Sum of the numbers: 275
Average of the numbers: 27.50
Write a program in C to display the multiplication table for a given integer.
#include <stdio.h>
int main() {
int num, i;
printf("Enter an integer to display its multiplication table: ");
scanf("%d", &num);
printf("\nMultiplication Table for %d:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
Output :
Enter an integer to display its multiplication table: 5
Multiplication Table for 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Write a program in C for 3x3 matrix and perform the following operations in array:
a) Addition of two matrix
b) Transpose of 3x3 matrix
c) Multiplication of two matrix
#include <stdio.h>
void matrixAddition(int a[3][3], int b[3][3], int result[3][3]) {
// Add corresponding elements of a and b
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}
void matrixTranspose(int matrix[3][3], int result[3][3]) {
// Transpose of the matrix (swap rows with columns)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[j][i] = matrix[i][j];
}
}
}
void matrixMultiplication(int a[3][3], int b[3][3], int result[3][3]) {
// Matrix multiplication
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0; // Initialize result element
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void printMatrix(int matrix[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrixA[3][3], matrixB[3][3];
int sum[3][3], transpose[3][3], product[3][3];
// Input for Matrix A
printf("Enter elements for Matrix A (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
// Input for Matrix B
printf("\nEnter elements for Matrix B (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element B[%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrixB[i][j]);
}
}
// Perform Matrix Addition
matrixAddition(matrixA, matrixB, sum);
printf("\nMatrix A + Matrix B (Sum):\n");
printMatrix(sum);
// Perform Matrix Transpose for Matrix A
matrixTranspose(matrixA, transpose);
printf("\nTranspose of Matrix A:\n");
printMatrix(transpose);
// Perform Matrix Multiplication
matrixMultiplication(matrixA, matrixB, product);
printf("\nMatrix A * Matrix B (Product):\n");
printMatrix(product);
return 0;
}
Output :
Enter elements for Matrix A (3x3):
Enter element A[1][1]: 1
Enter element A[1][2]: 2
Enter element A[1][3]: 3
Enter element A[2][1]: 4
Enter element A[2][2]: 5
Enter element A[2][3]: 6
Enter element A[3][1]: 7
Enter element A[3][2]: 8
Enter element A[3][3]: 9
Enter elements for Matrix B (3x3):
Enter element B[1][1]: 9
Enter element B[1][2]: 8
Enter element B[1][3]: 7
Enter element B[2][1]: 6
Enter element B[2][2]: 5
Enter element B[2][3]: 4
Enter element B[3][1]: 3
Enter element B[3][2]: 2
Enter element B[3][3]: 1
Matrix A + Matrix B (Sum):
10 10 10
10 10 10
10 10 10
Transpose of Matrix A:
147
258
369
Matrix A * Matrix B (Product):
30 24 18
84 69 54
138 114 90
Write a program in C to find the maximum and minimum elements in an array.
#include <stdio.h>
int main() {
int n;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input elements in the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Initialize the max and min elements as the first element
int max = arr[0];
int min = arr[0];
// Traverse the array to find the maximum and minimum
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i]; // Update max if a larger element is found
}
if (arr[i] < min) {
min = arr[i]; // Update min if a smaller element is found
}
}
// Output the results
printf("\nThe maximum element in the array is: %d\n", max);
printf("The minimum element in the array is: %d\n", min);
return 0;
}
Output
Enter the number of elements in the array: 5
Enter the elements of the array:
Element 1: 10
Element 2: 5
Element 3: 8
Element 4: 20
Element 5: 3
The maximum element in the array is: 20
The minimum element in the array is: 3
Write a C program to input basic salary of an employee and calculate its Gross salary according to
following: Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
#include <stdio.h>
int main() {
float basicSalary, hra, da, grossSalary;
// Input basic salary
printf("Enter the basic salary of the employee: ");
scanf("%f", &basicSalary);
// Calculate HRA and DA based on the basic salary
if (basicSalary <= 10000) {
hra = 0.20 * basicSalary; // 20% of basic salary
da = 0.80 * basicSalary; // 80% of basic salary
} else if (basicSalary <= 20000) {
hra = 0.25 * basicSalary; // 25% of basic salary
da = 0.90 * basicSalary; // 90% of basic salary
} else {
hra = 0.30 * basicSalary; // 30% of basic salary
da = 0.95 * basicSalary; // 95% of basic salary
}
// Calculate the gross salary
grossSalary = basicSalary + hra + da;
// Output the results
printf("\nBasic Salary: %.2f\n", basicSalary);
printf("HRA: %.2f\n", hra);
printf("DA: %.2f\n", da);
printf("Gross Salary: %.2f\n", grossSalary);
return 0;
}
Output :
Enter the basic salary of the employee: 15000
Basic Salary: 15000.00
HRA: 3750.00
DA: 13500.00
Gross Salary: 32250.00
Write a C program to input electricity unit charges and calculate total electricity bill according to
the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit An additional surcharge of 20% is added to the bill
#include <stdio.h>
int main() {
float units, totalBill, surcharge;
// Input the number of units consumed
printf("Enter the number of electricity units consumed: ");
scanf("%f", &units);
// Calculate the total bill based on unit consumption
if (units <= 50) {
totalBill = units * 0.50; // Rs. 0.50/unit for first 50 units
} else if (units <= 150) {
totalBill = 50 * 0.50 + (units - 50) * 0.75; // Rs. 0.75/unit for next 100 units
} else if (units <= 250) {
totalBill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20; // Rs. 1.20/unit for next 100 units
} else {
totalBill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50; // Rs. 1.50/unit for units
above 250
}
// Calculate the surcharge (20% of total bill)
surcharge = totalBill * 0.20;
// Add surcharge to the total bill
totalBill = totalBill + surcharge;
// Output the total bill
printf("\nTotal Electricity Bill: Rs. %.2f\n", totalBill);
return 0;
}
Output
Enter the number of electricity units consumed: 320
Total Electricity Bill: Rs. 292.00
Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and
Computer.
Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
#include <stdio.h>
int main() {
float physics, chemistry, biology, mathematics, computer;
float totalMarks, percentage;
// Input marks for five subjects
printf("Enter marks for Physics: ");
scanf("%f", &physics);
printf("Enter marks for Chemistry: ");
scanf("%f", &chemistry);
printf("Enter marks for Biology: ");
scanf("%f", &biology);
printf("Enter marks for Mathematics: ");
scanf("%f", &mathematics);
printf("Enter marks for Computer: ");
scanf("%f", &computer);
// Calculate total marks and percentage
totalMarks = physics + chemistry + biology + mathematics + computer;
percentage = (totalMarks / 500) * 100; // Total maximum marks = 500
// Output percentage
printf("\nPercentage: %.2f%%\n", percentage);
// Determine grade based on percentage
if (percentage >= 90) {
printf("Grade: A\n");
} else if (percentage >= 80) {
printf("Grade: B\n");
} else if (percentage >= 70) {
printf("Grade: C\n");
} else if (percentage >= 60) {
printf("Grade: D\n");
} else if (percentage >= 40) {
printf("Grade: E\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Output
Enter marks for Physics: 85
Enter marks for Chemistry: 78
Enter marks for Biology: 92
Enter marks for Mathematics: 88
Enter marks for Computer: 90
Percentage: 86.60%
Grade: B
Write a C program to convert a decimal number to hexadecimal.
#include <stdio.h>
void decimalToHexadecimal(int decimal) {
// Declare a string to store hexadecimal result
char hexadecimal[50];
int i = 0;
// Edge case for 0
if (decimal == 0) {
printf("Hexadecimal: 0\n");
return;
}
// Convert decimal to hexadecimal
while (decimal != 0) {
int remainder = decimal % 16;
// Check remainder and convert to corresponding hexadecimal character
if (remainder < 10) {
hexadecimal[i] = remainder + '0'; // Convert to character '0' to '9'
} else {
hexadecimal[i] = remainder - 10 + 'A'; // Convert to character 'A' to 'F'
}
decimal = decimal / 16;
i++;
}
// Print the hexadecimal number in reverse
printf("Hexadecimal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}
printf("\n");
}
int main() {
int decimal;
// Input decimal number
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Convert decimal to hexadecimal
decimalToHexadecimal(decimal);
return 0;
}
Output
Enter a decimal number: 254
Hexadecimal: FE
Write a C program that implements a program to count the number of digits in a given integer using
a do-while loop.
#include <stdio.h>
int main() {
int number, count = 0;
// Input the integer
printf("Enter an integer: ");
scanf("%d", &number);
// Handle negative numbers
if (number < 0) {
number = -number; // Convert to positive for counting digits
}
// Count digits using do-while loop
do {
count++; // Increment the count for each digit
number = number / 10; // Remove the last digit
} while (number != 0); // Continue until the number becomes 0
// Output the number of digits
printf("Number of digits: %d\n", count);
return 0;
}
Output
Enter an integer: 12345
Number of digits: 5
Write a C program that calculates and prints the sum of prime numbers up to a specified limit (e.g.,
50) using a do-while loop.
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false; // Not a prime number
}
}
return true; // Prime number
}
int main() {
int limit = 50; // You can change the limit here
int sum = 0;
int num = 2; // Start checking from the first prime number
// Calculate the sum of prime numbers using a do-while loop
do {
if (isPrime(num)) {
sum += num; // Add the prime number to the sum
}
num++; // Increment to check the next number
} while (num <= limit); // Continue until the limit is reached
// Output the sum of prime numbers up to the limit
printf("Sum of prime numbers up to %d is: %d\n", limit, sum);
return 0;
}
Output
Sum of prime numbers up to 50 is: 328
Write a C program that checks if a positive integer is divisible by either 3 or 7, or both. If the
integer is a multiple of 3, then the program will return true. Similarly, if the integer is a multiple of
7, then also the program will return true. If the integer is not a multiple of 3 or 7, then the program
will return false.
#include <stdio.h>
#include <stdbool.h>
// Function to check if the number is divisible by 3 or 7
bool isDivisible(int num) {
if (num % 3 == 0 || num % 7 == 0) {
return true; // Divisible by either 3 or 7
}
return false; // Not divisible by 3 or 7
}
int main() {
int num;
// Input positive integer
printf("Enter a positive integer: ");
scanf("%d", &num);
// Check if the number is divisible by 3 or 7
if (num <= 0) {
printf("Please enter a positive integer.\n");
} else {
if (isDivisible(num)) {
printf("The number is divisible by either 3 or 7, or both.\n");
} else {
printf("The number is not divisible by either 3 or 7.\n");
}
}
return 0;
}
Output
Enter a positive integer: 21
The number is divisible by either 3 or 7, or both.
Enter a positive integer: 10
The number is not divisible by either 3 or 7.
Write a C program that prompts the user to enter a password. Use a do-while loop to keep asking
for the password until the correct one is entered.
#include <stdio.h>
#include <string.h>
int main() {
char correctPassword[] = "mypassword"; // Predefined correct password
char enteredPassword[50]; // Array to store user input
int isCorrect = 0;
// Do-while loop to keep asking for password until the correct one is entered
do {
// Prompt user to enter the password
printf("Enter the password: ");
scanf("%s", enteredPassword);
// Check if the entered password matches the correct one
if (strcmp(enteredPassword, correctPassword) == 0) {
isCorrect = 1; // Password is correct
printf("Access granted!\n");
} else {
printf("Incorrect password. Please try again.\n");
}
} while (isCorrect == 0); // Continue until the correct password is entered
return 0;
}
Output
Enter the password: abc123
Incorrect password. Please try again.
Enter the password: mypassword
Access granted!
Write a C program that calculates and prints the sum of cubes of even numbers up to a specified
limit (e.g., 20) using a while loop.
#include <stdio.h>
int main() {
int limit = 20; // You can change the limit to any other positive integer
int sum = 0;
int num = 2; // Start from the first even number
// Using while loop to calculate the sum of cubes of even numbers
while (num <= limit) {
sum += num * num * num; // Cube the number and add to sum
num += 2; // Increment by 2 to get the next even number
}
// Output the result
printf("The sum of cubes of even numbers up to %d is: %d\n", limit, sum);
return 0;
}
Output
The sum of cubes of even numbers up to 20 is: 4400
Write c program for palindrome of number
#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);
original = num; // Store the original number for comparison
// Reverse the number
while (num != 0) {
remainder = num % 10; // Get the last digit
reversed = reversed * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit
}
// Check if the original number is equal to its reversed version
if (original == reversed) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.