C Programming Lab Exercises for IT Students
C Programming Lab Exercises for IT Students
R20
I B TECH – I SEMESTER
EXTERNAL EXAMINER
Index
[Link] Date List of Experiments Page Remarks
No.
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.
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.
3 Write a C program to display multiple variables.
Exercise-2
4 Write a C program to calculate the distance between the two
points.
5 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".
Exercise-3
6 Write a C program to convert a string to a long integer.
7 Write a program in C which is a Menu-Driven Program to compute
the area of the various geometrical shape
8 Write a C program to calculate the factorial of a given number.
Exercise-4
9 Write a program in C to display the n terms of even natural
number and their sum.
10 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.
11 Write a C program to check whether a given number is an
Armstrong number or not.
Exercise-5
12 Write a program in C to print all unique elements in an array.
13 Write a program in C to separate odd and even integers in separate
arrays.
14 Write a program in C to sort elements of array in ascending order.
Exercise-6
16 Write a program in C for multiplication of two square Matrices.
Exercise-9
22 Write a C Program to Store Information Using Structures
with Dynamically Memory Allocation
23 Write a program in C to demonstrate how to handle the pointers in
the program.
Exercise-10
24 Write a program in C to demonstrate the use of & (address
of) and *(value at address) operator.
25 Write a program in C to add two numbers using pointers.
Exercise-11
26 Write a program in C to add numbers using call by reference
27 Write a program in C to find the largest element using Dynamic
Memory Allocation.
Exercise-12
28 Write a program in C to swap elements using call by reference.
29 Write a program in C to count the number of vowels and
consonants in a string using a pointer.
Exercise-13
30 Write a program in C to show how a function returning pointer
31 Write a C program to find sum of n elements entered by user. To
perform this program, allocate memory dynamically using
malloc( ) function
Exercise-14
32 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
33 Write a program in C to convert decimal number to binary number
using the function.
Exercise - 15
34 Write a program in C to check whether a number is a prime
number or not using the function.
35 Write a program in C to get the largest element of an array using
the function.
Exercise - 16
36 Write a program in C to append multiple lines at the end of a text
file.
Write a program in C to copy a file in another name
37
Write a program in C to remove a file from the disk.
Additional programs
38 Write a c program to illustrating factorial with recursion without
recursion
Exercise- 1
1. A
Aim: 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.
#include <stdio.h>
int main()
{
printf("######\n");
printf("#\n");
printf("#\n");
printf("#####\n");
printf("#\n");
printf("#\n");
printf("#\n");
return(0);
}
VIEW Page 1
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
######
#
#
#####
#
#
#
VIEW Page 2
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
1. B
Aim: 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>
/* height and width of a rectangle in inches */
int width;
int height;
int area;
int perimeter;
int main() {
height = 7;
width = 5;
return(0);
}
Output:
VIEW Page 3
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
1. C
Aim: Write a C program to display multiple variables.
Variable declaration:
Source code:
#include <stdio.h>
int main()
{
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;
return 0;
}
VIEW Page 4
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
a + c = 212
x + c = 89.134590
dx + x = 3.276183
((int) dx) + ax = 1234567891
a + x = 127.134590
s + b = 16388
ax + b = 1234580235
s + c = 4130
ax + c = 1234567977
ax + ux = 3776135780
VIEW Page 5
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise- 2
2.a
Aim: Write a C program to calculate the distance between the two points.
Formula:
Source code:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, gdistance;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(gdistance));
printf("\n");
return 0;
}
VIEW Page 6
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Distance between the said points: 11.1803
VIEW Page 7
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
2. B
Aim: 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 “wrong values”.
Source code:
#include <stdio.h>
int main()
{
int p, q, r, s;
printf("\nInput the first integer: ");
scanf("%d", &p);
printf("\nInput the second integer: ");
scanf("%d", &q);
printf("\nInput the third integer: ");
scanf("%d", &r);
printf("\nInput the fourth integer: ");
scanf("%d", &s);
if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2==0))
{
printf("\nCorrect values\n");
}
else {
printf("\nWrong values\n");
}
return 0;
}
VIEW Page 8
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Wrong values
VIEW Page 9
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise - 3
3. A
i1 = strtol (buffer,&ptr_end,10);
i2 = strtol (ptr_end,&ptr_end,16);
i3 = strtol (ptr_end,&ptr_end,2);
i4 = strtol (ptr_end,NULL,0);
printf ("\nIn decimals: %ld, %ld, %ld, %ld.\n\n", i1, i2, i3, i4);
return 0;
}
Output:
VIEW Page 10
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
3. B
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %f\n",area);
}
VIEW Page 11
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 12
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
3. C
Source code:
#include <stdio.h>
void main(){
int i,f=1,num;
for(i=1;i<=num;i++)
f=f*i;
VIEW Page 13
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 14
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 4
4. A
Aim: Write a program in C to display the n terms of even natural number and their sum.
Source code:
#include <stdio.h>
void main()
{
int i,n,sum=0;
VIEW Page 15
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 16
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
4. B
Aim: 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>
void main()
{
int i,n;
float s=0.0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\n\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
}
VIEW Page 17
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 18
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
4. C
Aim: Write a C program to check whether a given number is an Armstrong number or not.
Source Code:
#include <stdio.h>
void main(){
int num,r,sum=0,temp;
scanf("%d",&num);
for(temp=num;num!=0;num=num/10){
r=num % 10;
sum=sum+(r*r*r);
if(sum==temp)
else
VIEW Page 19
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 20
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 5
5.1
Source code:
#include <stdio.h>
void main()
{
int arr1[100], n,ctr=0;
int i, j, k;
VIEW Page 21
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
}
VIEW Page 22
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Element - 0 : 1
Element - 1 : 5
Element - 2 : 1
VIEW Page 23
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
5. B
Aim: Write a program in C to separate odd and even integers in separate arrays.
Source Code:
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
VIEW Page 24
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
VIEW Page 25
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
42 56 32
25 47
VIEW Page 26
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
5. C
Source Code:
#include <stdio.h>
void main()
{
int arr1[100];
int n, i, j, tmp;
VIEW Page 27
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9
24579
VIEW Page 28
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 6
6. A
Source code:
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
VIEW Page 29
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}
VIEW Page 30
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Multiplication of two Matrices :
element - [1],[1] : 8
The First matrix is :
1 2
2 4
VIEW Page 31
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
6.b
Aim: Write a program in C to find transpose of a given matrix.
Source code:
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],i,j,k=0,r,c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
}
}
VIEW Page 32
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
for(j=0;j<r;j++){
printf("%d\t",brr1[i][j]);
}
}
printf("\n\n");
}
VIEW Page 33
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Transpose of a Matrix :
The matrix is :
1 2
2 4
The transpose of a matrix is :
1 3
2 4
VIEW Page 34
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 7
7. A
Aim: Write a program in C to search an element in a row wise and column wise sorted
matrix.
Source code:
#include <stdio.h>
int i = 0, j = n-1;
if ( arr2D[i][j] == x )
printf("\nThe element Found at the position in the matrix is: %d, %d", i, j);
return 1;
if ( arr2D[i][j] < x )
j--;
else
i++;
return 0;
int main()
VIEW Page 35
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
};
int i,j,v;
v=37;
for (j=0;j<4;j++)
printf("\n");
//
searchElement(arr2D, 4, v);
return 0;
VIEW Page 36
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
The given array in matrix form is :
15 23 31 39
18 26 36 43
25 28 37 48
30 34 39 50
VIEW Page 37
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
7. B
Aim: Write a program in C to print individual characters of string in reverse order.
Source Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
int l,i;
printf(" \n");
l=strlen(str);
for(i=l;i>=0;i--)
printf("\n");
VIEW Page 38
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 39
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 8
8. A
Aim: Write a program in C to compare two strings without using string library functions.
Source code:
#include<stdio.h>
int main() {
char str1[30], str2[30];
int i;
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}
VIEW Page 40
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Enter two strings: NSRIT
CSE Department
Str1 > Str2
VIEW Page 41
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
8. B
Aim: Write a program in C to copy one string to another string.
Source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
VIEW Page 42
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 43
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercises - 9
9. A
Source code:
#include <stdio.h>
#include<stdlib.h>
struct course
{
int marks;
char subject[30];
};
int main()
{
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records: ");
scanf("%d", &noOfRecords);
// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base
address.
ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));
printf("Displaying Information:\n");
return 0;
}
VIEW Page 44
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
33
Displaying Information:
Programming 22
Structure 33
VIEW Page 45
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
9. B
Aim: Write a program in C to demonstrate how to handle the pointers in the program.
Source code:
#include <stdio.h>
int main()
{
int* ab;
int m;
m=29;
printf("\n\n Pointer : How to handle the pointers in the program :\n");
printf(" \n");
printf(" Here in the declaration ab = int pointer, int m= 29\n\n");
VIEW Page 46
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Value of m : 29
Content of pointer ab : 29
The value of m assigned to 34 now.
Address of pointer ab : 0x7fff24a3f8bc
Content of pointer ab : 34
Address of m : 0x7fff24a3f8bc
Value of m : 7
VIEW Page 47
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise - 10
10. A
Aim: Write a program in C to demonstrate the use of & (address of) and *(value at
address) operator
Source code:
#include <stdio.h>
void main()
{
int m=300;
float fx = 300.60;
char cht = 'z';
int *pt1;
float *pt2;
char *pt3;
pt1= &m;
pt2=&fx;
pt3=&cht;
printf ( " m = %d\n",m);
printf ( " fx = %f\n",fx);
printf ( " cht = %c\n",cht);
printf("\n Using & operator :\n");
printf(" \n");
printf ( " address of m = %p\n",&m);
printf ( " address of fx = %p\n",&fx);
printf ( " address of cht = %p\n",&cht);
printf("\n Using & and * operator :\n");
printf(" \n");
printf ( " value at address of m = %d\n",*(&m));
printf ( " value at address of fx = %f\n",*(&fx));
printf ( " value at address of cht = %c\n",*(&cht));
printf("\n Using only pointer variable :\n");
printf(" \n");
printf ( " address of m = %p\n",pt1);
printf ( " address of fx = %p\n",pt2);
printf ( " address of cht = %p\n",pt3);
printf("\n Using only pointer operator :\n");
printf(" \n");
printf ( " value at address of m = %d\n",*pt1);
printf ( " value at address of fx= %f\n",*pt2);
printf ( " value at address of cht= %c\n\n",*pt3);
VIEW Page 48
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Pointer: Demonstrate the use of & and * operator:
m = 300
fx = 300.600006
cht = z
address of m = 0x7fff71cd0b38
address of fx = 0x7fff71cd0b3c
address of cht = 0x7fff71cd0b37
address of m = 0x7fff71cd0b38
address of fx = 0x7fff71cd0b3c
VIEW Page 49
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
10. B
Source code:
#include <stdio.h>
int main()
p = &first;
q = &second;
sum = *p + *q;
return 0;
VIEW Page 50
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 51
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 11
11. A
Source Code:
#include <stdio.h>
long addTwoNumbers(long *, long *);
int main()
{
long fno, sno, *ptr, *qtr, sum;
VIEW Page 52
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 53
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
11. B
Aim: 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 i,n;
float *element;
printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation
:\n");
printf(" \n");
scanf("%d",&n);
if(element==NULL)
exit(0);
printf("\n");
for(i=0;i<n;++i)
scanf("%f",element+i);
for(i=1;i<n;++i)
VIEW Page 54
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
if(*element<*(element+i))
*element=*(element+i);
return 0;
VIEW Page 55
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:]
VIEW Page 56
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise – 12
12. A
Aim: Write a program in C to swap elements using call by reference.
Source code:
#include <stdio.h>
void swapNumbers(int *x,int *y,int *z);
int main()
{
int e1,e2,e3;
printf("\n\n Pointer : Swap elements using call by reference :\n");
printf(" \n");
printf(" Input the value of 1st element : ");
scanf("%d",&e1);
printf(" Input the value of 2nd element : ");
scanf("%d",&e2);
printf(" Input the value of 3rd element : ");
scanf("%d",&e3);
VIEW Page 57
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 58
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
12. B
Aim: 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];
char *p;
int vCount=0,cCount=0;
VIEW Page 59
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 60
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise -13
13. A
Source Code:
#include <stdio.h>
int* findLarger(int*, int*);
void main()
{
int numa=0;
int numb=0;
int *result;
printf("\n\n Pointer : Show a function returning pointer :\n");
printf(" \n");
printf(" Input the first number : ");
scanf("%d", &numa);
printf(" Input the second number : ");
scanf("%d", &numb);
result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
}
VIEW Page 61
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 62
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
13. B
Aim: 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 #include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
return 0;
}
}
VIEW Page 63
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Enter size: 2
Addresses of previously allocated memory:26855472
26855476
VIEW Page 64
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise - 14
14. A
Aim: 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, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
VIEW Page 65
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Enter size: 2
Addresses of previously allocated memory:26855472
26855476
VIEW Page 66
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
14. B
Aim: Write a program in C to convert decimal number to binary number using the function.
Source code:
#include<stdio.h>
long toBin(int);
int main()
{
long bno;
int dno;
printf("\n\n Function : convert decimal to binary :\n");
printf(" \n");
printf(" Input any decimal number : ");
scanf("%d",&dno);
bno = toBin(dno);
printf("\n The Binary value is : %ld\n\n",bno);
return 0;
}
long toBin(int dno)
{
long bno=0,remainder,f=1;
while(dno != 0)
{
remainder = dno % 2;
bno = bno + remainder * f;
f = f * 10;
dno = dno / 2;
}
return bno;
}
VIEW Page 67
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 68
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise - 15
15. A
Aim: Write a program in C to check whether a number is a prime number or not using
the function.
Source code:
#include<stdio.h>
int PrimeOrNot(int);
int main()
{
int n1,prime;
printf("\n\n Function : check whether a number is prime number or not :\n");
printf(" \n");
VIEW Page 69
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 70
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
15. B
Aim: Write a program in C to get the largest element of an array using the function.
Source code:
#include<stdio.h>
#define MAX 100
int main()
{
int arr1[MAX],mxelem,i;
printf("\n\n Function : get largest element of an array :\n");
printf(" \n");
VIEW Page 71
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 72
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Exercise - 16
16. A
Aim: Write a program in C to append multiple lines at the end of a text file.
Source code:
#include <stdio.h>
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20];
char str1;
VIEW Page 73
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 74
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
16. B
Source code:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
VIEW Page 75
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 76
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
16. C
Source code:
#include <stdio.h>
void main()
{
int status;
char fname[20];
printf("\n\n Remove a file from the disk :\n");
printf(" \n");
printf(" Input the name of file to delete : ");
scanf("%s",fname);
status=remove(fname);
if(status==0)
{
printf(" The file %s is deleted successfully..!!\n\n",fname);
}
else
{
printf(" Unable to delete file %s\n\n",fname);
}
}
VIEW Page 77
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
VIEW Page 78
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Additional programs:
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, ch;
clrscr();
printf("\n Enter the first number:");
scanf("%d", &n1);
printf("\n Enter the second number:");
scanf("%d", &n2);
printf("\n [1] -> Addition ");
printf("\n [2] -> Subtraction ");
printf("\n [3] -> Multiplication ");
printf("\n [4] -> Division ");
printf("\n\n Enter your choice <1...4>:");
scanf("%d", &ch);
switch(ch)
{
case 1 :
printf("\n %d + %d = %d", n1, n2, n1 + n2) ;
break ;
case 2 :
printf("\n %d - %d = %d", n1, n2, n1 - n2) ;
break ;
case 3 :
printf("\n %d * %d = %d", n1, n2, n1 * n2);
break ;
VIEW Page 79
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
case 4 :
printf("\n %d / %d = %.2f", n1, n2, (float)n1 / n2);
break ;
default :
printf("\n Invalid choice");
break ;
}
getch();
}
VIEW Page 80
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Enter the first number: 3
Enter the second number: 6
[1] Addition
[2] Subtraction
[3] Multiplication
[4] Division
Enter your choice <1…4> : 1
3+6 = 9
VIEW Page 81
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
2.
Aim: Write a c program to make a single calculator to add, subtract, multiply or divide
using switch case
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
int recfib(int);
void nonrecfib(int);
clrscr();
printf("\n Enter the numbers:");
scanf("%d",&n);
printf("\n The Fibonacci series using recursion is:");
for(i=0;i<n;i++)
printf("%d\t",recfib(i));
printf("\n The Fibonacci series without using recursion is:");
nonrecfib(n);
}
int recfib(int x)
{
if(x==0)
return 0;
else if(x==1)
return 1;
else
return recfib(x-1)+recfib(x-2);
}
void nonrecfib(int c)
{
int a=0,b=1,d,i;
VIEW Page 82
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
printf("\n%d\t%d",a,b);
for(i=3;i<=c;i++)
{
d=a+b;
printf("\t%d",d);
a=b;
b=d;
}
}
VIEW Page 83
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
Output:
Enter the numbers: 8
The Fibonacci series using recursion is:
0 1 1 2 3 5 8 13
The Fibonacci series without using recursion is:
0 1 1 2 3 5 8 13
VIEW Page 84
ROLL No: PROGRAMMING FOR PROBLEM SOLVING USING C
VIEW Page 85