0% found this document useful (0 votes)
21 views43 pages

Programming For Problem Solving Using C Lab r20 Lab Manual

The document is a lab manual for the Programming for Problem Solving using C course at the International School of Technology and Sciences for Women, prepared by Associate Professor K. Chinnanagaraju. It includes various exercises with source code and output examples for programming tasks such as printing shapes, calculating areas, and manipulating arrays. The manual is designed for first-year B.Tech students across multiple branches including CSE, AI, ML, AGR, and ECE.

Uploaded by

Gopi Tirumala
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)
21 views43 pages

Programming For Problem Solving Using C Lab r20 Lab Manual

The document is a lab manual for the Programming for Problem Solving using C course at the International School of Technology and Sciences for Women, prepared by Associate Professor K. Chinnanagaraju. It includes various exercises with source code and output examples for programming tasks such as printing shapes, calculating areas, and manipulating arrays. The manual is designed for first-year B.Tech students across multiple branches including CSE, AI, ML, AGR, and ECE.

Uploaded by

Gopi Tirumala
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/ 43

INTERNATIONAL SCHOOL OF TECHNOLOGY AND SCIENCES

(FOR WOMEN)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PROGRAMMING FOR PROBLEM SOLVING USING C

LAB MANUAL

Year : 2021 - 2022

Subject Name : Programming for Problem Solving using C

Regulations : R20

Class : I B.Tech

Branch : Common To (CSE,AI,ML,AGR,ECE)

PREPARED BY

K.CHINNANAGARAJU,

Associate Professor,

DEPARTMENT OF CSE.
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-1
1. Write a C program to print a block F using hash (#), where the F has a
height of six characters and width of five and four characters.
SOURCE CODE:
#include<stdio.h>
int main(){
printf("#####\n");
printf("#\n");
printf("#\n");
printf("####\n");
printf("#\n");
printf("#\n");
return 0;
}
OUTPUT:
#####
#
#
####
#
#
2. Write a C program to compute the perimeter and area of a rectangle
with a height of 7 inches and width of 5 inches.
SOURCE CODE:
#include<stdio.h>
int main(){
float length, width, area, perimeter;
printf("Enter the length and width of the rectangle : ");
scanf("%f%f",&length,&width);
area = length*width;
perimeter = 2*(length+width);

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

printf("Area of rectangle : %f square inches\n",area);


printf("Perimeter of rectangle : %f inches\n",perimeter);
return 0;
}
OUTPUT:
Enter the length and width of the rectangle : 7 5
Area of rectangle : 35.000000 square inches
Perimeter of rectangle : 24.000000 inches

3. Write a C program to display multiple variables.


SOURCE CODE:
#include<stdio.h>
int main(){
int i;
short int si;
long int li;
float f;
double d;
long double ld;
char c;
printf("Enter int, short int and long int values : ");
scanf("%d%hd%ld",&i,&si,&li);
printf("Enter float, double and long double values : ");
scanf("%f%lf%Lf",&f,&d,&ld);
printf("Enter a character : ");
scanf(" %c",&c);
printf("Given int value : %d\n",i);
printf("Given short int value : %hd\n",si);
printf("Given long int value : %ld\n",li);
printf("Given float value : %f\n",f);

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

printf("Given double value : %lf\n",d);


printf("Given long double value : %Lf\n",ld);
printf("Given character : %c\n",c);
return 0;
}
OUTPUT:
Enter int, short int and long int values : 12345 123 1234567
Enter float, double and long double values : 1.2 3.14 9.334
Enter a character : C
Given int value : 12345
Given short int value : 123
Given long int value : 1234567
Given float value : 1.200000
Given double value : 3.140000
Given long double value : 9.334000
Given character : C

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-2
1. Write a C program to calculate the distance between the two points.
SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main(){
float x1,x2,y1,y2,dist;
printf("Enter x1 and x2 : ");
scanf("%f%f",&x1,&x2);
printf("Enter y1 and y2 : ");
scanf("%f%f",&y1,&y2);
dist = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
printf("Distance between two points : %f\n",dist);
return 0;
}
OUTPUT:
Enter x1 and x2 : 5 9
Enter y1 and y2 : 4 8
Distance between two points : 5.656854

2. Write a C program that accepts 4 integers p, q, r, s from the user where


r and s are positive and p is even. If q is greater than r and s is greater
than p and if the sum of r and s is greater than the sum of p and q print
"Correct values", otherwise print "Wrong values".
SOURCE CODE:
#include<stdio.h>
int main(){
int p,q,r,s;
printf("Enter p,q,r,s values:");
scanf("%d%d%d%d",&p,&q,&r,&s);
if(r>0 && s>0 && p%2==0 && q>r && s>p && r+s>p+q)

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

printf("Correct values");
else
printf("Wrong values");
return 0;
}
OUTPUT-1:
Enter p,q,r,s values:4 8 2 13
Correct values
OUTPUT-2:
Enter p,q,r,s values:9 3 1 45
Wrong values

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-3
1. Write a C program to convert a string to a long integer.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
long int l;
char str[30];
printf("Enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("String to long integer is : %ld",l);
return 0;
}
OUTPUT:
Enter a string:
123456789
String to long integer is : 123456789

2. Write a program in C which is a Menu-Driven Program to compute the


area of the various geometrical shape.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
float radius, length, width, base, height, area;
int choice;
do{
printf("1. Area of circle\n2. Area of rectangle\n3. Area of triangle\n4.
Exit\n");
printf("Enter your choice : ");

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter radius : ");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("Area of circle : %f\n",area);
break;
case 2:
printf("Enter length and width : ");
scanf("%f%f",&length,&width);
area = length * width;
printf("Area of rectangle : %f\n",area);
break;
case 3:
printf("Enter base and height : ");
scanf("%f%f",&base,&height);
area = (base*height)/2;
printf("Area of triangle : %f\n",area);
break;
case 4:
exit(0);
}
} while(1);
return 0;
}
OUTPUT:
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

Enter your choice : 1


Enter radius : 4
Area of circle : 50.240002
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 2
Enter length and width : 4 8
Area of rectangle : 32.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 3
Enter base and height : 2 3
Area of triangle : 3.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 4

3. Write a C program to calculate the factorial of a given number.


SOURCE CODE:
#include<stdio.h>
int main(){
int num,fact=1,i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

fact = fact*i;
printf("Factorial of %d is %d",num,fact);
return 0;
}
OUTPUT:
Enter a number: 5
Factorial of 5 is 120

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-4
1. Write a program in C to display the n terms of even natural number and
their sum.
SOURCE CODE:
#include<stdio.h>
int main(){
int n,i=1,c=1,sum=0;
printf("Enter number of terms : ");
scanf("%d",&n);
printf("Even natural numbers are : \n");
while(c<=n){
if(i%2==0){
printf("%d ",i);
sum=sum+i;
c++;
}
i++;
}
printf("\nSum of even natural numbers is: %d\n",sum);
return 0;
}
OUTPUT:
Enter number of terms : 5
Even natural numbers are :
2 4 6 8 10
Sum of even natural numbers is: 30

2. Write a program in C to display the n terms of harmonic series and


their sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
SOURCE CODE:
#include<stdio.h>

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

int main(){
int n,i;
float sum=0;
printf("Enter range:");
scanf("%d",&n);
printf("Series is:\n");
for(i=1;i<=n;i++){
printf("1/%d",i);
if(i<n)
printf("+");
sum = sum + (float)1/i;
}
printf("\nSum of series is: %f",sum);
return 0;
}
OUTPUT:
Enter range:5
Series is:
1/1+1/2+1/3+1/4+1/5
Sum of series is: 2.283334

3. Write a C program to check whether a given number is an Armstrong


number or not.
SOURCE CODE:
/*Program to check whether the given number is Armstrong number or not.*/
#include<stdio.h>
#include<math.h>
int main(){
int n,temp,arm=0,c=0;
printf("Enter number:");
scanf("%d",&n);

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

temp=n;
while(temp>0){
temp=temp/10;
c++;
}
temp=n;
while(temp>0){
arm=arm+pow((temp%10),c);
temp=temp/10;
}
if(n==arm)
printf("Given number is Armstrong.");
else
printf("Given number is not Armstrong.");
return 0;
}
OUTPUT-1:
Enter number:371
Given number is Armstrong.

OUTPUT-2:
Enter number:151
Given number is not Armstrong.

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-5
1. Write a program in C to print all unique elements in an array.
SOURCE CODE:
#include<stdio.h>
int main(){
int a[20],n,i,j,k;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
j--;
} } }
printf("\nUnique elements in array are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter array elements:
12321

Unique elements in array are:


1 2 3

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to separate odd and even integers in separate


arrays.
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[30],evenArr[30],oddArr[30],i,j=0,k=0,ea=0,oa=0,n;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++){
if(arr[i]%2==0)
evenArr[j++]=arr[i];
else
oddArr[k++]=arr[i]; }
printf("Even elements in the array:\n");
for(i=0;i<j;i++)
printf("%d\t",evenArr[i]);
printf("\nOdd elements in the array:\n");
for(i=0;i<k;i++)
printf("%d\t",oddArr[i]);
return 0;}
OUTPUT:
Enter array size:5
Enter 5 elements:
10 25 30 35 55
Even elements in the array:
10 30
Odd elements in the array:
25 35 55

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

3. Write a program in C to sort elements of array in ascending order.


SOURCE CODE:
#include<stdio.h>
int main(){
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

OUTPUT:
Enter array size:5
Enter 5 elements:
34251
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-6
1. Write a program in C for multiplication of two square Matrices.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx1[20][20],mtrx2[20][20],mulmtrx[40][40],n1,n2,i,j,k;
printf("Enter matrix sizes:");
scanf("%d%d",&n1,&n2);
if(n1==n2){
printf("Enter first matrix elements\n");
for(i=0;i<n1;i++)
for(j=0;j<n1;j++)
scanf("%d",&mtrx1[i][j]);
printf("Enter second matrix elements\n");
for(i=0;i<n2;i++)
for(j=0;j<n2;j++)
scanf("%d",&mtrx2[i][j]);
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
mulmtrx[i][j]=0;
for(k=0;k<n1;k++)
mulmtrx[i][j]=mulmtrx[i][j]+mtrx1[i][k]*mtrx2[k][j];
}
}
printf("\nResult matrix is\n");
for(i=0;i<n1;i++){
for(j=0;j<n1;j++)
printf("%d\t",mulmtrx[i][j]);
printf("\n");
}
}

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

else
printf("Matrix multiplication is not possible");
return 0;
}
OUTPUT-1:
Enter matrix sizes:3 3
Enter first matrix elements
123
456
789
Enter second matrix elements
987
654
321

Result matrix is
30 24 18
84 69 54
138 114 90

OUTPUT-2:
Enter matrix sizes:3 2
Matrix multiplication is not possible

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to find transpose of a given matrix.


SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10],r,c,i,j,temp;
printf("Enter matrix size:\n");
scanf("%d %d",&r,&c);
printf("Enter matrix elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
if(r>=c){
for(i=0;i<c;i++){
for(j=0;j<r;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
else{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

}
}
printf("Transpose of the given matrix is\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d ",mtrx[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-1:
Enter matrix size:
23
Enter matrix elements:
123
456
Transpose of the given matrix is
14
25
36

OUTPUT-2:
Enter matrix size:
32
Enter matrix elements:
12
34
56
Transpose of the given matrix is
135
246

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-7
1. Write a program in C to search an element in a row wise and column
wise sorted matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10], r, c, i, j, ele;
printf("Enter matrix size:");
scanf("%d%d",&r,&c);
printf("Enter %d X %d matrix elements:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
printf("Enter search element:");
scanf("%d",&ele);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(mtrx[i][j]==ele)
break;
}
if(j!=c)
break;
}
if(i!=r && j!=c)
printf("%d element is found at %d-row and %d-column.\n",ele,i,j);
else
printf("%d element is not found.\n",ele);
}

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

OUTPUT:
Enter matrix size:3 3
Enter 3 X 3 matrix elements:
123
456
789
Enter search element:5
5 element is found at 1-row and 1-column.

2. Write a program in C to print individual characters of string in reverse


order.
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
int i,len;
printf("Enter string:\n");
gets(str);
len = strlen(str);
printf("String in reverse order:\n");
for(i=len-1;i>=0;i--)
printf("%c",str[i]);
return 0;
}
OUTPUT:
Enter string:
hello world
String in reverse order:
dlrow olleh

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-8
1. Write a program in C to compare two strings without using string
library functions.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[50],str2[50];
int i;
printf("Enter string1:\n");
gets(str1);
printf("Enter string2:\n");
gets(str2);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
if(str1[i]!=str2[i])
break;
if(str1[i]-str2[i]==0)
printf("Both strings are equal.");
else if(str1[i]-str2[i]>0)
printf("String1 is greater than String2.");
else
printf("String1 is lesser than String2.");
return 0;
}
OUTPUT-1:
Enter string1:
there
Enter string2:
there
Both strings are equal.

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

OUTPUT-2:
Enter string1:
their
Enter string2:
there
String1 is lesser than String2.
OUTPUT-3:
Enter string1:
there
Enter string2:
their
String1 is greater than String2.
2. Write a program in C to copy one string to another string.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[30],str2[30];
int i;
printf("Enter string:\n");
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("Copied string is:\n");
puts(str2);}
OUTPUT:
Enter string:
hello world
Copied string is:
hello world

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-9
1. Write a C Program to Store Information Using Structures with
Dynamically Memory Allocation
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct course{
char subject[30];
int marks;
};
int main(){
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records : ");
scanf("%d", &noOfRecords);
ptr = (struct course*)malloc(sizeof(struct course)*noOfRecords);
for(i=0;i<noOfRecords;i++){
printf("Enter the subject name and marks : ");
scanf("%s%d",ptr[i].subject,&ptr[i].marks);
}
printf("Subject\tMarks\n");
for(i=0;i<noOfRecords;i++){
printf("%s\t%d\n",ptr[i].subject,ptr[i].marks);
}}
OUTPUT:
Enter number of records : 2
Enter the subject name and marks : ENG 79
Enter the subject name and marks : CP 65
Subject Marks
ENG 79
CP 65

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to demonstrate how to handle the pointers in the


program.
SOURCE CODE:
#include<stdio.h>
int main(){
int a = 9, *intptr;
float pi = 3.14, *floatptr;
char ch = 'I', *charptr;
intptr = &a;
floatptr = &pi;
charptr = &ch;
printf("Size of an int ptr = %d\n",sizeof(intptr));
printf("Size of a float ptr = %d\n",sizeof(floatptr));
printf("Size of a char ptr = %d\n",sizeof(charptr));
printf("Size of an int type = %d\n",sizeof(*intptr));
printf("Size of a float type = %d\n",sizeof(*floatptr));
printf("Size of a char type = %d\n",sizeof(*charptr));
return 0;
}

OUTPUT:
Size of an int ptr = 4
Size of a float ptr = 4
Size of a char ptr = 4
Size of an int type = 4
Size of a float type = 4
Size of a char type = 1

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-10
1. Write a program in C to demonstrate the use of & (address of) and
*(value at address) operator.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, *p1;
float num2, *p2;
char ch1, *p3;
printf("Enter int, float and char values : ");
scanf("%d %f %c",&num1, &num2, &ch1 );
p1=&num1;
p2=&num2;
p3=&ch1;
printf("Given int value : %d\n", *p1);
printf("Given float value : %f\n", *p2);
printf("Given char value : %c\n", *p3);
return 0;
}
OUTPUT:
Enter int, float and char values : 97 3.24 I
Given int value : 97
Given float value : 3.240000
Given char value : I

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to add two numbers using pointers.


SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int *num1, *num2;
num1 = (int*)malloc(sizeof(int));
num2 = (int*)malloc(sizeof(int));
printf("Enter two numbers : ");
scanf("%d%d",num1,num2);
printf("Sum of %d and %d is %d\n",*num1,*num2,*num1+*num2);
return 0;
}
OUTPUT:
Enter two numbers : 5 9
Sum of 5 and 9 is 14

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-11
1. Write a program in C to add numbers using call by reference.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, sum=0;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)malloc(sizeof(int)*n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
sum = add(eles, n);
printf("Sum of all elements is : %d\n",sum);
return 0;
}
int add(int *eles, int n){
int i, sum=0;
for(i=0;i<n;i++)
sum = sum + eles[i];
return sum;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 2 6 1 5
Sum of all elements is : 14

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to find the largest element using Dynamic


Memory Allocation.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, largest;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)malloc(sizeof(int)*n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
largest = eles[0];
for(i=0;i<n;i++){
if(largest<eles[i])
largest = eles[i];
}
printf("Largest element is : %d\n",largest);
return 0;
}
OUTPUT:
Enter number of elements : 5
Enter 5 elements : 5 4 3 9 7
Largest element is : 9

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-12
1. Write a program in C to swap elements using call by reference.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, num2;
printf("Enter two integer values : ");
scanf("%d %d", &num1, &num2);
printf("Before swapping in main : num1 = %d \t num2 = %d\n", num1,
num2);
swap(&num1, &num2);
printf("After swapping in main : num1 = %d \t num2 = %d\n", num1, num2);
return 0;
}

void swap(int *num1, int *num2){


int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("After swapping in swap : *num1 = %d \t *num2 = %d\n", *num1,
*num2);
}
OUTPUT:
Enter two integer values : 5 6
Before swapping in main : num1 = 5 num2 = 6
After swapping in swap : *num1 = 6 *num2 = 5
After swapping in main : num1 = 6 num2 = 5

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to count the number of vowels and consonants in


a string using a pointer.
SOURCE CODE:
#include<stdio.h>
int main(){
char str[100], *ch;
int i, v=0, c=0;
printf("Enter a string: ");
scanf("%s",str);
ch = str;
while(*ch!='\0'){
if(*ch=='a' || *ch=='e' || *ch=='i' || *ch=='o' || *ch=='u' || *ch=='A' ||
*ch=='E' || *ch=='I' || *ch=='O' || *ch=='U'){
v++;
}
else{
c++;
}
ch++;
}
printf("Total number of vowels : %d, consonants : %d\n",v,c);
return 0;
}

OUTPUT:
Enter a string: welcometocprogramming
Total number of vowels : 7, consonants : 14

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-13
1. Write a program in C to show how a function returning pointer.
SOURCE CODE:
#include<stdio.h>
int* findLarger(int*, int*);
int main() {
int num1, num2;
int *result;
printf("Enter two numbers : ");
scanf("%d %d", &num1, &num2);
result = findLarger(&num1, &num2);
printf("Largest number : %d\n" , *result);
return 0;
}

int* findLarger(int *num1, int *num2){


return *num1>*num2?num1:num2;
}
OUTPUT:
Enter two numbers : 4 6
Largest number : 6

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a C program to find sum of n elements entered by user. To


perform this program, allocate memory dynamically using malloc( )
function.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, sum=0;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)malloc(sizeof(int)*n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
for(i=0;i<n;i++)
sum = sum + eles[i];
printf("Sum of all elements is : %d\n",sum);
return 0;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 2 6 1 5
Sum of all elements is : 14

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-14
1. Write a C program to find sum of n elements entered by user. To
perform this program, allocate memory dynamically using calloc( )
function. Understand the difference between the above two programs.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, sum=0;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)calloc(n,sizeof(int));
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
for(i=0;i<n;i++)
sum = sum + eles[i];
printf("Sum of all elements is : %d\n",sum);
return 0;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 5 4 3 2
Sum of all elements is : 14

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to convert decimal number to binary number


using the function.
SOURCE CODE:
#include <stdio.h>
long decimalToBinary(int);
int main() {
int d;
long int b;
printf("Enter a positive decimal number : ");
scanf("%d", &d);
b=decimalToBinary(d);
printf("The binary number of decimal %d is : %ld\n", d, b);
return 0;
}
long decimalToBinary(int d){
long int bn=0, revbn=0;
int c=0;
while(d>0){
revbn=(revbn*10)+(d%2);
d=d/2;
c++;
}
while(c>0){
bn=(bn*10)+(revbn%2);
revbn=revbn/10;
c--; }
return bn;}
OUTPUT:
Enter a positive decimal number : 36
The binary number of decimal 36 is : 100100

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-15
1. Write a program in C to check whether a number is a prime number or
not using the function.
SOURCE CODE:
#include<stdio.h>
void isPrime(int);
int main(){
int n;
printf("Enter number : ");
scanf("%d",&n);
isPrime(n);
return 0;
}
void isPrime(int n){
int i, c=0;
for(i=1;i<=n/2;i++){
if(n%i==0)
c++;
}
if(c==1)
printf("Given number %d is a prime number.\n",n);
else
printf("Given number %d is not a prime number.\n",n);
}
OUTPUT-1:
Enter number : 5
Given number 5 is a prime number.

OUTPUT-2:
Enter number : 25
Given number 25 is not a prime number.

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

2. Write a program in C to get the largest element of an array using the


function.
SOURCE CODE:
#include <stdio.h>
int large(int[], int);
int main() {
int a[10], i, n;
printf("Enter number of elements to be insert : ");
scanf("%d", &n);
printf("Enter %d elements : ",n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("The largest element of the array = %d\n", large(a, n));
return 0;
}
int large(int ary[], int n){
int i, max;
max=ary[0];
for(i=1;i<n;i++){
if(ary[i]>max)
max=ary[i];
}
return max;
}
OUTPUT:
Enter number of elements to be insert : 4
Enter 4 elements : 8 3 4 2
The largest element of the array = 8

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

EXERCISE-16
1. Write a program in C to append multiple lines at the end of a text file.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "a");
printf("Enter the text to append to a file press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "r");
printf("File content after appending : \n");
while ((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
Hello

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

^Z
Enter the text to append to a file press ctrl+z for end of file:
Welcome
here you learn file concepts.
^Z
File content after appending :
Hello
Welcome
here you learn file concepts.

2. Write a program in C to copy a file in another name.


SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
char ch;
fp1 = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp1);
}
fclose(fp1);
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("CopiedFile.txt", "w");
while((ch=fgetc(fp1))!=EOF){
putc(ch,fp2);
}
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
fp2 = fopen("CopiedFile.txt", "r");

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

printf("Copied text is : \n");


while((ch=fgetc(fp2))!=EOF){
putchar(ch);
}
fclose(fp2);
printf("\n");
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
welcome
learn file concepts
^Z
Copied text is :
welcome
learn file concepts

3. Write a program in C to remove a file from the disk.


SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch, fileName[30]="Sample.txt";
int status;
fp = fopen(fileName, "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen(fileName,"r");

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)
PROGRAMMING FOR PROBLEM SOLVING USING C LAB CSE (R20)

if(fp==NULL){
printf("File doesn't exists.\n");
return 0;
}
else{
printf("File exists.\n");
}
fclose(fp);
status = remove(fileName);
if(status==0)
printf("%s file is deleted.",fileName);
else
printf("Unable to delete %s file.",fileName);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
hello
^Z
File exists.
Sample.txt file is deleted.

K.CHINNANAGARAJU, Assoc. Professor, CSE DEPT. (Common to AI, ML, ECE, AGR)

You might also like