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

C Lab Record

The program takes input of numbers from the user, stores them in an array and finds the largest and smallest number among the entered numbers using loops. It displays the largest and smallest number.

Uploaded by

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

C Lab Record

The program takes input of numbers from the user, stores them in an array and finds the largest and smallest number among the entered numbers using loops. It displays the largest and smallest number.

Uploaded by

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

INDEX

Ex Date Program Page No. Signature


No
1. Find simple interest

2. Checking Positive, Negative & Zero Number

3. Checking Vowel & Consonant

4. Checking Prime Or Not

5. Fibonacci Series
Sorting an Element in an ascending or
6.
descending order
7. Linear Search

8. Finding Smallest & Largest Number

9. Matrix Addition and subtraction

10. Matrix Multiplication


11. Checking String Palindrome
12. String Manipulation

13. Student Mark Sheet Preparation


Function with arguments and without return
14.
value
15. Call by value and call by reference

16. Factorial Using recursion

Largest Element using Dynamic Memory


17. Allocation

18. File – Create, Write and Read

19. File Handling functions – getw(), putw()

20. Area calculation using macros


Ex No: 1 Name:
Date: Reg No:

Find the simple interest

Aim : Program to find the simple interest.

#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:

Enter Principal Amount : 500


Enter Rate of interest : 5
Enter Period of Time   : 2

Simple Interest : 50

Result:

The Program has been executed and the output was verified.
Ex No: 2 Name:
Date: Reg No:

Finding Positive or Negative or Zero number

Aim : Program to check whether a given number is Positive or Negative or Zero.

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:

Finding Vowels and Consonant using Switch case

Aim : Program for Vowels and Consonant Using Switch Case

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:

Checking Prime Number or Not.

Aim: Program to check the given number is Prime Number or not.

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:

Generating Fibonacci series

Aim: Program to display Fibonacci series

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:

Sorting an elements in an ascending order

Aim: - Program to sort elements in ascending order using array.

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;
}
}
}

printf("\n\n After Sorting Array are - \n");


for(i=0;i<n;i++)
{
printf("\t %d",ar[i]);
}
getch();
}

Output

Result:
The Program has been executed and the output was verified.
Ex No: 6b Name:
Date: Reg No:

Sorting an elements in descending order

Aim: - Program to sort elements in descending order using array.

Program:
#include <stdio.h>
#include<conio.h>

void main ()
{
int number[30];
int i, j, a, n;

printf("Enter the value of N:\n");


scanf("%d",&n);
printf("Enter the numbers:\n");
for(i =0; i < n;++i)
scanf("%d",&number[i]);
/* sorting begins ... */
for(i =0; i < n;++i)
{
for(j = i +1; j < n;++j)
{
if(number[i]< number[j])
{
a = number[i];
number[i]= number[j];
number[j]= a;
}
}
}
printf("The numbers arranged in descending order are given below\n");
for(i =0; i < n;++i)
{
printf("%d\n", number[i]);
}
}
Output:
Enter the value of N
5
Enter the numbers
234
780
130
56
90
The numbers arranged in descending order are given below
780
234
130
90
56

Result:
The Program has been executed and the output was verified.
Ex No: 7 Name:
Date: Reg No:

Linear Search

Aim : - Program to search an element in an array using linear search method

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]);
}

//Read the element to be searched


printf("\nEnter the elements to be searched :");
scanf("%d", &ele);

//Search starts from the zeroth location


clrscr();
printf(" Linear Search \n");
printf("_______________\n");
printf(" The Value of N is %d \n",num);
printf(" N Terms are : \n");
for(i=0;i<num;i++)
{
printf("%d\n",a[i]);
}

printf(" The Search Element is %d \n",ele);

i = 0;
while (i < num && ele != a[i]) {
i++;
}

//If i < num then Match found


if (i < num) {
printf("Number %d found at the location %d",ele, i + 1);
} else {
printf("Number %d not found",ele);
}
getch();
return (0);
}

Output

Result:

The Program has been executed and the output was verified.
Ex No: 8 Name:
Date: Reg No:

Finding smallest and largest number

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:

Date: Reg No:

Matrix Addition and Subtraction

Aim :- Program to add two matrices.

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

The Subtraction of two Matrices


____________________________
3 2
1 0
Result:
The Program has been executed and the output was verified.
Ex No: 10 Name:

Date: Reg No:

Matrix multiplication

Aim :- Program to multiply two matrices.

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];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

Output:

Enter number of rows and columns of first matrix


32
Enter elements of first matrix
123456
Enter number of rows and columns of second matrix
25
Enter elements of second matrix
1111111111
Product of the matrices:
3 3 3 3 3
7 7 7 7 7
11 11 11 11 11

Result:

The Program has been executed and the output was verified.
Ex No: 11 Name:

Date: Reg No:

Checking String Palindrome

Aim: Program to check the given string is palindrome or not.

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:

Student Mark Sheet Preparation

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:

Date: Reg No:

Function with arguments and without return value

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);
}

void add (intx,int y)


{
int z;
z = x+y;
printf ("Result is %d \n", z);
}

Output:

Enter the value of A: 5

Enter the value of B: 6

Result is: 11

Result:

The Program has been executed and the output was verified.
Ex No: 15 Name:

Date: Reg No:

Swap two numbers using call by value and call by reference

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:

Enter the value of A and B=


10
20

Before swapping A=10,B=20

After call by value,values are A=10,B=20

After call by reference,values are A=20,B=10

Result:

The Program has been executed and the output was verified.
Ex No: 16 Name:

Date: Reg No:

Finding Factorial Using Recursive Function

Aim: Program to calculate factorial of a number using recursion.

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:

Find Largest Element Using Dynamic Memory Allocation

Aim: To find largest element using dynamic memory allocation.


Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
float *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL) {
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i) {
printf("Enter Number %d: ", i + 1);
scanf("%f", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf("Largest number = %.2f", *data);
return 0;
}

Output:

Enter the total number of elements: 5


Enter Number 1: 3.4
Enter Number 2: 2.4
Enter Number 3: -5
Enter Number 4: 24.2
Enter Number 5: 6.7
Largest number = 24.20

Result:

The Program has been executed and the output was verified.
Ex No: 18 Name:

Date: Reg No:


To create and write, read text in a file

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;
}

Enter file name to create : ok.txt

File created successfully.

Data written successfully.

Contents of file is :

ABC

Result:

The Program has been executed and the output was verified.
Ex No: 19 Name:
Date: Reg No:

File Handling Functions

Aim : Program using file handling functions.

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:

Data in test.c file is 1


Data in test.c file is 2
Data in test.c file is 3

Result:

The Program has been executed and the output was verified.
Ex No: 20 Name:
Date: Reg No:

Area Calculation using Macros

Aim: Program for area calculation using macros.

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:

Enter the radius: 5


Area = 78.54

Result:

The Program has been executed and the output was verified.

You might also like