Program 1: WRITE A PROGRAM to display your name.
Write another program
to print message with inputted name.
Solution → Display the Name:
#include <stdio.h>
int main() {
printf("My name is AYUSH");
return 0;
}
→ Print message with inputted name
#include <stdio.h>
int main() {
char name[30];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello %s, welcome to C programming!", name);
return 0;
}
OUTPUT
My name is AYUSH
Program 2: WRITE A PROGRAM to add two numbers.
Solution →
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
OUTPUT
Enter two numbers: 1
2
Sum = 3
Program 3: WRITE A PROGRAM to find the square of a given number.
Solution →
#include <stdio.h>
int main() {
int num, square;
printf("Enter a number: ");
scanf("%d", &num);
square = num * num;
printf("Square = %d", square);
return 0;
}
OUTPUT
Enter a number: 5
Square = 25
Program 4: WRITE A PROGRAM to calculate the average of three numbers.
Solution →
#include <stdio.h>
int main() {
float a, b, c, avg;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
avg = (a + b + c) / 3;
printf("Average = %.2f", avg);
return 0;
}
OUTPUT
Enter three numbers: 55
56
45
45
Average = 52.00
Program 5: Write a program to Find ASCII Value of a Character.
Solution →
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("ASCII value of %c = %d", ch, ch);
return 0;
}
OUTPUT
Enter a character: a
ASCII value of a = 97
Program 6: WRITE A PROGRAM to Find the Size of int, float, double
and char
Solution →
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu byte\n", sizeof(char));
return 0;
}
OUTPUT
Size of different data types in C:
=====================================
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
=====================================
Additional information:
Size of short int: 2 bytes
Size of long int: 8 bytes
Size of long double: 16 bytes
Program 7: WRITE A PROGRAM to Compute Quotient and
Remainder
Solution →
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend and divisor: ");
scanf("%d %d", ÷nd, &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
OUTPUT
Enter dividend and divisor: 30
2
Quotient = 15
Remainder = 0
Program 8: WRITE A PROGRAM to accept the values of two variables.
Solution →
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("You entered: %d and %d", a, b);
return 0;
}
OUTPUT
Enter two numbers: 5
5
You entered: 5 and 5
Program 9: WRITE A PROGRAM to find the simple interest, inputs are amount,
period in years and rate of interest.
Solution →
#include <stdio.h>
int main() {
float amount, rate, time, si;
printf("Enter amount, rate of interest and time (in years): ");
scanf("%f %f %f", &amount, &rate, &time);
si = (amount * rate * time) / 100;
printf("Simple Interest = %.2f", si);
return 0;
}
OUTPUT
Enter amount, rate of interest and time (in years): 20000
10
15
Simple Interest = 30000.00
Program 10: Basic salary of an employee is input through the keyboard. The DA is 25% of the basic salary
while the HRA is 15% of the basic salary. Provident Fund is deducted at the rate of 10% of the gross
salary(BS+DA+HRA). WRITE A PROGRAM to calculate the net salary
Solution → #include <stdio.h>
void main() {
float basic Salary, DA, HRA, gross Salary, PF, net Salary;
// Input basic salary
printf("Enter basic salary of the employee: ");
scanf("%f", &basic Salary);
// Calculate DA (25% of basic salary)
DA = 0.25 * basic Salary;
// Calculate HRA (15% of basic salary)
HRA = 0.15 * basic Salary;
// Calculate gross salary
gross Salary = basic Salary + DA + HRA;
// Calculate Provident Fund (10% of gross salary)
PF = 0.10 * gross Salary;
// Calculate net salary
net Salary = gross Salary - PF;
// Display results
printf("\nBasic Salary: %.2f", basic Salary);
printf("\nDA (25%%): %.2f", DA);
printf("\nHRA (15%%): %.2f", HRA);
printf("\n Gross Salary: %.2f", gross Salary);
printf("\nProvident Fund (10%% of Gross Salary): %.2f", PF);
printf("\n Net Salary: %.2f\n", net Salary1
);
}
OUTPUT
Enter the basic salary: 20000
----- Salary Details -----
Basic Salary: 20000.00
DA (25%): 5000.00
HRA (15%): 3000.00
Gross Salary: 28000.00
Provident Fund (10%): 2800.00
Net Salary: 25200.00
Program 11: WRITE A PROGRAM to find area of a circle using PI as constant
Solution →
#include <stdio.h>
#define PI 3.14159 // Define constant PI
int main() {
float radius, area;
printf("Enter radius of the circle: ");
scanf("%f", &radius); area = PI * radius * radius;
printf("Area of the circle = %.2f", area);
return 0;
}
OUTPUT
Enter radius of the circle: 5
Area of the circle = 78.54
Program 12: WRITE A PROGRAM to find volume of a cube using side as
input from user
Solution →
#include <stdio.h>
int main() {
float side, volume;
printf("Enter the side of the cube: ");
scanf("%f", &side); volume = side * side * side;
printf("Volume of the cube = %.2f", volume);
return 0;
}
OUTPUT
Enter the side of the cube: 10
Volume of the cube = 1000.00
Program 13: WRITE A PROGRAM using various unformatted Input Functions
Solution →
#include <stdio.h>
int main() {
char ch, name[50];
int i;
// 1. getchar() - reads one character
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n\n", ch);
// Clear input buffer
while(getchar() != '\n');
// 2. Another getchar() example
printf("Enter another character: ");
ch = getchar();
printf("You entered: %c\n\n", ch);
// Clear input buffer
while(getchar() != '\n');
// 3. Read string using getchar() in loop
printf("Enter your name: ");
i = 0;
while((ch = getchar()) != '\n') {
name[i] = ch;
i++;
}
name[i] = '\0';
printf("Hello, %s!\n", name);
return 0;
}
OUTPUT
Enter a character: a
You entered: a
Enter another character: b
You entered: b
Enter your name: Ayush
Hello, Ayush!
Program 14: WRITE A PROGRAM to find area of rectangle and print the result
using unformatted output Functions
Solution →
#include <stdio.h>
int main() {
float length, width, area;
// Input length and width
printf("Enter length and width of rectangle: ");
scanf("%f %f", &length, &width);
// Calculate area
area = length * width;
// Output the area
printf("Area of rectangle = %.2f", area);
return 0;
}
OUTPUT
Enter length and width of rectangle: 50
60
Area of rectangle = 3000.00
Program 15: WRITE A PROGRAM to find the larger of two numbers.
Solution →
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is larger", a);
else if (b > a)
printf("%d is larger", b);
else
printf("Both numbers are equal");
return 0;
}
OUTPUT
Enter two numbers: 2
30
30 is larger
Program 16: WRITE A PROGRAM to find greater of three numbers using Nested If.
Solution →
#include <stdio.h>
int main() {
int a, b, c, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b) {
if(a > c)
largest = a;
else
largest = c;
} else {
if(b > c)
largest = b;
else
largest = c;
}
printf("The largest number is %d", largest);
return 0;
}
OUTPUT
Enter three numbers: 5
4
9
The largest number is 9
Program 17: WRITE A PROGRAM to find whether the given number is even or
odd.
Solution → #include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even", num);
else
printf("%d is odd", num);
return 0;
}
OUTPUT
Enter a number: 95
95 is odd
Program 18: WRITE A PROGRAM to Generate Multiplication Table Using for
loop
Solution → #include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
OUTPUT
Enter a number: 654
Multiplication table of 654:
654 x 1 = 654
654 x 2 = 1308
654 x 3 = 1962
654 x 4 = 2616
654 x 5 = 3270
654 x 6 = 3924
654 x 7 = 4578
654 x 8 = 5232
654 x 9 = 5886
654 x 10 = 6540
Program 19: WRITE A PROGRAM to Generate Multiplication Table Using while loop
Solution →
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
while(i <= 10) {
printf("%d x %d = %d\n", num, i, num * i);
i++;
}
return 0;
}
OUTPUT
Enter a number: 54
Multiplication table of 54:
54 x 1 = 54
54 x 2 = 108
54 x 3 = 162
54 x 4 = 216
54 x 5 = 270
54 x 6 = 324
54 x 7 = 378
54 x 8 = 432
54 x 9 = 486
54 x 10 = 540
Program 20: WRITE A PROGRAM to Make a Simple Calculator Using switch...case
Solution →
#include <stdio.h>
int main() {
char op;
float num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op); // space before %c to ignore leftover newline
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
result = num1 / num2;
else {
printf("Error! Division by zero.");
return 0;
}
break;
default:
printf("Invalid operator!");
return 0;
}
printf("Result: %.2f", result);
return 0;
}
OUTPUT
Enter operator (+, -, *, /): +
Enter two numbers: 8
6
Result = 14.00
Program 21: WRITE A PROGRAM to find whether the given number is a prime number.
Solution →
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
if(num <= 1) {
printf("%d is not a prime number.", num);
return 0;
}
for(i = 2; i <= num/2; i++) {
if(num % i == 0) {
flag = 1;
break;
}
}
if(flag == 0)
printf("%d is a prime number.", num);
else
printf("%d is not a prime number.", num);
return 0;
}
OUTPUT
Enter a number: 2
2 is a prime number.
Enter a number: 55
55 is not a prime number.
Program 22: WRITE A PROGRAM using function to print first 20 numbers and its
squares.
Solution →
#include <stdio.h>
void printSquares() {
int i;
printf("Number\tSquare\n");
for(i = 1; i <= 10; i++) {
printf("%d\t%d\n", i, i * i);
}
}
void main() {
printSquares(); // Call the function
}
OUTPUT
Number Square
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Program 23: WRITE A PROGRAM to find the factorial of a given number.
Solution →
#include <stdio.h>
int main() {
int num, i;
unsigned long long fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
if(num < 0) {
printf("Factorial of a negative number doesn't exist.");
return 0;
}
for(i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d = %llu", num, fact);
return 0;
}
OUTPUT
Enter a number: 23
Factorial of 23 = 8128291617894825984