0% found this document useful (0 votes)
6 views

CS Programming in C Record

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS Programming in C Record

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Programming

in C
(C Lab)

By
SRI SNEHA DEGREE COLLEGE
B.SC-Computer Science 1st Semester (CBCS)
Ravi. M MCA, M.Tech(CSE), [Ph.D]
Programming in C [C Lab] B.SC- Computer Science 1st Semester
PROBLEM- NO:1
Write a program to find the largest of two (three) numbers using if and
conditional operator.
PSEUDOCODE

Solution:
Pseudo code:
1. Input three numbers A, B and C.
2. If A is greater than B and A is greater than C
Print “A”is the largest number
else
3. If B is greater than A and B is greater than C
Print “B”is the largest number
else
Print “C”is the largest number.

FLOW CHART

1
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
C PROGRAM:

// By using if statement
/* This Program will find the largest of three integer numbers by using
‘if’ statement.*/
#include <stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a>b)
{
if(a>c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else
{
if(b>c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.",c);
}
getche();
return 0;
}

2
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
// By using Conditional Operator
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,big;
clrscr();
printf("Enter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is:%d",big);
getche();
return 0;
}
I/O
Enter three numbers: 10 40 20
40 is the largest number.

PROBLEM- NO:2
Write a program to print the reverse of a given number. (Palindrome).
PSEUDOCODE

Solution:
1.Declare an integer variable called N
2.Declare an integer variable R
3.Declare an integer variable Rev
4.Declare an integer variable X
5. Set Reverse to Zero
6. Input a Number, N.
7. Set X to N.
8. Repeat until N is not equal to Zero
a. R = N % 10;
b. Rev = R + (Rev * 10)
c. N = N / 10;
end loop
9. Print Reverse.
10. If Rev is equal to X
print "X is a Palindrome Number".
else
print "X is not a Palindrome".

3
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
FLOW CHART:

4
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
C PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int n,r,rev=0,x;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
x=n;
while(n)
{
r=n%10;
rev=r+(rev*10);
n=n/10;
}
printf("\n Reverse of the number %d is = %d", x, rev);

if( rev == x)
printf("%d is a palindrom number ",x);
else
printf("%d is Not a palindrom number ",x);
getche();
return 0;
}
I/O:
(1)
Enter a Number: 12
Reverse of the Number 12 = 21
12 is not a Palindrome number
(2)
Enter a Number: 121
Reverse of the Number 121 = 121
121 is a Palindrome number

5
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
PROBLEM- NO:3

Write a program to print the Prime numbers for 2 to n where n is given


by the user.

PSEUDOCODE

1. Declare an integer variable called N


2. Declare an integer variable i
3. Declare an integer variable Range
4. Declare an integer variable Count
5. Input the range of the Prime numbers, Range.
6. Set N to 2
7. Repeat until n is lessthan or equal to Range
Set Count to 0
Set i to 1
for loopcounter = 0 to (size of arr)-1
if N % i is equal to 0
increment Count by 1
increment i by 1
endfor
if Count is equal to 2
Print "N" is Prime.
8. end loop

6
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
FLOW CHART:

7
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
C PROGRAM:

#include <stdio.h>
#include<conio.h>
main()
{
int n, i, range, count;
printf("Enter the range of the Prime Numbers: ");
scanf("%d",&range);
printf("\n Prime Numbers from 2 to %d", range);
for(n=2;n<=range;n++)
{
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if (count==2)
printf("\n%d ",n);
}
getche();
return 0;
}

I/O
Enter the range of Prime Numbers: 10
Prime Numbers from 2 to 10:
2 3 5 7

8
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:4

Write a program to find the roots of a quadratic equation using switch


statement.

PSEUDOCODE

1.Declare an integer variable called A


2.Declare an integer variable B
3.Declare an integer variable C
4.Declare a float variable called Root1
5.Declare a float variable called Root2
6.Declare a float variable called Root3
7.Declare a float variable called Discriminant
8. input A,B,C
9. Set Discriminant = b^2 - 4*a*c
10. Switch Discriminant >0
a. case 1:
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
print "Two distinct and real roots exists"
break;
b. case 0:
switch discriminant < 0
case 1:
root1 = root2 = -b / (2*a);
imaginary = sqrt(-discriminant) / (2*a);
print "Two distinct complex roots exists"
break;
case 0:
root1 = root2 = -b / (2*a);
print "Two equal and real roots exists"
break;

9
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
FLOW CHART:

C PROGRAM:

#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
clrscr();
printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);
discriminant = (b*b) - (4*a*c);

10
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
switch(discriminant > 0)
{
case 1:
//If discriminant is positive
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Two distinct and real roots exists: %.2f and %.2f\n", root1, root2);
break;

case 0:
switch(discriminant < 0)
{
case 1:
//If discriminant is negative
root1 = root2 = -b / (2*a);
imaginary = sqrt(-discriminant) / (2*a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f\n",
root1, imaginary, root2, imaginary);
break;
case 0:
//If discriminant is zero
root1 = root2 = -b / (2*a);
printf("Two equal and real roots exists: %.2f and %.2f\n", root1, root2);
break;
}
}
getche();
return 0;
}

I/O
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10
Two distinct and real roots exists: 1.85 and -1.35

11
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:5

Write a program to print a triangle of stars as follows (take number of


lines from user):

PSEUDOCODE

1. Declare integer variables i, j, k stars, lines.


2. Input the Number of Lines required
3. Set stars to 6
4. Set i to 1
5. Set j to 1
6. Set k to 1
7. Repeat
for i = 1 to lines
for j=1 to stars
print “ “
j = j+1
endfor
for k= 1 to i
print “*”
print “ “
k = k+1
endfor
print New Line
stars = stars-1
i = i+1
endfor
end loop.

12
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
FLOW CHART:

13
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
C PROGRAM:

#include <stdio.h>
#include<conio.h>
int main()
{
int lines, i,j,k, stars=6;
clrscr();
printf("Enter the number of lines for the triangle:");
scanf("%d",&lines);

for (i=1; i<= lines; i++)


{
for (j=1; j<= stars; j++)
{
printf(" ");
}
for (k=1; k<= i; k++)
{
printf("*");
printf(" ");
}
printf("\n");
stars--;
}
getche();
return 0;
}

I/O
Enter the number of lines for the Triangle: 6
*
* *
* * *
* * * *
* * * * *
* * * * * *

14
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
PROBLEM- NO:6

Write a program to find largest and smallest elements in a given list of


numbers.

PSEUDOCODE

1. Declare an integer variable ARR


2 .Declare an integer variable I
3. Declare an Integer N
4. Declare an Integer SMALL
5. Declare an Integer LARGE
6. Set ARR to size 10
for i = 0 to (size of ARR)-1
Input N
Set ARR[I] = N
i=i+1
endfor
7. Set LARGE = SMALL = ARR[0]
8. for i = 1 to (size of ARR)-1
if ARR[i] > LARGE
LARGE = ARR[i]
if ARR[i] < SMALL
SMALL = ARR[i]
i=i+1
endfor
8. Print "LARGEST"
9. Print "SMALLEST"

15
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

FLOW CHART:

16
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

C PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[50], i, n, large, small;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n; i++)
scanf("%d",&a[i]);

large=small=a[0];
for(i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}

printf("\nThe largest element is %d",large);


printf("\nThe smallest element is %d",small);

getche();
return 0;
}
I/O
Enter the No. Of elements:5
Enter 5 Elements: 10 40 5 85 63
Largest Element is 85
Smallest Element is 5

17
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:7

Write a program to find the product of two matrices.

PSEUDOCODE

1.Declare an integer variable A


2.Declare an integer variable B
3.Declare an integer variable C
4.Declare an integer variable M
5.Declare an integer variable I
6.Declare an Integer variable J
7.Declare an integer variable K
8.Declare an integer variable ROW1
9.Declare an integer variable ROW2
10. Declare an integer variable COL1
11. Declare an integer variable COL2
Set A to size 10 , 10
Set B to size 10,10
Set C to size 10,10
Input the size of Matrix-1 to ROW1 and COL1
for I = 0 to (ROW1)-1
for J = 0 to (COL1)-1
Input A[i][j]
I=I+1
end for
j = j+1
endfor

Input the size of Matrix-2 to ROW2 and COL2


for I = 0 to (ROW2)-1
for J = 0 to (COL2)-1
Input B[i][j]
I=I+1
end for
j = j+1
endfor

Print Matrix1:
for I = 0 to (ROW1)-1
for J = 0 to (COL1)-1
Print A[i][j]
I=I+1
end for

18
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
j = j+1
endfor
Print Matrix2:
for I = 0 to (ROW2)-1
for J = 0 to (COL2)-1
Print B[i][j]
I=I+1
end for
j = j+1
endfor
if col1 is equal to Row2
print "Matrix Multiplication is Possible"
for I = 0 to (ROW1)-1
for J = 0 to (COL2)-1
Set M[i][j] to 0
for K = 0 to (COL1)-1
M[i][j] = M[i][j]+ A[i][k] * B[k][j]
K = K+1
endfor
I=I+1
end for
j = j+1
endfor
Print Matrix Multiplication:
for I = 0 to (ROW1)-1
for J = 0 to (COL2)-1
Print M[i][j]
I=I+1
end for
j = j+1
endfor
else
print "Matrix Multiplication i Not Possible".

19
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

FLOWCHART:

C PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int row1, col1;
int row2, col2;
int i,j,k;
int a[10][10];
int b[10][10];
int m[10][10];
clrscr();

20
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
printf("\n Enter the size of matrix->1:");
scanf("%d %d", &row1, &col1);
printf("\n Enter Elements for matrix-> 1 \n");

for(i = 0 ; i< row1; i++)


{
for(j = 0 ; j<col1; j++)
{
printf("Enter an Elemennt:");
scanf("%d", &a[i][j]);
}
}

printf("\n Enter the size of matrix->2:");


scanf("%d %d", &row2, &col2);
printf("\n Enter Elements for matrix-> 2\n");
for(i = 0 ; i< row2; i++)
{
for(j = 0 ; j<col2; j++)
{
printf("Enter an Elemennt:");
scanf("%d", &b[i][j]);
}
}

printf("\n Matrix a is:\n");


for(i = 0; i < row1; i++)
{
for(j = 0; j < col1; j++)
printf("%3d", a[i][j]);
printf("\n");
}

printf("\n Matrix b is: \n");


for(i = 0; i < row2; i++)
{
for(j = 0; j < col2; j++)
printf("%3d", b[i][j]);
printf("\n");
}

if(col1 == row2)
{
printf("\n Multiplication is possible and the Result is as follows \n");
for(i=0; i<row1; i++)
for(j=0; j<col2; j++)

21
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
{
m[i][j] = 0;
for(k = 0; k < col1; k++)
m[i][j] += a[i][k] * b[k][j];
}
printf("\n Matrix Multiplication is: \n");
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
printf("%3d", m[i][j]);
printf("\n");
}
}

else
printf("\n Multiplication is not possible");
getche();
return 0;
}

I/O
Enter the size of Matrix 1: 3 3
Enter elements for Matrix-1:
Enter an Element: 1
Enter an Element: 1
Enter an Element: 1
Enter an Element: 2
Enter an Element: 2
Enter an Element: 2
Enter an Element: 3
Enter an Element: 3
Enter an Element: 3

Enter the size of Matrix 2: 3 3


Enter elements for Matrix-2:
Enter an Element: 1
Enter an Element: 1
Enter an Element: 1
Enter an Element: 2
Enter an Element: 2
Enter an Element: 2
Enter an Element: 3
Enter an Element: 3
Enter an Element: 3

22
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

Matrix-A is:
1 1 1
2 2 2
3 3 3

Matrix-B is:
1 1 1
2 2 2
3 3 3

Matrix Multiplication is:


6 6 6
12 12 12
18 18 18

PROBLEM- NO:8

Write a program to find the GCD of two numbers using iteration and
recursion.
PSEUDOCODE

1. Declare an integer variable A


2. Declare an integer variable B
3. Declare an integer variable GCD
4. Input Two Numbers A,B
5. GCD<-- Call GCD(A,B)
Print GCD

6. Function GCD(A,B)
7. If b is not equal to 0
return<--Call GCD (B, A%B)
else
return<-- a
end function

23
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
FLOW CHART:

C PROGRAM:

#include <stdio.h>
#include<conio.h>
int gcd(int a, int b);
int main()
{
int a, b;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("G.C.D of %d and %d is %d.", a, b, gcd(a,b));
getche();
return 0;
}

24
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

int gcd(int a, int b)


{
if (b != 0)
return gcd(b, a%b);
else
return a;
}

I/O
Enter two integers: 100 10
G.C.D of 100 and 10 is 10
.
PROBLEM- NO:9

Write a program to illustrate use of storage classes.

PSEUDOCODE

1. Declare an external variable X and set it to 5


2. Declare an external variable Y and set it to 10
3. Declare an integer variable A and set it to 100;
4. Declare a register variable B and set it to 1000;
5. Print A
6. Print X
7. Print Y
8. Set X to 10
9. Set Y to 50
10. Print Modified Value of X
11. Print Modified Value of Y
12. Repeat until X is graterthan 0
13. Declare Static integer variable s and set it to 0
Increment S by 1
print S
Decrement X by 1
End loop.
25
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

C PROGRAM:

#include <stdio.h>
extern int x =15;
int y = 10;
int main()
{
auto int a = 100; // local variable
register int b = 1000; // kept in CPU registers
clrscr();
printf("\nLocal variable is:%d\n", a);
printf("\nExternal variables are: %d and %d", x,y);
printf("\nRegister variable is : %d",b);
x = 5; // refers the external variable
y = 50;// refers the external variable
printf("\n Modified values of the extern variables: %d and %d",x,y);
while (x > 0)
{
static int s = 0; // static local variable
s++;
printf("\nThe value of s is %d",s);
x--;
}
getche();
return 0;
}

I/O
Local variable is:100
External Variable is: 15 , 10
Register Variable is: 1000
Modified External variables:5 , 50
The Value of s is:1
The Value of s is:2
The Value of s is:3
The Value of s is:4
The Value of s is: 5

26
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
PROBLEM- NO:10

Write a program to demonstrate the call by value and the call by reference
concepts.

PSEUDOCODE
Declare an integer variable A
Declare an integer variable B
Print "Before Swaping"
print A
Print B
call-->swap(A,B)

Print "After Swaping-Call by Value"


print A
Print B
call-->swap1(&A,&B)
Print "After Swaping-Call by Reference"
print A
Print B

function swap(X,Y)
Declare TEMP
TEMP =X
X=Y
Y=TEMP
end function

function swap1(*X,*Y)
Declare TEMP
TEMP =*X
*X=*Y
*Y=TEMP
end function

C PROGRAM:

#include<stdio.h>
void swap(int x,int y);
void swap1(int *x,int *y);
int main()
{
int a=50, b=100;
clrscr();

27
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
printf("\n Before swaping:");
printf("\n a : %d",a);
printf("\n b : %d",b);

swap(a,b);
printf("\n After swaping- Call by Value Method:");
printf("\n a : %d",a);
printf("\n b : %d",b);

swap1(&a,&b);
printf("\nAfter swaping- Call by Ref Method:");
printf("\n a : %d",a);
printf("\n b : %d",b);

getche();
return(0);
}

void swap(int x, int y)


{
int temp;
temp = x;
x = y;
y = temp;
}

void swap1(int *x, int *y)


{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

I/O
Before Swaping a =50 , b=100
After Swaping- Call by Value Method: a=50, b=100
After Swaping-Call by Ref Method: a=100 , b =50

28
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
PROBLEM- NO:11

Write a program that prints a table indicating the number of occurrences


of each alphabet in the text entered as command line arguments.

C PROGRAM:

//nalphas.c
#include<stdio.h>
#include<conio.h>
#include<string.h>

int main(int argc, char *argv[])


{
char a[100];
int i,j,l,flag,ch;
clrscr();
fflush(stdin);
printf("\n\n Command-Line Argument\n");
for(i=0;i<argc;i++)
{
printf("\n %c \n",argv[i]);
strcat(a,argv[i]);
}

l=strlen(a);

for(i=97;i<=122;i++)
{
flag=0;
for(j=0;j<l;j++)
{
ch=(int)a[j];
if(i==ch)
flag++;
}
printf("\t %c ---%d ",i,flag);
flag=0;
}
printf(" \n ");

29
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
for(i=65;i<=90;i++);
{
flag=0;
for(j=0;j<l;j++)
{
ch=(int)a[j];
if(i==ch)
flag++;
}
printf("\t %c---%d ",i,flag);
flag=0;
}
getch();
return 0;
}

I/O:
C:\TURBOC3\SOURCE\nalphas ravi
a---1
b---0
c---0
d---0
e---0
f---0
g---0
h---0
i---1
j---0
k---0
l---0
m---0
n---0
o---0
p---0
q---0
r---1
s---0
t---0
u---0
v---1
w---0
x---0
y---0
z---0

30
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:12

Write a program to illustrate use of data type enum.

C PROGRAM:

#include <stdio.h>
enum week { sunday, monday, tuesday, wednesday, thursday, friday,
saturday };
int main()
{
enum week today;
today = monday;
printf("Day = %d",today+1);
getche();
return 0;
}

I/O
Day = 2

PROBLEM- NO:13

Write a program to demonstrate use of string functions string.h header


file.

C PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Sri";
char s2[20] = " Sneha";
char s3[40];
printf("Initially Length of string1 is : %d", strlen(s1);
printf("Length of string2 is : %d", strlen(s2);

31
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
strcat(s1,s2);
strcpy(s3,s1);
printf("Modified String s1 is: %s", s1);
printf("String s2 is: %s", s2);
printf("String s3 is: %s", s3);
getche();
retrn 0;
}
I/O:
Initially Length of string1 is: 3
Length of string1 is: 5
String1 nd String2 are different
Modified String1 is : Sri Sneha
String2 is : Sri
String3 is : Sri Sneha

32
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:14

Write a program that opens a file and counts the number of characters in
the file.

C PROGRAM:
#include <stdio.h>
int main()
{
FILE *fp;
char filename[20];
char ch;
int count=0;
clrscr();
printf("Enter a filename :");
gets(filename);

fp = fopen(filename,"r");
if ( fp )
{
while ((ch=getc(fp)) != EOF)
{
if (ch != ' ' && ch != '\n')
{
count++;
}
}
}
else
{
printf("File Cannot be Opened\n");
}

printf("No.of Characters in the file : %d \n", count);

getche();
return(0);
}

I/O
Enter a File Name: Test.txt
No of Characters in the file: 32

33
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

PROBLEM- NO:15

Write a program to create a structure Student containing fields for Roll


No., Name, Class, Year and Total Marks. Create 10 students and store
them in a file.

C PROGRAM:

#include <stdio.h>
struct student
{
char name[50], class[20];
int roll;
int total;
} s[10];

int main()
{
int i;
clrscr();
printf("Enter 10 Students information:\n");
for(i=0;i<10;i++)
{
printf("Enter name: ");
scanf("%s", s[i].name);

printf("Enter class name: ");


scanf("%s", s[i].class);

printf("Enter roll number: ");


scanf("%d", &s[i].roll);

printf("Enter marks: ");


scanf("%d", &s[i].total);
}

34
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
printf("Student Information:\n");

printf("\nRoll.No \t Name \t Class \t Total Marks: \n");

for(i=0;i<10;i++)
{
printf("\n%d \t %s \t %s \t %d \n", s[i].roll, s[i].name, s[i].class, s[i].total);

getche();
return 0;
}

I/O:
Enter 10 Students Information:
Enter Name: Arun
Enter Class Name: B.SC
Enter Roll.Number: 1
Enter Marks: 610

Enter Name: Bharathi


Enter Class Name: B.SC
Enter Roll.Number: 2
Enter Marks: 642

Enter Name: Charitha


Enter Class Name: B.SC
Enter Roll.Number: 3
Enter Marks: 600

Enter Name: Dhavan


Enter Class Name: B.SC
Enter Roll.Number: 4
Enter Marks: 582

35
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

Enter Name: Eesha


Enter Class Name: B.SC
Enter Roll.Number: 5
Enter Marks: 621

Enter Name: Fayaz


Enter Class Name: B.SC
Enter Roll.Number: 6
Enter Marks: 555

Enter Name: Gayathri


Enter Class Name: B.SC
Enter Roll.Number: 7
Enter Marks: 603

Enter Name: Hruthik


Enter Class Name: B.SC
Enter Roll.Number: 8
Enter Marks: 525

Enter Name: Immanuel


Enter Class Name: B.SC
Enter Roll.Number: 9
Enter Marks: 611

Enter Name: Janaki Ram


Enter Class Name: B.SC
Enter Roll.Number: 10
Enter Marks: 611

36
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

Student Information:
Roll.NO Name Class TotalMarks
1 Arun B.SC 610
2 Bharathi B.SC 642
3 Charitha B.SC 600
4 Dhavan B.SC 582
5 Eesha B.SC 621
6 Fayaz B.SC 555
7 Gayathri B.SC 603
8 Hruthik B.SC 525
9 Immanuel B.SC 611
10 Janaki Ram B.SC 611

PROBLEM- NO:16

Write a program that opens an existing text file and copies it to a new text
file with all lowercase letters changed to capital letters and all other
characters unchanged.

C PROGRAM:
#include<stdio.h>
#include<process.h>
void main() {
FILE *fp1, *fp2;
char a;
clrscr();
fp1 = fopen("test.txt", "r");
if (fp1 == NULL) {
printf("cannot open this file");
exit(1);
}

37
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester

fp2 = fopen("test1.txt", "w");


if (fp2 == NULL) {
printf(" Cannot open this file");
fclose(fp1);
exit(1);
}
do {
a = fgetc(fp1);
if(a >= 'a' && a <= 'z')
{
a = toupper(a);
}
fputc(a, fp2);
} while (a != EOF);

printf(“\n 1 File is Successfully Copied”);


fclose(fp1);
fclose(fp2);
getch();
return 0;
}

I/O:
1 File is Successfully Copied

*****

38
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066

You might also like