C Lab Record
C Lab Record
5. Fibonacci Series
Sorting an Element in an ascending or
6.
descending order
7. Linear Search
#include<stdio.h>
#include<conio.h>
voidmain()
{
int amount, rate, time, si;
printf("\nEnter Principal Amount : ");
scanf("%d", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);
printf("\nEnter Period of Time : ");
scanf("%d", &time);
si = (amount * rate * time) / 100;
printf("\nSimple Interest : %d", si);
Output:
Simple Interest : 50
Result:
The Program has been executed and the output was verified.
Ex No: 2 Name:
Date: Reg No:
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int number;
clrscr();
printf(" Finding positive,Negative and Zero \n");
printf("_____________________________________ \n");
printf("Enter a number\n");
scanf ("%d", &number);
printf("The Given Number is %d \n",number);
if (number > 0)
printf ("%d is a positive number\n", number);
else
{
if(number<0)
{
printf ("%d is a negative number\n", number);
}
else
{
printf ("%d is a Zero \n", number);
}
}
getch();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 3 Name:
Date: Reg No:
Program:
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf(" Finding Vowels Or Consonant \n");
printf(" ___________________________ \n");
printf("\n Enter any character:");
scanf("%c",&ch);
clrscr();
switch(tolower(ch))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\n The character %c is vowel!",ch);
break;
default :
printf("\n The character %c is consonant!",ch);
}
getch();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 4 Name:
Date: Reg No:
Program:
#include<stdio.h>
void main()
{
int i=1,count=0,n;
clrscr();
printf(" Checking Prime Or Not \n");
printf(" _____________________ \n");
printf("\nEnter a number:");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
count++;
i++;
}
if(count==2)
printf("\n Given Number %d is prime",n);
else
printf("\nGiven number %d is not a prime",n);
getch();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 5 Name:
Date: Reg No:
Program:
#include <stdio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
clrscr();
printf("Enter number of terms: ");
scanf("%d",&n);
clrscr();
printf(" Fibonacci Series \n");
printf(" ________________ \n");
printf(" The Given Term is : %d\n",n);
printf("Fibonacci Series: \n%d \n%d\n", t1, t2); count=2;
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d\n",display);
}
getch();
return 0;
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 6a Name:
Date: Reg No:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int ar[100],j,n,i,tmp;
printf(" Enter the size of the array \t");
scanf("%d",&n);
printf("Now enter the elements in the array \t");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
clrscr();
printf("\n Sorting Element \n ");
printf(" _______________ \n");
printf(" Size of an Given Array %d \n",n);
printf(" Before Sorting Array are \n");
for(i=0;i<n;i++)
{
printf("\t %d",ar[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(ar[j]>ar[j+1])
{
tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp;
}
}
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 6b Name:
Date: Reg No:
Program:
#include <stdio.h>
#include<conio.h>
void main ()
{
int number[30];
int i, j, a, n;
Result:
The Program has been executed and the output was verified.
Ex No: 7 Name:
Date: Reg No:
Linear Search
Program:
#include<stdio.h>
#include<conio.h>
int main() {
int a[30], ele, num, i;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++) {
scanf("%d", &a[i]);
}
i = 0;
while (i < num && ele != a[i]) {
i++;
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 8 Name:
Date: Reg No:
Aim : Program to find the smallest and largest number among ‘n’ numbers
Program:
#include <stdio.h>
#include<conio.h>
void main ()
{
int i, j, n, a[20], max, min;
clrscr ();
printf ("Enter the limit\n");
scanf ("%d", &n);
printf ("Enter the numbers\n");
for (i = 0; i < n; i++)
{
scanf ("%d", &a[i]);
}
max = a[0]; min = a[0];
for (i = 0; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
clrscr();
printf(" Finding Largest & Smallest Number \n");
printf(" _________________________________ \n");
printf(" The Value of N is %d \n",n);
printf(" The N Terms are \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf ("Largest number is %d\nSmallest number is %d", max, min);
getch ();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 9 Name:
Program:
#include<stdio.h>
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],d[10][10],m1,n1,m2,n2;
printf("Enter the number of Rows of Mat1 : ");
scanf ("%d",&m1);
printf("Enter the number of Columns of Mat1 : ");
scanf ("%d",&n1);
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
{
printf("Enter the Element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter the number of Rows of Mat2 : ");
scanf ("%d",&m2);
printf("Enter the number of Columns of Mat2 : ");
scanf ("%d",&n2);
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf(" Enter the Element b[%d][%d] :",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j] = a[i][j] + b[i][j] ;
d[i][j] = a[i][j] - b[i][j];
}
}
printf("\nThe Addition of two Matrices \n");
printf(" ____________________________ \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++ )
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\nThe Subtraction of two Matrices \n");
printf(" ____________________________ \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++ )
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the number of Rows of Mat1 : 2
Enter the number of Columns of Mat1 : 2
Enter the Element a[0][0] : 4
Enter the Element a[0][1] : 4
Enter the Element a[1][0] : 4
Enter the Element a[1][1] : 4
Enter the number of Rows of Mat2 : 2
Enter the number of Columns of Mat2 : 2
Enter the Element b[0][0] :1
Enter the Element b[0][1] :2
Enter the Element b[1][0] :3
Enter the Element b[1][1] :4
The Addition of two Matrices
____________________________
5 6
7 8
Matrix multiplication
Program:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
printf("\n");
}
}
return 0;
}
Output:
Result:
The Program has been executed and the output was verified.
Ex No: 11 Name:
Program:
#include <stdio.h>
#include <conio.h>
void main() {
char *a;
int i,len,flag=0;
clrscr();
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
if(a[i]==a[len-i-1])
flag=flag+1;
}
printf("String Palindrome \n");
printf("_________________ \n");
if(flag==len)
printf("\nThe Given String %s is Palindrome \n",a);
else
printf("\nThe Given String %s is not Palindrome \n",a);
getch();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 12 Name:
Date: Reg No:
String Manipulation
Aim: Program for manipulating the strings using string handling functions
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20],s3[30];
int x,l1,l2,l3;
printf("Enter the strings\n");
scanf("%s %s",s1,s2);
clrscr();
printf(" String Manipulation \n");
printf(" ___________________ \n");
printf(" First String %s \n",s1);
printf(" Second String %s \n\n",s2);
l1=strlen(s1);
l2=strlen(s2);
printf( "Length of First String %d \n",l1);
printf(" Length of Second String %d \n\n",l2);
printf(" Before Concatenation First String is %s\n",s1);
strcat(s1,s2);
printf(" After Concatenation Second String is %s \n",s1);
printf(" Before Copying Second String is %s \n",s2);
strcpy(s2,s1);
printf(" After Copying Second String is %s \n\n",s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf("\nBoth Strings are not equal\n");
}
else
{
printf("\nBoth Strings are equal");
}
getch();
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 13 Name:
Date: Reg No:
Aim: Program to generate the mark sheet of the student using structure
Program:
#include<stdio.h>
struct student
{
char name[30];
int rollno;
int m1,m2,m3;
int total;
};
void main()
{
int i,n;
float avg;
struct student st[30];
clrscr();
printf("Enter how many students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Student name \n");
scanf("%s",&st[i].name);
printf(" Enter Student Roll No. \n");
scanf("%d",&st[i].rollno);
printf("Enter Subject Mark : ");
scanf("%d",&st[i].m1);
scanf("%d",&st[i].m2);
scanf("%d",&st[i].m3);
}
printf("Student Mark Preparation \n");
printf("_______________________ \n");
for(i=0;i<n;i++)
{
printf(" Student Name %s\n",st[i].name);
printf(" Student Roll No: %d\n",st[i].rollno);
printf(" Marks %d \n%d\n %d\n",st[i].m1,st[i].m2,st[i].m3);
st[i].total=st[i].m1+st[i].m2+st[i].m3;
avg=st[i].total/3;
printf("\nTotal Mark : %d\n",st[i].total);
printf(" Average Marks %f\n",avg);
if(st[i].m1>50&&st[i].m2>50&&st[i].m3>50)
{
printf(" The Result for the student is Pass \n");
}
else
{
printf(" The Result for the Student is Failed \n");
}
}
getch();
}
Output:
Result:
The Program has been executed and the output was verified.
Ex No: 14 Name:
Aim: Program to sum of two numbers using function with arguments and without return value.
Program:
#include<stdio.h>
void add(x,y);
void main ()
{
int a, b;
printf(“Enter the value for A:”);
scanf(“%d”,&a);
printf(“Enter the value for B:”);
scanf(“%d”,&b);
add(a,b);
}
Output:
Result is: 11
Result:
The Program has been executed and the output was verified.
Ex No: 15 Name:
Aim: Program to swap two numbers using call by value and call by reference.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
void swap1(int,int);
void swap2(int*,int*);
inta,b;
clrscr();
printf("\n\nEnter the value of A and B=");
scanf("%d%d",&a,&b);
printf("\n\nBefore swapping A=%d,B=%d",a,b);
swap1(a,b);
printf("\n\nAfter call by value,values are A=%d,B=%d",a,b);
swap2(&a,&b);
printf("\n\nAfter call by reference,values are A=%d,B=%d",a,b);
getch();
}
void swap1(inta,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap2(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
OUTPUT:
Result:
The Program has been executed and the output was verified.
Ex No: 16 Name:
Program:
#include<stdio.h>
#include<conio.h>
long int factorial(long int n);
long int main()
{
long int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%ld",&n);
clrscr();
printf(" Factorial Value \n");
printf(" _______________ \n");
printf(" The Given Value is %ld \n",n);
printf("Factorial of %ld is %ld", n, factorial(n));
getch();
return 0;
}
long int factorial( long int n)
{
if(n==0)
{
return (1);
}
else
{
return n*factorial(n-1);
}
}
Output
Result:
The Program has been executed and the output was verified.
Ex No: 17 Name:
Date: Reg No:
Output:
Result:
The Program has been executed and the output was verified.
Ex No: 18 Name:
Aim : Program to create and write, read text in a file using file handling.
Program:
#include<stdio.h>
intmain()
{
FILE*fp; /* file pointer*/
charfName[20];
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}
printf("File created successfully.");
/*writting into file*/
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
printf("Contents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return0;
}
Contents of file is :
ABC
Result:
The Program has been executed and the output was verified.
Ex No: 19 Name:
Date: Reg No:
Program:
#include <stdio.h>
int main ()
{
FILE *fp;
int i=1, j=2, k=3, num;
fp = fopen ("test.c","w+");
putw(i,fp);
putw(j,fp);
putw(k,fp);
//fclose(fp);
//fp = fopen ("test.c","r");
fseek(fp, 0, SEEK_SET);
while((num=getw(fp))!=EOF)
{
printf("Data in test.c file is %d \n", num);
}
fclose(fp);
return 0;
}
Output:
Result:
The Program has been executed and the output was verified.
Ex No: 20 Name:
Date: Reg No:
Program:
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
return 0;
}
Output:
Result:
The Program has been executed and the output was verified.