0% found this document useful (0 votes)
3 views25 pages

Programming in C Lab

The document contains a series of C programming exercises, each with a specific task such as printing a message, performing arithmetic operations, calculating the area of a circle, checking leap years, and more. Each exercise includes the C code implementation along with sample input and output. The tasks demonstrate various programming concepts including loops, conditionals, arrays, and functions.

Uploaded by

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

Programming in C Lab

The document contains a series of C programming exercises, each with a specific task such as printing a message, performing arithmetic operations, calculating the area of a circle, checking leap years, and more. Each exercise includes the C code implementation along with sample input and output. The tasks demonstrate various programming concepts including loops, conditionals, arrays, and functions.

Uploaded by

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

-: Programming in “C” Lab :-

1) Write a C Program to print “This is My First C Program”.

Ans)
#include<stdio.h>
#include<conio.h>
Void main()
{
clrscr();
printf(“This is My First C Program”);
getch();
}

OutPut :- This is My First C Program


2) Write a C program to perform arithmetic operations.

#include <stdio.h>

int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div; // Division might result in a floating-point number

printf("Enter the first number: ");


scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

sum = num1 + num2;


sub = num1 - num2;
mult = num1 * num2;

div = (float)num1 / num2;

mod = num1 % num2;

printf("\nResults:\n");
printf("Addition: %d + %d = %d\n", num1, num2, sum);
printf("Subtraction: %d - %d = %d\n", num1, num2, sub);
printf("Multiplication: %d * %d = %d\n", num1, num2, mult);
printf("Division: %d / %d = %.2f\n", num1, num2, div);
printf("Modulus: %d %% %d = %d\n", num1, num2, mod);

return 0;
}
Output:-

Enter the first number: 25


Enter the second number: 20

Results:
Addition: 25 + 20 = 45
Subtraction: 25 - 20 = 5
Multiplication: 25 * 20 = 500
Division: 25 / 20 = 1.25
Modulus: 25 % 20 = 5
3) Write a C program to calculate area of the circle & circumferences of a
circle (pi value as a constant)

#include <stdio.h>

int main() {

const float PI = 3.14159;


float radius;
float area;
float circumference;

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

area = PI * radius * radius;

circumference = 2 * PI * radius;

printf("The area of the circle is: %.2f\n", area);


printf("The circumference of the circle is: %.2f\n", circumference);

return 0;
}
Output:-

Enter the radius of the circle: 15


The area of the circle is: 706.86
The circumference of the circle is: 94.25
4) Write a C program to check whether the entered year is a leap year or
not.

#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

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


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

return 0;
}
Output :-
Enter a year: 2020
2020 is a leap year.
5) Write a C Program to display factorial of a given number (take the
number as user input)

#include <stdio.h>

int main() {
int num, i;
unsigned long long factorial = 1;

printf("Enter a number to find its factorial: ");


scanf("%d", &num);

if (num < 0) {
printf("Factorial of a negative number doesn't exist.\n");
}
else {
for (i = 1; i <= num; ++i) {
factorial *= i;
}

printf("Factorial of %d = %llu\n", num, factorial);


}

return 0;
}
Output:-

Enter a number to find its factorial: 5


Factorial of 5 = 120
6) Write a C program to print the days names using switch cases.

#include <stdio.h>

int main() {
int day;

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


scanf("%d", &day);

switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
}

return 0;
}
Output:-

Enter day number (1-7): 5


Friday
7) Write a C program to print the sum of 1 to 10 number using a for loop.

#include <stdio.h>

int main() {
int sum = 0;
int i;

for (i = 1; i <= 10; i++) {


sum = sum + i;
}
printf("The sum of numbers from 1 to 10 is: %d\n", sum);

return 0;
}
Output:-

The sum of numbers from 1 to 10 is: 55


8) Write a Program to find the largest and smallest elements element in
Array .

#include <stdio.h>
int main() {
int n;

printf("Enter the size of the array: ");


scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int largest = arr[0];


int smallest = arr[0];

for (int i = 1; i < n; i++) {

if (arr[i] > largest) {


largest = arr[i];
}

if (arr[i] < smallest) {


smallest = arr[i];
}
}
printf("Largest element: %d\n", largest);
printf("Smallest element: %d\n", smallest);

return 0;
}
Output:-

Enter the size of the array: 25


Enter the elements of the array: C Programming
Largest element: 508871392
Smallest element: -657944752
9) Write a Program to display following patterns.

*
* *
* * *
* * * *
* * * * *

#include <stdio.h>

int main() {
int rows, i, j;

printf("Enter number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


// Inner loop for stars
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}
Output:-

Enter number of rows: 5


*
**
***
****
*****
10) Write a C Program to perform Matrix Addition using 2-D array.

#include <stdio.h>

int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

int matrix1[rows][cols];
int matrix2[rows][cols];
int sumMatrix[rows][cols];

printf("\nEnter elements for the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element matrix1[%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

printf("\nEnter elements for the second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element matrix2[%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("\nSum of the two matrices:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", sumMatrix[i][j]);
}
printf("\n");
}

return 0;
}
OutPut:-

Enter the number of rows: 2


Enter the number of columns: 2

Enter elements for the first matrix:


Enter element matrix1[0][0]: 2
Enter element matrix1[0][1]: 3
Enter element matrix1[1][0]: 6
Enter element matrix1[1][1]: 5

Enter elements for the second matrix:


Enter element matrix2[0][0]: 3
Enter element matrix2[0][1]: 6
Enter element matrix2[1][0]: 8
Enter element matrix2[1][1]: 4

Sum of the two matrices:


5 9
14 9
11) Write a C program to Multiply Matrix by 2 using a 2-D array.

#include <stdio.h>

int main() {
int matrix[10][10];
int result[10][10];
int rows, cols, i, j;

printf("Enter number of rows: ");


scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

printf("\nEnter elements of the matrix:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

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


for (j = 0; j < cols; j++) {
result[i][j] = matrix[i][j] * 2;
}
}

printf("\nMatrix after multiplying by 2:\n");


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}

return 0;
}
Output:-

Enter number of rows: 2


Enter number of columns: 3

Enter elements of the matrix:


123
456

Matrix after multiplying by 2:


2 4 6
8 10 12
12) Write a program to swap values of two variables using a pointer.

#include <stdio.h>

int main() {
int a, b, temp;
int *p1, *p2;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

p1 = &a;
p2 = &b;

printf("\nBefore swapping:\n");
printf("a = %d, b = %d\n", a, b);

temp = *p1;
*p1 = *p2;
*p2 = temp;

printf("\nAfter swapping:\n");
printf("a = %d, b = %d\n", a, b);

return 0;
}
Output:-

Enter two numbers:


25
36

Before swapping:
a = 25, b = 36

After swapping:
a = 36, b = 25

You might also like