0% found this document useful (0 votes)
32 views93 pages

Bachelor of Computer Adminstration (Bca) : Practical File On

This document contains a summary of programming labs using C language. It outlines 12 labs covering C basics, decision making, loops, functions, pointers, arrays, strings and recursion. Each lab lists specific programming problems to solve such as calculating sums, averages, finding factors, reversing numbers and strings. Students are tasked with writing C programs to demonstrate their understanding of these fundamental programming concepts. The document provides a record of which problems each student completed and the dates they did so.

Uploaded by

Krishna Goswami
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)
32 views93 pages

Bachelor of Computer Adminstration (Bca) : Practical File On

This document contains a summary of programming labs using C language. It outlines 12 labs covering C basics, decision making, loops, functions, pointers, arrays, strings and recursion. Each lab lists specific programming problems to solve such as calculating sums, averages, finding factors, reversing numbers and strings. Students are tasked with writing C programs to demonstrate their understanding of these fundamental programming concepts. The document provides a record of which problems each student completed and the dates they did so.

Uploaded by

Krishna Goswami
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
You are on page 1/ 93

PROGRAMMING WITH C-171

Practical File on
PROGRAMMING WITH C

BACHELOR OF COMPUTER ADMINSTRATION (BCA)

Guru Gobind Singh Indraprastha University, Delhi

Submitted To: Submitted By:


Mr. MUNNA PANDAY Name: Devyanshu Kataria
(Assistant Professor-IT) Enrollment No:01221102021
Mr. ASHISH NAYYAR BCA Semester-I (E)
(Assistant Professor-IT)

Institute of Information Technology & Management,


New Delhi – 1100 58
Batch (2021-2024)

Page 1
PROGRAMMING WITH C-171

INDEX
S No Lab Objective Problem Statement Page Date of Signature
No. Completion
1. To teach them how to use 1. Program to find sum of two 6 20-12-2021
C Basics (printf(), scanf(), numbers.
arithmetic operators) 2. Program to find area and 7 20-12-2021
circumference of circle. 20-12-2021
3. Program to find the simple
8
interest 20-12-2021
4. Program to convert 9
temperature from degree
centigrade to Fahrenheit.

2. Make them understand 1. Program to calculate sum of 10 21-12-2021


Decision Making ( If, 5 subjects & find
multiple if, nested if) percentage. 21-12-2021
12
2. Program to show swap of
two no’s with and without 21-12-2021
using third variable.
13
3. Program to find gross salary

3. To explain the use of  Program to print a table of any 14 27-12-2021


relational, conditional, if, number.
if-else, nested if-else  Program to find greatest in 3 27-12-2021
15
statements numbers 27-12-2021
 Program to show the use of 17
conditional operator. 27-12-2021
 Program to find that entered
year is leap year or not 18

4. To explain the use of  Program to find whether given 20 28-12-2021


switch, no is even or odd
 Program to shift input data by 21
28-12-2021
two bits to the left. 28-12-2021
 Program to use switch 22 28-12-2021
statement. Display Monday to
Sunday
 Program to display 23
arithmetic operator using
switch case.

5. To explain the use of for,  Program to reverse a given 25 3-01-2022


while loop do-while number. 3-01-2022

Page 2
PROGRAMMING WITH C-171

statement  Program to find sum of the


digits of 3 digit number entered 26
by the user. 3-01-2022
 Program to check whether a 3-01-2022
number is an Armstrong 27
number or not. 4-01-2022
 Program to display first 10 4-01-2022
natural no & their sum. 28
 Program to print Fibonacci 4-01-2022
series up to 100 29
 Program to display all
Armstrong numbers in a given
range 30
 Program to find factorial of
a number.
31
6. To explain the use of for,  Program to find whether given 32 10-01-2022
while loop no is a prime no or not.
 Program to display all prime 33
10-01-2022
numbers in a given range. 10-01-2022
Program to display sum of series 34
1+1/2+1/3+ ............ +1/n.

7. To explain the use of  Program to display series and 35 11-01-2022


bitwise operators find sum of 1+3+5+ ......... +n
Program to use bitwise AND 11-01-2022
36
operator between the two integers.

8. To explain the use of  Program to reverse a number 37 17-01-2022


pointers, and functions using pointer.
 Program to add two number 38
17-01-2022
using pointer. 17-01-2022
 Program to find square of a 39 18-01-2022
number using functions 40
 Program to add 2 numbers
using user defined function 18-01-2022
 Program to calculate 41
factorial of a number passed
as an argument to a user
defined function, function
should return the answer.

9. To explain the use of  Program to swap two numbers 42 24-01-2022


calling of functions using functions.

Page 3
PROGRAMMING WITH C-171

 Program to show table of a 43 24-01-2022


number using functions. 24-01-2022
44
Program to show call by value

10 To explain the use of  Program to show call by 45 25-01-2022


Calling of functions reference. 25-01-2022
Program to find largest of two
46
numbers using functions

11 To explain the use of  Program to find factorial of a 47 31-01-2022


Recursion, number using recursion.
and working of string  Program to show input and 48 31-01-2022
output of a string 31-01-2022
 Program to find whether a 49 31-01-2022
string is palindrome or not.
 WAP to concatenate two strings 51 31-01-2022
 Program to reverse the string 31-01-2022
Program to find the length of the
string 52

12 To explain the use of  Program to print array elements 54 01-02-2022


1-D, 2-D arrays in reverse order.
 Program to show sum of 10 56 01-02-2022
elements of array & show the
average. 01-02-2022
 Program to perform linear 58 01-02-2022
search. 01-02-2022
 Program to perform bubble sort. 60
 Program to find the maximum
no in an array.
62

13 To explain the use of 2-D  Program to find subtraction of 64 07-02-2022


array two matrices
 Program to find multiplication 66 07-02-2022
of two matrices
 Program to find transpose of a 68 07-02-2022
matrix 07-02-2022
 Program to display matrix. 70 07-02-2022
 Program to find sum of two
matrices 72 07-02-2022
 Program to find the 74
maximum number in array
using pointer.

Page 4
PROGRAMMING WITH C-171

14 To explain the use of  Program to enter book records 76 08-02-2022


structures  Program to enter student 78
record(name roll no, course) 08-02-2022
Program to enter book 08-02-2022
records(using union)

15 To explain the use of  Program to reverse the string 80 14-02-2022


strings  Program to concatenate two 14-02-2022
string 82 14-02-2022
 Program to copy the string 14-02-2022
 Program to change the string in 83
uppercase 84 14-02-2022
 Program to change the
string in lowercase 85

16 To explain the use of files  WAP to create a file. 87 15-02-2022


 WAP to write a content in the 15-02-2022
file 88 15-02-2022
 WAP to copy the content of one
file into another file. 89 15-02-2022
 WAP to display the content
of file on screen 91

Page 5
PROGRAMMING WITH C-171

EXPERIMENT NO. 1
1. Write a Program to find sum of two numbers.

Solution:

#include<stdio.h>
void main ()
{
printf("DEVYANSHU KATARIA , 01221102021");
int num1, num2, sum;
printf("\n Enter First Number:");
scanf("%d",&num1);
printf("\n Enter Second Number:");
scanf("%d",&num2);

sum = num1+num2;

printf(" Sum of Entered Number:%d", sum);


}

OUTPUT:

Page 6
PROGRAMMING WITH C-171

2. Program to find area and circumference of circle.

Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021");
float rad,area,circum;
printf("\n Enter The Radius of Circle:-");
scanf("%f",&rad);
area=3.14*rad*rad;
printf("\n Area of Circle = %f",area);
circum= 2*3.14*rad;
printf("\n Circumference of Circle = %f",circum);
}

OUTPUT:

Page 7
PROGRAMMING WITH C-171

3. Program to find the simple interest.

Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
float principle, time, rate, SI;

printf("Enter principle amount: ");


scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);

printf("Enter rate: ");


scanf("%f", &rate);
SI = (principle * time * rate) / 100;

printf("Simple Interest = %f", SI);


return 0;
}

Output:

Page 8
PROGRAMMING WITH C-171

4. Program to convert temperature from degree centigrade to Fahrenheit.

Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA");
float cel,fah;
printf("\n Temperature in Celsius:");
scanf("%f",&cel);
fah=(cel*9/5)+32;
printf("\n Temperature in Fahrenheit:%f",fah);
getch ();
}

Output:

Page 9
PROGRAMMING WITH C-171

EXPERIMENT NO. 2

1. Write a Program to calculate sum of 5 subjects & find percentage.

Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
printf("DEVYANSHU KATARIA");
float phy,chem,math,eng,cs,total,per;
printf("\n Enter Marks for Physics:");
scanf("%f",&phy);
printf("\n Enter Marks for English:");
scanf("%f",&eng);
printf("\n Enter Marks for Maths:");
scanf("%f",&math);
printf("\n Enter Marks for Chemistry:");
scanf("%f",&chem);
printf("\n Enter Marks for Computer Science:");
scanf("%f",&cs);
total=phy+chem+math+eng+cs;
printf("\n Total Marks Obtained:%f",total);
per=(total/500)*100;
printf("\n Percentage Obtained is:%f",per);
getch ();
}

Page 10
PROGRAMMING WITH C-171

Output:

Page 11
PROGRAMMING WITH C-171

2. Program to show swap of two no’s with and without using third variable

Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA \n");
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
getch ();
}

Output:

Page 12
PROGRAMMING WITH C-171

3. Program to find gross salary.

Solution:
#include<stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int gross_salary, basic, da, ta;

printf("Enter basic salary : ");


scanf("%d", &basic);

da = (10 * basic) / 100;


ta = (12 * basic) / 100;

gross_salary = basic + da + ta;

printf("\nGross salary : %d", gross_salary);


return 0;
}

Output:

Page 13
PROGRAMMING WITH C-171

EXPERIMENT NO. 3

1. Program to print a table of any number.

Solution:

#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i<= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
}

Output:

Page 14
PROGRAMMING WITH C-171

2. Program to find greatest in 3 numbers

Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int num1, num2, num3;
printf(" Enter the number1 = ");
scanf("%d", &num1);
printf("\n Enter the number2 = ");
scanf("%d", &num2);
printf("\n Enter the number3 = ");
scanf("%d", &num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("\n Largest number = %d \n",num1);
}
else
{
printf("\n Largest number = %d \n",num3);
}
}
else if (num2 > num3)
{
printf("\n Largest number = %d \n",num2);
}
else
{
printf("\n Largest number = %d \n",num3);
}
return 0;
}

Page 15
PROGRAMMING WITH C-171

Output:

Page 16
PROGRAMMING WITH C-171

3. Program to show the use of conditional operator.

Solution:
#include<stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num>=0)?printf(" Number is Positive."):printf(" Number is Negative");
return 0;
}

Output:

Page 17
PROGRAMMING WITH C-171

4. Program to find that entered year is leap year or not.


Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int y;
printf("Enter year: ");
scanf("%d",&y);
if(y % 4 == 0)
{
if( y % 100 == 0)
{
if ( y % 400 == 0)
printf("%d is a Leap Year", y);
else
printf("%d is not a Leap Year", y);
}
else
printf("%d is a Leap Year", y );
}
else
printf("%d is not a Leap Year", y);
return 0;
}

Page 18
PROGRAMMING WITH C-171

Output:

Page 19
PROGRAMMING WITH C-171

EXPERIMENT NO. 4

1. Program to find whether given no is even or odd

Solution:
#include <stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}

Output:

Page 20
PROGRAMMING WITH C-171

2. Program to shift input data by two bits to the left.

Solution:
#include<stdio.h>
int main()
{
int a, b;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Read the integer from keyboard-");
scanf("%d",&a);
printf("\nInteger value = %d ",a);
a<<=2;
b=a;
printf("\nThe left shifted data is = %d ",b);
}

Output:

Page 21
PROGRAMMING WITH C-171

3. Program to use switch statement. Display Monday to Sunday.


Solution:
#include <stdio.h>
void main()
{
int day;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Enter Day Number (1 = Monday ...... 7 = Sunday)\n");
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 !!!!\n");
}
}

Output:

Page 22
PROGRAMMING WITH C-171

4. Program to display arithmetic operator using switch case.


Solution:
#include<stdio.h>
void main()
{
int choice,num1,num2,result;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("****** ---- welcome to Our Program");
printf("\n 1. Additional Operation");
printf("\n 2. Subtration Operation");
printf("\n 3. Multiplication Operation");
printf("\n 4. Division Operation");
printf("\n Please Specify Your Choice:");
scanf("%d",&choice);
switch(choice )
{
case 1:
printf("\n Please Enter the Value of Num1 and Num2:");
scanf("%d%d",&num1,&num2);
result=num1+num2;
printf("\n Sum of %d, %d=%d",num1,num2,result);
break;
case 2:
printf("\n Please Enter the Value of Num1 and Num2:");
scanf("%d%d",&num1,&num2);
result=num1-num2;
printf("\n Difference between %d, %d = %d",num1,num2,result);
break;
case 3:
printf("\n Please Enter the Value of Num1 and Num2:");
scanf("%d%d",&num1,&num2);
result=num1*num2;
printf("\n Product of %d, %d = %d",num1,num2,result);

break;
case 4:
printf("\n Please Enter the Value of Num1 and Num2:");
scanf("%d%d",&num1,&num2);
result=num1/num2;
printf("\n Division of %d with %d = %d",num1,num2, result);
break;
default:
printf("\n Enter the Wrong Choice:");
}
Page 23
PROGRAMMING WITH C-171

Output:

Page 24
PROGRAMMING WITH C-171

EXPERIMENT NO. 5

1. Program to reverse a given number.


Solution:
#include <stdio.h>
void main()
{
int n, RR = 0, r, sum = 0;
printf("DEVYANSHU KATARIA ,
01221102021 \n");
printf("Enter The Integer Number:
");scanf("%d", &n);
while (n != 0)
{
r = n % 10;
RR = RR * 10 + r;
n = n / 10;
}
printf("Reversed Number = %d", RR);
}

Output:

Page 25
PROGRAMMING WITH C-171

2. Program to find sum of the digits of 3 digit number entered by the user.
Solution:
#include<stdio.h>
void main()
{
int n,sum=0,m;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
}

Output:

Page 26
PROGRAMMING WITH C-171

3. Program to check whether a number is an Armstrong number or not.


Solution:
#include <stdio.h>
void main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0)
{
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
}

Output:

Page 27
PROGRAMMING WITH C-171

4. Program to display first 10 natural no & their sums.


Solution:
#include <stdio.h>
voidmain()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)


{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}

Output:

Page 28
PROGRAMMING WITH C-171

5. Program to print Fibonacci series up to 100


Solution:
#include<stdio.h>
void main()
{
int a=0, b=1, range = 100, c, sum=0;
printf("The fibonacci series is: \t");
while( a<= range )
{
printf("%d\t",a);
sum = sum + a;
c = a + b;
a = b;
b = c;
}
printf("\nTheir sum is = %d", sum);
}

Output:

Page 29
PROGRAMMING WITH C-171

6. Program to display all Armstrong numbers in a given range


Solution:
#include <stdio.h>
void main(){
int num,r,sum,temp;
int stno,enno;
printf("Input starting number of range: ");
scanf("%d",&stno);

printf("Input ending number of range : ");


scanf("%d",&enno);

printf("Armstrong numbers in given range are: ");


for(num=stno;num<=enno;num++){
temp=num;
sum = 0;
while(temp!=0)
{
r=temp % 10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
printf("\n");
}

Output:

Page 30
PROGRAMMING WITH C-171

7. Program to find factorial of a number.


Solution:
#include<stdio.h>
void main()
{
int number, fact=1,i;
printf("\n Enter a Number:");
scanf("%d",&number);
for(i=number;i>=1;i--)
fact=fact*i;
printf("\n Factorial of %d=%d",number,fact);
}

Output:

Page 31
PROGRAMMING WITH C-171

EXPERIMENT NO. 6

1. Program to find whether given no is a prime no or not.


Solution:
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
}

Output:

Page 32
PROGRAMMING WITH C-171

2. Program to display all prime numbers in a given range.


Solution:
#include <stdio.h>
void main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);

while (low < high)


{
flag = 0;
if (low <= 1)
{
++low;
continue;
}

for (i = 2; i<= low / 2; ++i) {

if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
}

Output:

Page 33
PROGRAMMING WITH C-171

3. Program to display sum of series 1+1/2+1/3+ ..............+1/n.


Solution:
#include<stdio.h>
int main()
{
int num,i,sum=0;
printf("Input any number: ");
scanf("%d",&num);
printf("1 + ");
for(i=2;i<=num-1;i++)
printf(" 1/%d +",i);
for(i=1;i<=num;i++)
sum = sum + i;
printf(" 1/%d",num);
printf("\nSum = 1/%d",sum+1/num);
return 0;
}

Output:

Page 34
PROGRAMMING WITH C-171

EXPERIMENT NO. 7

1. Program to display series and find sum of 1+3+5+ ..........+n

Solution:

#include<stdio.h>
void main()
{
int i,n,sum=1;
printf("Enter any number n:");
scanf("%d",&n);
printf("\n1 ");
for(i=3;i<=n;i++){
printf("+ %d ",i);
sum=sum+i;
i++;
}
printf("\nSum is: %d", sum);
}
Output:

Page 35
PROGRAMMING WITH C-171

2. Program to use bitwise AND operator between the two integers.


Solution:
#include<stdio.h>
void main()
{
int a,b,c;
printf("Read the integers from keyboard:- ");
scanf("%d %d",&a,&b);
c=a&b;
printf("\nThe Answer after ANDing is: %d ",c);
}

Output:

Page 36
PROGRAMMING WITH C-171

EXPERIMENT NO. 8

1. Program to reverse a number using pointer

Solution:
#include<stdio.h>
void func(int *);
int main()
{
int n,*p;
printf("enter the number ");
scanf("%d",&n);
func(&n);
printf("\n the reverse is %d ",n);
return 0;
}
void func(int *n)
{
int i,s=0;
int a[3];
for(i=0;*n!=0;i++)
{
a[i]=*n%10;
*n=*n/10;
}
for(i=0;i<3;i++)
{
*n=*n+a[i]*pow(10,2-i);
}
}
Output:

Page 37
PROGRAMMING WITH C-171

2. Program to add two number using pointer


Solutiom:
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;

printf("Enter two integers to add\n");


scanf("%d%d", &first, &second);
p = &first;
q = &second;

sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}

Output:

Page 38
PROGRAMMING WITH C-171

3. Program to find square of a number using functions

Solution:
int main()
{
int no, square;

printf("\n Enter an no : ");


scanf("%d",&no);

square = func(no);

printf("\n Square of no is : %d ", square);


}

int func(int temp)


{
return temp*temp;
}

Output:

Page 39
PROGRAMMING WITH C-171

4.Program to add 2 numbers using user defined function


Solution:
#include<stdio.h>
int sum(int num1, int num2);
int main() {
int num1, num2, res;
printf("\nEnter the two numbers : ");
scanf("%d %d", &num1, &num2);
res = sum(num1, num2);
printf("\n Addition of two number is :%d ",res);
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}

Output:

Page 40
PROGRAMMING WITH C-171

5. Program to calculate factorial of a number passed as an argument to a


user defined function, function should return the answer.
Solution:
#include <stdio.h>
int fact(int);
void main(){
int no,factorial;

printf("Enter a number to calculate it's factorial\n");


scanf("%d",&no);
factorial=fact(no);
printf("Factorial of the num(%d) = %d\n",no,factorial);
}

int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

Outcome:

Page 41
PROGRAMMING WITH C-171

EXPERIMENT NO. 9

1. Program to swap two numbers using functions


Solution:
#include<stdio.h>
void swap(int, int);
int main()
{
int a, b;
printf("Enter values for a and b\n");
scanf("%d%d", &a, &b);
printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);
swap(a, b);
return 0;
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf("\nAfter swapping: a = %d and b = %d\n", x, y);
}

Output:

Page 42
PROGRAMMING WITH C-171

2. Program to show table of a number using functions


Solution:
#include<stdio.h>
void tables(int);
int main()
{
int num;
printf("Enter a positive number\n");
scanf("%d", &num);
printf("\nMultiplication Table for %d is:\n", num);
tables(num);
return 0;
}
void tables(int num)
{
int count;
for(count = 1; count <= 10; count++)
{
printf("%d x %d = %d\n", num, count, num*count);
}
}
Output:

Page 43
PROGRAMMING WITH C-171

3. Program to show call by value


Solution:
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);
printf("After function call x=%d \n", x);
return 0;
}
Output:

Page 44
PROGRAMMING WITH C-171

EXPERIMENT NO. 10

1. Program to show call by reference


Solution:
#include <stdio.h>
int sum(int a, int b)
{
int c=a+b;
return c;
}
int main()
{
int var1 =10;
int var2 = 20;
int var3 = sum(var1, var2);
printf("DEVYANSHU KATARIA ,
01221102021 \n");printf("%d", var3);

return 0;
}

Output:

Page 45
PROGRAMMING WITH C-171

2. Program to find largest of two numbers using functions


Solution:
#include<stdio.h>
int findLargest(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
printf("DEVYANSHU KATARIA , 01221102021 \n");
int a, b, large;
printf("Enter two numbers:");
scanf("%d%d", &a, &b);
large = findLargest(a, b);
printf("Largest is:%d", large);
return 0;
}

Output:

Page 46
PROGRAMMING WITH C-171

EXPERIMENT NO. 11

1. Program to find factorial of a number using recursion.


Solution:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output:

Page 47
PROGRAMMING WITH C-171

2. Program to show input and output of a string


Solution:
#include<stdio.h>
void main()
{
char a[50];
printf("Enter any string: \n");
gets(a);
printf("String=%s",a);
}

Output:

Page 48
PROGRAMMING WITH C-171

3. Program to find whether a string is palindrome or not.


Solution:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
printf("DEVYANSHU KATARIA , 01221102021 \n");
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0;i <len ;i++){
if(str[i] != str[len-i-1]){
temp = 1;
break;
}
}
if (temp==0) {
printf("String is a palindrome");
}
else {
printf("String is not a palindrome");
}
return 0;
}

Page 49
PROGRAMMING WITH C-171

Output:

Page 50
PROGRAMMING WITH C-171

4. Write a program to concatenate two strings


Solution:
#include <stdio.h>
int main() {
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
length = 0;
while (s1[length] != '\0') {
++length;
}
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
return 0;
}

Output:

Page 51
PROGRAMMING WITH C-171

5. Program to reverse the string


Solution:
#include <stdio.h>
#include <string.h>
void revstr(char *str1)
{
static int i, len, temp;
len = strlen(str1);
if (i<len/2){
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
i++;
revstr(str1);
}
}
int main()
{
printf("DEVYANSHU KATARIA , 01221102021\n");
char str1[50];
printf (" Enter the string: ");
gets(str1);
printf (" \n Before reversing the string: %s \n", str1);
revstr(str1);
printf (" After reversing the string: %s", str1);
}

Output:

Page 52
PROGRAMMING WITH C-171

6. Program to find the length of the string


Solution:
#include <stdio.h>
int main() {
printf("DEVYANSHU KATARIA , 01221102021 \n");
char s[50];
printf("Enter a String:\n");
gets(s);
int i;
for (i = 0; s[i] != '\0'; ++i);
printf("Length of the string: %d", i);
return 0;
}

Output:

Page 53
PROGRAMMING WITH C-171

EXPERIMENT NO. 12
1. Program to print array elements in reverse order.

Solution:

#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA,
01221102021 \n");int arr[] = {1, 2, 3,
4, 5};
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i< length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
for (int i = length-1; i>= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}

Page 54
PROGRAMMING WITH C-171

Output:

Page 55
PROGRAMMING WITH C-171

Page 56
PROGRAMMING WITH C-171

2.Program to show sum of 10 elements of array & show the average.

Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");

int a[10],i,sum=0;
float av;
printf("enter elements of an array: \n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);

}
for(i=0;i<10;i++)
{
sum=sum+a[i];
}
printf("sum=%d\n",sum);
av=sum/10;
printf("average=%f\n",av);

Page 57
PROGRAMMING WITH C-171

Output:

Page 58
PROGRAMMING WITH C-171

Page 59
PROGRAMMING WITH C-171

3. Program to perform linear search.

Solution:

#include <stdio.h>

int main()

printf("DEVYANSHU KATARIA , 01221102021 \n");

int array[100], search, c, n;

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter a number to search\n");

scanf("%d", &search);

for (c = 0; c < n; c++)


{

if (array[c] == search)

printf("%d is present at location %d.\n", search, c+1);

break;

}
Page 60
PROGRAMMING WITH C-171

if (c == n)

printf("%d isn't present in the array.\n", search);

return 0;

Output:

Page 61
PROGRAMMING WITH C-171

4. Program to perform bubble sort.


Solution:
#include<stdio.h>
void print(int a[], int n)
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
int i;
for(i = 0; i< n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n)
{
int i, j, temp;
for(i = 0; i< n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Page 62
PROGRAMMING WITH C-171

}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
Output:

Page 63
PROGRAMMING WITH C-171

5. Program to find the maximum no in an array.


Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA, 01221102021 \n");
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n",
location, maximum);
return 0;
}

Page 64
PROGRAMMING WITH C-171

Output:

Page 65
PROGRAMMING WITH C-171

EXPERIMENT NO. 13
1. Program to find subtraction of two matrices
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA, 01221102021 \n");
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & second[c][d]);
printf("Difference of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
difference[c][d] = first[c][d] - second[c][d];
printf("%d\t", difference[c][d]);
}
printf("\n");

Page 66
PROGRAMMING WITH C-171

}
return 0;
}

Output:

Page 67
PROGRAMMING WITH C-171

2. Program to find multiplication of two matrices


Solution:
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("DEVYANSHU KATARIA,
01221102021 \n"); int
a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{

Page 68
PROGRAMMING WITH C-171
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:

Page 69
PROGRAMMING WITH C-171

3. Program to find transpose of a matrix


Solution:
#include <stdio.h>
int main() {
printf("DEVYANSHU KATARIA , 01221102021 \n");
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i< r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i< r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i< r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i< c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

Page 70
PROGRAMMING WITH C-171

Output:

Page 71
PROGRAMMING WITH C-171

4. Program to find sum of two matrices


Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA, 01221102021 \n");
int i, j, m, n;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
for (i = 0; i< m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i< m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}

Page 72
PROGRAMMING WITH C-171

Output:

Page 73
PROGRAMMING WITH C-171

5. Program to find sum of two matrices


Solution:
#include <stdio.h>
int main() {
printf("DEVYANSHU KATARIA , 01221102021 \n");
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}

Page 74
PROGRAMMING WITH C-171

Output:

Page 75
PROGRAMMING WITH C-171

6. Program to find the maximum number in array using pointer.


Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA, 01221102021 \n");
long array[100], *maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%ld", &size);
printf("Enter %ld integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%ld", &array[c]);
maximum = array;
*maximum = *array;
for (c = 1; c < size; c++)
{
if (*(array+c) > *maximum)
{
*maximum = *(array+c);
location = c+1;
}
}
printf("Maximum element is present at location number %ld and it's value is
%ld.\n", location, *maximum);
return 0;
}

Page 76
PROGRAMMING WITH C-171

Output:

Page 77
PROGRAMMING WITH C-171

EXPERIMENT NO. 14
1. Program to enter book records

Solution:

#include<stdio.h>
struct book
{
char book_name[20];
int bookid;
float book_price;
char author[15];
};
int main()
{
struct book b[3];
int i;
for(i=0; i<3; i++)
{
printf("Enter details of book #%d\n", i+1);
printf("Enter book id: ");
scanf("%d", &b[i].bookid);
printf("Enter book name: ");
scanf("%s", b[i].book_name);
printf("Enter book author: ");

Page 78
PROGRAMMING WITH C-171

scanf("%s", b[i].author);
printf("Enter book price: ");
scanf("%f", &b[i].book_price);
}
for(i=0; i<3; i++)
{
printf("\nBook %d. ........... \n\n", i+1);
printf("Book Id: %d\n", b[i].bookid);
printf("Book Name: %s\n", b[i].book_name);
printf("Book Author: %s\n", b[i].author);
printf("Book price: %f", b[i].book_price);
}
return 0;
}
Output:

Page 79
PROGRAMMING WITH C-171

Q2.Program to enter student record(name roll no, course)

Solution:

#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("DEVYANSHU KATARIA \n");
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;

Page 80
PROGRAMMING WITH C-171

Output:

Page 81
PROGRAMMING WITH C-171

3. Program to enter book records(using union)


Solution:
#include<stdio.h>
#include<string.h>
struct Book
{
int book_id;
int book_price;
char book_name[100];
char book_author[100];
};
int main()
{
printf("DEVYANSHU KATARIA , 01221102021\n");
struct Book b1,b2;
b1.book_id=807714;
b1.book_price=450;
strcpy(b1.book_name,"C Language");
strcpy(b1.book_author,"Ravi Kumar");
b2.book_id=4547714;
b2.book_price=360;
strcpy(b2.book_name,"Python Language");
strcpy(b2.book_author,"Rohan Dass");
printf("Book1 Id:%d\n",b1.book_id);
printf("Book1 Price:%d\n",b1.book_price);
printf("Book1 Name:%s\n",b1.book_name);
printf("Book1 Auther:%s\n",b1.book_author);
printf("Book2 Id:%d\n",b2.book_id);
printf("Book2 Price:%d\n",b2.book_price);
printf("Book2 Name:%s\n",b2.book_name);
printf("Book2 Auther:%s\n",b2.book_author);
return 0;
}

Page 82
PROGRAMMING WITH C-171

Output:

Page 83
PROGRAMMING WITH C-171

EXPERIMENT NO. 15
1. Program to reverse the string.

Solution:

#include <stdio.h>

#include <string.h>

int main()

printf("DEVYANSHU KATARIA ,

01221102021\n\n");

char str[40];

printf (" \n Enter a string to be reversed: ");

scanf ("%s", str);

printf (" \n After the reverse of a string: %s ", strrev(str));

getch();

Output:

Page 84
PROGRAMMING WITH C-171

2. Program to concatenate two string

Solution:

#include <stdio.h>
int main() {
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
length = 0;
while (s1[length] != '\0') {
++length;
}
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
s1[length] = '\0';
printf("DEVYANSHU KATARIA ,
01221102021\n\n");
printf("After concatenation: "); puts(s1);
getch();
}

Output:

Page 85
PROGRAMMING WITH C-171

3. Program to copy the string

Solution:

#include<stdio.h>
#include<string.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
char str1[100],str2[50];
printf("Enter string str1\n");
gets(str1);
strcpy(str2,str1);
printf("Copied String(str2) is %s",str2);
getch();
}

Output:

Page 86
PROGRAMMING WITH C-171

4. Program to change the string in uppercase

Solution:

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
int i;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Enter your text : ");
gets(str);

for(i=0; str[i]!='\0'; i++)


{
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
}

printf("Uppercase string : %s",str);


getch();
}

OUTPUT:

Page 87
PROGRAMMING WITH C-171

5. Program to change the string in lowercase

Solution:

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
int i;
printf("DEVYANSHU KATARIA , 01221102021\n");
printf("Enter any string: ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
str[i] = str[i] + 32;
}
}

printf("Lower case string: %s", str);


getch();
}

OUTPUT:

Page 88
PROGRAMMING WITH C-171

EXPERIMENT NO. 15
1. WAP to create a file.

Solution:

#include<stdio.h>
int main()
{
FILE *fp;
char fName[20];
printf("DEVYANSHU KATARIA ,
01221102021\n");
printf("Enter file name to create :");
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
{
printf("File does not created!!!");
exit(0);
}
printf("File created successfully.");
getch();
}

OUTPUT:

Page 89
PROGRAMMING WITH C-171

2. WAP to write a content in the file

Solution:

#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
FILE *fptr;
printf("DEVYANSHU KATARIA , 01221102021\n");
fptr = fopen("fp1.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
getch();
}

OUTPUT:

Page 90
PROGRAMMING WITH C-171

3. WAP to copy the content of one file into another file.

Solution:

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("DEVYANSHU KATARIA, 01221102021 \n");
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);

fclose(fptr1);
Page 91
PROGRAMMING WITH C-171

fclose(fptr2);
getch();
}

OUTPUT:

Page 92
PROGRAMMING WITH C-171

4. WAP to display the content of file on screen

Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fPtr;
char ch;
printf("DEVYANSHU KATARIA, 01221102021 \n");
fPtr = fopen("fp1.txt", "r");
if(fPtr == NULL)
{
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
}
printf("File opened successfully. Reading file contents character by character. \n\n");
do
{
ch = fgetc(fPtr);
putchar(ch);
} while(ch != EOF);
fclose(fPtr);
getch();
}

OUTPUT:

Page 93

You might also like