QuestionbankProgrammSolution_pps
QuestionbankProgrammSolution_pps
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>
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;
}
int main() {
int 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
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>
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
}
int main() {
int 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
Write an algorithm and c programm for the reverse of a number taken as an input.
#include <stdio.h>
int main() {
int num, rev = 0, digit;
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
}
Output
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;
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.
#include <stdio.h>
int main() {
int rows;
return 0;
}
Output
*
**
***
****
*****
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");
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
#include <stdio.h>
int main() {
int num, i;
return 0;
}
Output :
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>
int main() {
int matrixA[3][3], matrixB[3][3];
int sum[3][3], transpose[3][3], product[3][3];
return 0;
}
Output :
Transpose of Matrix A:
147
258
369
#include <stdio.h>
int main() {
int n;
int arr[n];
return 0;
}
Output
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;
return 0;
}
Output :
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;
return 0;
}
Output
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);
// Output percentage
printf("\nPercentage: %.2f%%\n", percentage);
return 0;
}
Output
Percentage: 86.60%
Grade: B
Write a C program to convert a decimal number to hexadecimal.
#include <stdio.h>
int main() {
int decimal;
return 0;
}
Output
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;
return 0;
}
Output
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>
int main() {
int limit = 50; // You can change the limit here
int sum = 0;
int num = 2; // Start checking from the first prime number
return 0;
}
Output
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>
int main() {
int num;
return 0;
}
Output
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);
return 0;
}
Output
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
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;
return 0;
}
Output
121 is a palindrome.