CS Programming in C Record
CS Programming in C Record
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
PSEUDOCODE
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
PSEUDOCODE
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
PSEUDOCODE
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);
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
PSEUDOCODE
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];
}
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
PSEUDOCODE
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");
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
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
PROBLEM- NO:8
Write a program to find the GCD of two numbers using iteration and
recursion.
PSEUDOCODE
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
I/O
Enter two integers: 100 10
G.C.D of 100 and 10 is 10
.
PROBLEM- NO:9
PSEUDOCODE
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)
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);
}
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
C PROGRAM:
//nalphas.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
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
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
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");
}
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
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);
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");
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
35
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066
Programming in C [C Lab] B.SC- Computer Science 1st Semester
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
I/O:
1 File is Successfully Copied
*****
38
SRI SNEHA DEGREE COLLEGE (Affiliated to Mahatma Gandhi University) : 4066