JCDM COLLEGE OF
ENGINEERING
PRACTICAL FILE
ON
PROGRAMMING FOR PROGRAM SOLVING
USING C (ESC-103)
SUMITTED TO: SUMITTED BY:
MRS. Aman paul Himanshu Giri
Assitant professor,csedeptt. Btech Cse(1st sem)
C0225153
PROGRAM NO 1
AIM: Write a program to add,subtract,multiply and divide
two integers
Source code :-
#include<stdio.h>
int main(){
// declare variables
int a,b,sum;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
// calculate the sum of two numbers
sum = a + b ;
// print the result
printf("%d + %d = %d",a,b,sum);
return 0 ;
}
OUTPUT 1 :-
#include<stdio.h>
int main(){
// declare variables
int a,b,multi;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
// calculate the multi of two numbers
multi = a * b ;
// print the result
printf("%d * %d = %d",a,b,multi);
return 0 ;
}
OUTPUT :-
#include<stdio.h>
int main(){
// declare variables
int a,b,divide;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
// calculate the divide of two numbers
divide = a/b ;
// print the result
printf("%d/%d = %d",a,b,divide);
return 0 ;
}
OUTPUT :-
PROGRAM NO 2
AIM : Write a program to find average and percentage of
five subjects marks.
SOURCE CODE :
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math,
&comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
percentage = (total / 500.0) * 100;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f", percentage);
return 0;
}
OUTPUT :-
PROGRAM NO 3
AIM: write a program to find area and circumference of a
circle.
SOURCE CODE :
#include<stdio.h>
int main() {
int radius;
float PI = 3.14;
float area, circumference;
printf("Enter the radius of the circle: ");
scanf("%d", &radius);
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("\nArea of Circle: %f", area);
printf("\nCircumference of Circle: %f", circumference);
return 0;
}
OUTPUT:-
PROGRAM NO 4
AIM: Write a program to find root of quadratic equation.
SOURCE CODE :-
#include <stdio.h>
#include <math.h> // For sqrt() function
int main() {
float a, b, c;
float discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2f and %.2f\n",
root1, root2);
}
else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal: %.2f\n", root1);
}
// Condition for complex roots
else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2f + %.2fi and %.2f
- %.2fi\n", realPart, imaginaryPart, realPart,
imaginaryPart);
}
return 0;
}
OUTPUT :-
PROGRAM NO 5
AIM : write a program to find simple and compound
interset.
SOURCE CODE :-
#include <stdio.h>
#include <math.h> // For pow() function
int main() {
float principal, rate, time;
float simpleInterest, compoundInterest;
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the rate of interest: ");
scanf("%f", &rate);
printf("Enter the time (in years): ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
compoundInterest = principal * pow((1 + rate / 100),
time) - principal;
printf("Simple Interest: %.2f\n", simpleInterest);
printf("Compound Interest: %.2f\n",
compoundInterest);
return 0;
}
OUTPUT:
PROGRAM NO 6
AIM: Write a program to check a number is even and
odd.
SOURCE CODE:-
#include<stdio.h>
int main(){
int num;
printf("Enter a number : ");
scanf("%d",&num);
if (num%2==0)
{
printf("The number is even");
}
else
{
printf("The number is odd");
}
return 0;
}
OUTPUT:1
OUTPUT:2
PROGRAM NO 7
AIM: Write a program to check if a number is positive or
negative or zero.
SOURCE CODE:
#include<stdio.h>
int main(){
int num;
printf("Enter a number : ");
scanf("%d",&num);
if (num>0){ printf("The number is positive ");}
if (num<0){ printf("The number is neative ");}
if (num=0){printf("The number is zero");}
return 0 ;
}
OUTPUT:1
OUTPUT:2
PROGRAM NO 8
AIM: Write a program to find largest number among
three numbers.
SOURCE CODE :
#include<stdio.h>
int main(){
int a,b,c;
printf("Enter a number : ");
scanf("%d%d%d",&a,&b,&c);
if (a>b && a>c){
printf("%d is the largest number ",a);
}
if (b>a && b>c){
printf("%d is the largest number ",b);
}
if (c>a && c>b){
printf("%d is the largest number ",c); }
return 0;
}
OUTPUT:
PROGRAM NO 9
AIM:Write a program to check a given year is leaper or
not.
SOURCE CODE:
OUTPUT:
PROGRAM NO 10
AIM: Write a program to swap two numbers using third
variable.
SOURCE CODE:
#include<stdio.h>
int main(){
int a = 5, b = 10, temp;
// Swapping values of a and b
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
return 0;
}
PROGRAM NO 11
AIM: Write a program to swap two numbers without
using third variable.
SOURCE CODE:
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
OUTPUT:
PROGRAM NO 12
AIM: Write a program print even numbers from 0 to 50.
SOURCE CODE :
#include<stdio.h>
int main(){
int i;
for (i=1;i<=50;i++){
if(i%2==0){
printf("%d ",i);
}
}
}
OUTPUT :
PROGRAM NO 13
AIM: Write a program to print odd numbers from 0 to
100.
SOURCE CODE :
#include<stdio.h>
int main(){
for (int i = 1; i <=100; i=i+2)
{
printf("%d ",i);
}
return 0;
}
OUTPUT :
PROGRAM NO 14
AIM: Write a program to find an element from given
array using linear search
SOURCE CODE :
#include<stdio.h>
int main (){
int arr[5]= {12,13,23,34,45};
int data = 45;
for(int i=0;i<5;i++){
if(arr[i]==data)
{
printf("Element found at index : %d",i);
break;
}
}
return 0;
}
OUTPUT :
PROGRAM NO 15
AIM: Write a program to find an element from given
array using binary search
SOURCE CODE :
#include <stdio.h>
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (x == array[mid])
return mid;
if (x > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 7;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
OUTPUT :
PROGRAM NO 16
AIM: Write a program to find roots of quadratic equation
SOURCE CODE :
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart,
imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1,
root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2
= %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
OUTPUT:
PROGRAM NO 17
AIM: Write a program to print table of given number
SOURCE CODE :
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
OUTPUT :
PROGRAM NO 18
AIM: Write a program to find addition of two matrices
SOURCE CODE :
#include <stdio.h>
int main ()
{
int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16,
17} };
int sum[3][3], i, j;
printf ("matrix 1 is :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
printf ("matrix 2 is :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
// adding two matrices
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}
// printing the sum 0f two matrices
printf ("\nSum of two matrices: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", sum[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
return 0;
}
OUTPUT
PROGRAM NO 19
AIM: Write a program to find substraction of two
matrices
SOURCE CODE :
#include <stdio.h>
int main ()
{
int mat1[3][3] = { {78, 23, 21}, {33, 44, 54}, {62, 70,
81} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16,
17} };
int sum[3][3], i, j;
printf ("matrix 1 is :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
printf ("matrix 2 is :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
// subtraction two matrices
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sum[i][j] = mat1[i][j] - mat2[i][j];
}
// printing the sum 0f two matrices
printf ("\nSum of two matrices: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", sum[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
return 0;
}
OUTPUT
PROGRAM NO 20
AIM: Write a program to find multiplication of two
matrices.
SOURCE CODE:
#include <stdio.h>
void multiplyMatrices(int firstMatrix[][10], int
secondMatrix[][10], int resultMatrix[][10], int r1, int c1,
int c2) {
// Initialize the result matrix to zero
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
resultMatrix[i][j] = 0;
}
}
// Perform matrix multiplication
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
resultMatrix[i][j] += firstMatrix[i][k] *
secondMatrix[k][j];
}
}
}
}
void displayMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int firstMatrix[10][10], secondMatrix[10][10],
resultMatrix[10][10];
int r1, c1, r2, c2;
// Input dimensions of the first matrix
printf("Enter rows and columns of the first matrix: ");
scanf("%d %d", &r1, &c1);
// Input dimensions of the second matrix
printf("Enter rows and columns of the second matrix:
");
scanf("%d %d", &r2, &c2);
// Check if multiplication is possible
if (c1 != r2) {
printf("Matrix multiplication is not possible.\n");
return 0;
}
// Input the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &firstMatrix[i][j]);
}
}
// Input the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
scanf("%d", &secondMatrix[i][j]);
}
}
// Multiply the matrices
multiplyMatrices(firstMatrix, secondMatrix,
resultMatrix, r1, c1, c2);
// Display the result
printf("Resultant matrix:\n");
displayMatrix(resultMatrix, r1, c2);
return 0;
}
OUTPUT
PROGRAM NO 21
AIM: Write a program to find transpose of matrix
SOURCE CODE :
#include <stdio.h>
void displayMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[10][10], transpose[10][10];
int rows, cols;
// Input dimensions of the matrix
printf("Enter the number of rows and columns of the
matrix: ");
scanf("%d %d", &rows, &cols);
// Input the matrix
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Compute the transpose
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Display the original matrix
printf("Original matrix:\n");
displayMatrix(matrix, rows, cols);
// Display the transpose matrix
printf("Transpose matrix:\n");
displayMatrix(transpose, cols, rows);
return 0;
}
OUTPUT
PROGRAM NO 22
AIM: Write a program to find factorial of number using
function
SOURCE CODE: #include<stdio.h>
int factorial(int);
void main(){
int num , fact;
printf("Enter numbers ");
scanf("%d",&num);
fact = factorial(num);
printf("factorial of %d is %d",num,fact);
}
int factorial (int n)
{
if(n==0 || n==1)
return 1;
else
return (n*factorial(n-1));
}
OUTPUT
PROGRAM NO 23
AIM: Write a program to find Fibonacci series for given
number using function
SOURCE CODE:
#include<stdio.h>
int fibonacci(int);
void main()
{
int n;
printf("enter no of term ");
scanf("%d",&n);
fibonacci(n);
}
int fibonacci (int num)
{
int t1=0;
int t2=1;
int next=t1+t2;
printf("fibonacci series : %d%d",t1,t2);
for(int i=3;i<=num;i++)
{
printf("%d",next);
t1=t2;
t2=next;
next=t1+t2 ;
}
}
OUTPUT:
PROGRAM NO 24
AIM: Write a program to find sum of Fibonacci series for
given number using recursion
SOURCE CODE :
#include<stdio.h>
int fibonacci(int);
void main(){
int num,fibo;
printf("enter numb to genrate fibonacci series ");
scanf("%d",&num);
fibo = fibonacci(num);
printf("sum of fibonacci series = %d",fibo);
}
int fibonacci(int n)
{
if (n==0)
return 0;
else if (n==1)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
OUTPUT :