You are on page 1of 11

C programming practical

PROGRAM 1

WRITE A PORG TO ENETER THE USER INPUT IN THE PORGRAM .

#include <stdio.h>

int main() {

int number;

printf("Enter an integer: ");

scanf("%d", &number);

if (number % 2 == 0) {

printf("%d is even.\n", number);

} else {

printf("%d is odd.\n", number);

return 0;

}
Program 2

Develop a C Program that defines two integer variables (num1, and num 2), assign values to them ,
and perform the following operations

Coding

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 5;

int sum = num1 + num2;

int difference = num1 - num2;

int product = num1 * num2;

int quotient = num1 / num2;

int remainder = num1 % num2;

printf("num1 + num2 = %d\n", sum);

printf("num1 - num2 = %d\n", difference);

printf("num1 * num2 = %d\n", product);

printf("num1 / num2 = %d\n", quotient);

printf("num1 %% num2 = %d\n", remainder);

return 0;

}
OUTPUT

PROGRAM 3

Create a program that checks weather a given year is a leap year. Take the year as input and print
a message indicating weather it is a leap year or not .

#include <stdio.h>

int main() {

int year;

// Input

printf("Enter a year: ");

scanf("%d", &year);

// Check if it's a leap year

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

printf("%d is a leap year.\n", year);

} else {

printf("%d is not a leap year.\n", year);

return 0;

}
OUTPUT

PROGRAM 4

Write a C program that takes two numbers as input and prints the maximum of the two.

#include <stdio.h>

int main() {

int num1, num2;

// Input

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

// Check and print

if (num1 > num2) {

printf("The maximum is: %d\n", num1);

} else {

printf("The maximum is: %d\n", num2);

return 0;

}
OUTPUT

PROGAM 5

Develop a program that takes a person's age as input and prints their age category:

• 0-12: Child
• 13-17: Teenager
• 18-59: Adult
• 60 and above: Senior Citizen

CODING

#include <stdio.h>

int main() {

int age;

// Input

printf("Enter your age: ");

scanf("%d", &age);

// Check and print category

if (age >= 0 && age <= 12) {

printf("Category: Child\n");

} else if (age >= 13 && age <= 17) {

printf("Category: Teenager\n");

} else if (age >= 18 && age <= 59) {

printf("Category: Adult\n");

} else {
printf("Category: Senior Citizen\n");

return 0;

OUTPUT

PROGRAM 6

Write a C program that takes a number (1 to 5) as input and prints the corresponding day of
the week.

#include <stdio.h>

int main() {

int day;

printf("Enter a number (1-7): ");

scanf("%d", &day);

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;

OUTPUT
PROGRAM 7

Create a C program that takes a non-negative integer as input and calculates its factorial
using a do-while loop.

#include <stdio.h>

int main() {

int n, factorial = 1, i = 1;

// Input

printf("Enter a non-negative integer: ");

scanf("%d", &n);

// Calculate factorial using do-while loop

do {

factorial *= i;

i++;

} while (i <= n);

// Print the result

printf("Factorial of %d: %d\n", n, factorial);

return 0;

}
OUTPUT

PROGRAM 8

Write a Program on Matrix Addition

#include <stdio.h>

int main() {

int rows, cols;

// Input matrix dimensions

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

// Declare matrices

int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

// Input elements of matrix1

printf("Enter elements of matrix1:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

printf("Enter element at position (%d, %d): ", i + 1, j + 1);

scanf("%d", &matrix1[i][j]);
}

// Input elements of matrix2

printf("Enter elements of matrix2:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

printf("Enter element at position (%d, %d): ", i + 1, j + 1);

scanf("%d", &matrix2[i][j]);

// Perform matrix addition

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];

// Display the result

printf("Sum of the matrices:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

printf("%d\t", sum[i][j]);

printf("\n");

return

};
OUTPUT

You might also like