CS Lab File-1
CS Lab File-1
CS Lab File-1
1
2
Serial Program Title Date Signature
No.
3
4
Serial Signature
No.
5
#include<stdio.h>
void main()
{
clrscr();
int a,I,sum=0;
float average=0;
for(i=0;i<10;i++)
{
printf(“Enter a number”);
scanf(“%d”,&a);
sum=sum+a;
6
}
printf(“sum=%d\n”,sum);
average=sum/10.0;
printf(“Average=%f\n”,average)
getch();
}
7
2: Program to find the greatest of 10 numbers.
#include<conio.h>
#include<stdio.h>
8
void main()
{
clrscr();
int a,b,c;
printf("Enter three numbers");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
if(a>b && a>c)
{
printf("Greatest number is: %d" , a);
9
}
if(b>c && b>a)
{
printf("Greatest number is: %d" , b);
}
if(c>a && c>b)
{
printf("Greatest number is: %d" , c);
}
getch();
}
10
11
12
3: Program to find Simple Interest.
#include<conio.h>
#include<stdio.h>
void main()
{
13
int p,n,count;
float r,si;
count=1;
while(count<=3)
{
printf("\nenter values of p,n, and r");
scanf("%d%d%f" , &p,&n,&r);
si=p*n*r/100;
printf("simple ineterest = rs. %f" , si);
count=count+1;
}
14
getch();
}
15
4: Program to find whether the entered number is
prime.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,i,f,g,flag;
printf("Enter a number");
scanf("%d" , &a);
16
if(a==2 || f==3)
{
printf("Prime");
}
if(a==1)
{
printf("Undefined");
}
else
{
for(i=2;i<a;i++)
17
{
if(a%i==0)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0) printf("Prime");
if(flag==1) printf("Composite");
18
}
getch();
}
19
20
5. Program to find the sum of a 5 digit number
#include<conio.h>
#include<stdio.h>
Void main()
{
clrscr();
int a,rem,q,sum=0;
printf(“Enter a number”);
21
scanf(“%d”,&a);
while(a>0)
{
rem=a%10;
sum=sum+rem;
a=a/10;
}
printf(“Sum of digits=%d”,sum);
getch();
}
22
6. Program to reverse a 5 digit number
23
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,reverse=0;
printf("Enter a number=");
scanf("%d" , &n);
while(n!=0)
{
reverse=reverse*10;
24
reverse=reverse+n%10;
n=n/10;
}
printf("Reverse of entered number= %d",reverse);
getch();
}
25
7. Program to generate the Fibonacci Sequence
#include<conio.h>
26
#include<stdio.h>
void main()
{
clrscr();
int n,first=0,second=1,next,c;
printf("Enter the number of terms");
scanf("%d" , &n);
printf("First %d terms of Fibonacci Series are: \n",n);
for(c=0;c<n;c++)
{
if(c<=1) next=c;
27
else
{next=first+second;
first=second;
second=next;}
printf("%d\n" , next);
}
getch();
}
28
29
30
31
8. Write a program to sort an array using bubble
#include<stdio.h>
void main()
{
int a[10], i, j, t;
32
printf("\nEnter 10 numbers ");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
33
}
}
}
printf("\nSorted loop is: ");
for(i=0;i<10;i++)
{
printf("%d ", a[i]);
}
}
34
35
9. Write a program to sort an array using
selection sort.
#include<stdio.h>
void main()
{
int a[10], i, j, t;
printf("\nEnter 10 numbers ");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<10;i++)
36
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\nSorted loop is: ");
for(i=0;i<10;i++)
{
37
printf("%d ", a[i]);
}
}
38
10. Program to count the number of vowels in a
given string
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,ctr=0;
char a[50];
printf("Enter string: ");
39
gets(a);
for(i=0;a[i]!='\0';i++)
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]
=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
ctr++;
printf("\n No. of vowels:%d ",ctr);
getch();
}
40
41
11. Program to convert a string from lower case
to upper case and vice versa
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int i;
printf("Enter any string");
42
scanf("%s",str);
printf("The string is %s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in upper case is %s",str);
getch();
}
43
12. Program to check if a given string is a
palindrome or not
#include<conio.h>
#include<stdio.h>
#include<string.h>
44
void main()
{
clrscr();
int i,k=0;
char a[3];
printf("Enter a string");
for(i=0;i<3;i++)
scanf("%c",&a[i]);
for(int j=2;j>=0;j--)
{
if(a[j]!=a[2-j])
{
k=1;
45
}
}
46
13. Program to pass and return pointer to function
and hence calculate average of an array
#include <stdio.h>
47
#include<conio.h>
int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
return 0;
}
return avg;
}
50
51
52
53
14. Program to pass an array as pointer to a
function that calculates the sum of all elements of
the array
#include<conio.h>
#include<stdio.h>
void call(int arr[10],int n)
{
int *ptr,sum=0,i=0;
ptr=arr;
for(i=0;i<n;i++)
{
54
sum+=*ptr;
ptr++;
}
printf("\n Sum of all the elements of the array
is:%d",sum);
}
int main()
{
int a[10],size,i;
printf("Enter the size of the array:");
scanf("%d",&size);
55
printf("\nEnter your array:\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
call(a,size);
getch();
return 0;
}
56
57
58
59
15. Program to multiply two 3 by 3 matrices
#include<conio.h>
#include<stdio.h>
int main()
{
clrscr();
int p[3][3],q[3][3],r[3][3],i,j,k=0;
printf("/nEnter the elements of matrix 1\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&p[i][j]);
60
printf("\nEnter the elements of matrix 2\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&q[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
r[i][j]=0;
for(k=0;k<3;k++)
r[i][j]=r[i][j]+p[i][j]*q[k][j];
}
}
61
printf("\n The multiplication of the two matrices is
\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",r[i][j]);
printf("\n");
}
getch();
return 0;
}
62
63
64
65
16. Program to convert decimal to binary and vice
versa
#include<conio.h>
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,a[20],k=0,ch,dec=1;
long int j;
66
printf("Enter 1 for decimal to binary and enter 2 for
binary to decimal\n");
scanf("%d",&ch);
if(ch==1)
{
printf("\nEnter the decimal number\n");
scanf("%d",&n);
for(i=n;i>0;i=i/2)
a[k++]=i%2;
printf("\nThe binary form of the number is");
for(i=k;i>0;i--)
67
printf("%d",a[i]);
}
else if(ch==2)
{
printf("\nEnter the binary number\n");
scanf("%d",&j);
printf("\nThe deciaml form of the number is\t");
for(i=j;i>0;i=i/10)
dec=dec+pow(2,(i%10));
printf("%d",dec);
}
68
else
printf("\nWrong choice\n");
getch();
return 0;
}
69
70
71
17. Program to generate the employee details
using structure.
#include<conio.h>
#include<stdio.h>
struct employee
{
char name[25],dept[25];
72
int id;
float salary;
};
int main()
{
struct employee emp;
printf("\nEnter the name of employee\n");
scanf("%s",emp.name);
printf("\nEnter the department of employee\n");
scanf("%s",emp.dept);
printf("\nEnter the id of emplyee\n");
73
scanf("%d",&emp.id);
printf("\nEnter the salary of emplyee\n");
scanf("%f",&emp.salary);
printf("The details are as follows");
printf("\nName:\t%s",emp.name);
printf("\nDepartment:\t%s",emp.dept);
printf("\nEmplyee ID No.\t%d",emp.id);
printf("\nSalary\t%f",emp.salary);
getch();
return 0;
}
74
75
76
77
18. Program to find the length of a string without
using strlen and then pass the string to characters.
#include<conio.h>
#include<stdio.h>
int slen(char b[25])
78
{
int i;
for(b=0;b[i]!='\0';i++);
return i;
}
int main()
{
clrscr();
char a[25];
int len;
printf("\nEnter a string\n");
79
scanf("%s",a);
len=slen(a);
printf("\nThe length of the string is\t%d",len);
getch();
return 0;
}
80
19. Program to read a file and after converting all
lower case to upper case letters write it to another
file.
#include<conio.h>
81
#include<stdio.h>
int main()
{
file *fp,*fr;
fp=fopen("pr24.txt","r");
fr=fopen("pr24a.txt","w");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
82
if(ch>97||ch<=122)
ch=ch-32;
fputc(ch,fr);
}
fclose(fp);
fclose(fr);
getch();
return 0;
}
83
20. Program to find the factorial of a number
using recursion.
84
#include<conio.h>
#include<stdio.h>
long fact(int i)
{
if(i==1)
return 1;
else
return(i*fact(i-1));
}
int main()
{
int i;
85
printf("\nEnter the number to find the factorial\n");
scanf("%d",&i);
printf("\nThe factorial of the given number
is\t%d",fact(i));
getch();
return 0;
}
86
87
21. Program to find the area and perimeter of a
circle, rectangle, square and triangle using
functions.
#include<conio.h>
#include<stdio.h>
#include<math.h>
float ar,per;
void circle()
{
88
float r;
printf("\nEnter the radius of circle\n");
scanf("%d",&r);
ar=3.14*r*r;
per=2*3.14*r;
printf("\nThe area of circle if \t%f\t and its perimeter
if \t%f",ar,per);
}
void rectangle()
{
float a,b;
89
printf("\nEnter the sides of rectangle\n");
scanf("%f %f", &a, &b);
ar=a*b;
per=2*(a+b);
printf("\nThe area of rectangle is \t%f\t and its
perimeter if\t%f",ar,per);
}
void square()
{
float a;
printf("\nEnter the side of square\n");
90
scanf("%f",&a);
ar=a*a;
per=4*a;
printf("\nThe area of square is \t%f\tand its perimeter
if \t%f",ar,per);
}
void triangle()
{
float a,b,c,s;
printf("\nEnter the sides of triangle\n");
scanf("%f%f%f",&a,&b,&c);
91
per=a+b+c;
s=per/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nThe area of triangle iss \t%f\tand its
perimeter is\t5f",ar,per);
}
int main()
{
int i;
92
printf("\nEnter 1 for circle\nEnter 2 for
rectangle\nEnter 3 for square\nEnter 4 for
triangle\n");
scanf("%d",&i);
switch(i)
{
case 1:
circle();
break;
case 2:
rectangle();
93
break;
case 3:
square();
break;
case 4:
triangle();
break;
default:
printf("\nWrong choice\n");
}
getch();
94
return 0;
}
95
96
22. Write a program to implement switch case
statement
#include<conio.h>
#include<stdio.h>
void main()
{
97
clrscr();
int i;
printf("enter the value of i");
scanf("%d" , &i);
switch (i)
{
case 1:
printf("i am case 1 \n");
break;
case 2:
printf("i am case 2 \n");
break;
case 3:
98
printf("i am case 3 \n");
break;
default:
printf("i am default");
}
getch();
}
99
100
23. Program to print the following:
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
101
int c,k;
for(c=1;c<=5;c++)
{
for(k=1;k<=c;k++)
printf("%d" , k);
printf("\n");
}
getch();
return 0;
}
102
103
24. Write a program for the addition of two 3*3
matrices
#include<conio.h>
#include<stdio.h>
int main()
104
{
clrscr();
int i,j,p[3][3],q[3][3],r[3][3];
printf("Enter the elements of the first matrix\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&p[i][j]);
105
for(i=0;i<3;i++)
for(j=0;j<3;j++)
r[i][j]=p[i][j]+q[i][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",r[i][j]);
printf("\n");
}
getch();
106
return 0;
}
107
108
109
25. Write a program to search a number from an
array
#include<conio.h>
#include<stdio.h>
110
void main()
{
clrscr();
int a[30],element,num,i;
printf("Enter number of elements:");
scanf("%d" , &num);
printf("\n Enter the values:");
for(i=0;i<num;i++)
{
scanf("%d" , &a[i]);
}
111
printf("Enter the elements to be searched:");
scanf("%d" , &element);
i=0;
while(i<num&&element !=a[i])
{ i++; }
if(i<num)
{printf("Number found at the location =%d" , i);}
else
{printf("Number not found");}
getch();
}
112
113
114
115