#include <stdio.h>
int main() {
char ch;
// Prompt the user to input a character
printf("Enter a character: ");
scanf(" %c", &ch);
// Check if the input character is a vowel
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') {
printf("%c is a Vowel.\n", ch);
} else {
printf("%c is a Consonant.\n", ch);
}
return 0;
}
Q3- write a program for simple interest where p , r, t are given by user.
Sol-
#include <stdio.h>
int main() {
float p, r, t, simple_interest;
// Input principal amount, rate, and time period from the user
printf("Enter principal amount: ");
scanf("%f", &p);
printf("Enter interest rate (in percentage): ");
scanf("%f", &r);
printf("Enter time period (in years): ");
scanf("%f", &t);
// Convert interest rate from percentage to decimal
r = r / 100;
// Calculate simple interest
simple_interest = (p * r * t);
// Output the result
printf("Simple Interest = %.2f\n", simple_interest);
return 0;
}
Q4-Write a program for area of circle.
Sol-
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
// Get input from the user
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the area of the circle
area = PI * radius * radius;
// Display the result
printf("Area of the circle with radius %.2f = %.2f\n", radius, area);
return 0;
}
Q5- write a program for maximum of two number by using conditional operator.
Sol-
#include <stdio.h>
int main() {
int num1, num2, max;
// Input two numbers from the user
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Use conditional operator to find the maximum
max = (num1 > num2) ? num1 : num2;
// Display the maximum number
printf("Maximum of %d and %d is: %d\n", num1, num2, max);
return 0;
}
Q6- write a program for inserting first 10 even number.
Sol-
#include <stdio.h>
int main() {
int even_numbers[10];
int i, num = 2; // Starting from 2, the first even number
// Inserting the first 10 even numbers into the array
for (i = 0; i < 10; i++) {
even_numbers[i] = num;
num += 2; // Incrementing by 2 to get the next even number
}
// Printing the even numbers
printf("First 10 even numbers are:\n");
for (i = 0; i < 10; i++) {
printf("%d ", even_numbers[i]);
}
printf("\n");
return 0;
}
Q7- write a program to check the inserted number is even or odd.
Sol-
#include <stdio.h>
int main() {
int num;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Q8- write a program to displaying the table of inserted number.
Sol-
#include <stdio.h>
int main() {
int num;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &num);
// Display the multiplication table of the inserted number
printf("Multiplication table of %d:\n", num);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
Q9-write a program for sum of first n natural number.
Sol-
#include <stdio.h>
int main() {
int n, sum = 0;
// Input the value of n from the user
printf("Enter the value of n: ");
scanf("%d", &n);
// Calculate the sum of the first n natural numbers
for (int i = 1; i <= n; i++) {
sum += i;
}
// Display the sum
printf("Sum of first %d natural numbers = %d\n", n, sum);
return 0;
}
Q10- write a program to print half pyramid.
Sol-
#include <stdio.h>
int main() {
int rows;
// Input the number of rows from the user
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Print the half pyramid pattern
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Q11- write a program to find the factorial of an inserted number.
Sol-
#include <stdio.h>
int main() {
int num;
unsigned long long factorial = 1; // Use 'unsigned long long' to handle larger factorials
// Input a number from the user
printf("Enter a positive integer: ");
scanf("%d", &num);
// Check if the number is negative
if (num < 0) {
printf("Error: Factorial of a negative number is undefined.\n");
} else {
// Calculate the factorial
for (int i = 1; i <= num; i++) {
factorial *= i;
}
// Display the factorial
printf("Factorial of %d = %llu\n", num, factorial);
}
return 0;
}
Q12- write a program to find largest of two number using function.
Sol-
#include <stdio.h>
// Function to find the maximum of two numbers
int findMax(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
int main() {
int num1, num2;
// Input two numbers from the user
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Call the function to find the maximum
int largest = findMax(num1, num2);
// Display the result
printf("The largest number is: %d\n", largest);
return 0;
}
Q13- write a program to interchange two value by using function.
Sol-
#include <stdio.h>
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
// Input two numbers from the user
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Call the swap function to interchange the values
swap(&num1, &num2);
// After swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Q14-write a program to print the day name by switch statement
Sol-
#include <stdio.h>
int main() {
int day;
// Input a number representing the day of the week
printf("Enter a number (1-7) representing the day of the week: ");
scanf("%d", &day);
// Print the corresponding day name using switch statement
switch(day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
}
return 0;
}
Q15-write a program to insert a number and reverse the digit.
Sol-
#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &num);
// Reverse the digits of the number
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
// Display the reversed number
printf("Reversed number: %d\n", reversedNum);
return 0;
}
Q16-write a program print the prime number up to a given number.
Sol-
#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;
}
}
return true;
}
int main() {
int n;
// Input the number from the user
printf("Enter a number: ");
scanf("%d", &n);
// Print prime numbers up to n
printf("Prime numbers up to %d are: ", n);
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Q17- write a program to find the area of the rectangle (using reference)
Sol-
#include <stdio.h>
// Function to calculate the area of a rectangle using references
void calculateArea(float length, float width, float *area) {
*area = length * width;
}
int main() {
float length, width, area;
// Input length and width from the user
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
// Call the function to calculate area using references
calculateArea(length, width, &area);
// Display the area of the rectangle
printf("Area of the rectangle = %.2f\n", area);
return 0;
}
Comments