0% found this document useful (0 votes)
9 views17 pages

C Programs

The document contains a series of C programming exercises, each with a specific task such as displaying a name, adding two numbers, calculating the average, and finding the factorial of a number. Each program includes the code solution along with comments explaining the logic. The exercises cover basic programming concepts including input/output, arithmetic operations, control structures, and functions.

Uploaded by

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

C Programs

The document contains a series of C programming exercises, each with a specific task such as displaying a name, adding two numbers, calculating the average, and finding the factorial of a number. Each program includes the code solution along with comments explaining the logic. The exercises cover basic programming concepts including input/output, arithmetic operations, control structures, and functions.

Uploaded by

joy2008boy22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 Lovey Rathi");

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;

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;

}
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;

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;

}
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;

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; }
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", &dividend, &divisor);

quotient = dividend / divisor;

remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);

printf("Remainder = %d", remainder);

return 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;

}
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;

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 basicSalary, DA, HRA, grossSalary, PF, netSalary;

// Input basic salary

printf("Enter basic salary of the employee: ");

scanf("%f", &basicSalary);

// Calculate DA (25% of basic salary)

DA = 0.25 * basicSalary;
// Calculate HRA (15% of basic salary)

HRA = 0.15 * basicSalary;

// Calculate gross salary

grossSalary = basicSalary + DA + HRA;

// Calculate Provident Fund (10% of gross salary)

PF = 0.10 * grossSalary;

// Calculate net salary

netSalary = grossSalary - PF;

// Display results

printf("\nBasic Salary: %.2f", basicSalary);

printf("\nDA (25%%): %.2f", DA);

printf("\nHRA (15%%): %.2f", HRA);

printf("\nGross Salary: %.2f", grossSalary);

printf("\nProvident Fund (10%% of Gross Salary): %.2f", PF);

printf("\nNet Salary: %.2f\n", netSalary); }


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;

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; }

Program 13: WRITE A PROGRAM using various unformatted Input Functions


Solution → #include <stdio.h>

int main() {

int age;

char ch, name[50];

// Using scanf() to take integer input

printf("Enter your age: ");

scanf("%d", &age);

// Using getchar() to take a single character input

printf("Enter your initial: ");

getchar(); // to consume leftover newline from previous input

ch = getchar();

// Using fgets() to take string input (safer than gets)

printf("Enter your full name: ");

getchar(); // to consume leftover newline

fgets(name, sizeof(name), stdin);

printf("\nAge: %d", age);

printf("\nInitial: %c", ch);

printf("\nName: %s", name);

return 0; }

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;

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;

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;

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;

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; }

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;

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;

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;

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 <= 20; i++) {

printf("%d\t%d\n", i, i * i);

void main() {

printSquares(); // Call the function

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; // use long long for large factorials

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;

You might also like