c program lab final (1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

EX NO:1

DATE: PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

AIM:

To write the program using I/O statements and expression using c language

ALGORITHM:
1. Start the program
2. Input the radius of the circle
3. Find the area and circumference of the circle using the formule
Area=3.14*r*r
Circum=2*3.14*r
4. Print the area and the circum of the circle
5. Display the output of the calculations.
6. Stop.

1
PROGRAM:

#include<stdio.h>
#include<conio.h>
Void main()
{
float rad,area,circum;
printf("\nEnter the radius of the circle");
scanf("%f",&r);
area=3.14*rad*r;
circum=2*3.14*r;
printf(“\nArea=%f”,area);
printf("\n Circumference=%f,circum);
}

2
SAMPLE OUTPUT:

ENTER THE RADIUS OF THE CIRCLE


5
AREA=78.500000
CIRCUMFERENCE=31.4000000

RESULT:

Thus the program for I/O statement and expression was executed successfully and the result
was verified.

3
EX NO:2(a) PROGRAM TO CHECK WHETHER A PERSON IS ELIGIBLE
DATE: TO VOTE OR NOT

AIM:

To implement Bubble Sort using PHP Script.

ALGORITHM:

1. Start the program

2. Declare the variable age and read the age.

3. Check the status age =18 or age>18


4. If its condition is true eligible for vote.

5. Otherwise the person is not eligible for vote

6. Stop the program.

4
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50];//or*name
printf("\n Type the name of the candidate:");
gets(name);
printf("\n Enter the age:");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligibile for vote",name);
else
printf("\n %s is not Eligibile for vote",name);
getch();
}

5
SAMPLE OUTPUT:

Type The Name Of The Candidate:AAA


Enter The Age:18
Aaa Is Eligible For Vote
Type The Name Of The Candidate:bbb
Enter the age:14
Bbb is not Eligible for vote

RESULT:

Thus the program for checking eligibility for voting is executed successfully.

6
EX NO:2(b)
PROGRAM TO DO ARITHMETIC OPERATIONS
DATE:

AIM:
To generate a C program to doing arithmetic operations.

ALGORTHIM:

1. Start the program

2. Enter (assign)value of the variable(addend)X.

3. Enter (assign)value of the variable(addend)Y.


4. Calculate Z (Z = X + Y)
5. Display result of addition, value of Z on the monitor with the message "The sum of X and Y is:"
6. Stop the program.

7
PROGRAM:
#include<stdio.h>
void main()
{
int a,b,result,sq1,sq2,ch;
float divide;
clrscr();
printf("enter two integers:"); scanf("%d
%d",&a,&b);
printf("1.add,2.subtrat,3.multiply,4.divide,5.sqare");
printf("\n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
result=(a+b); printf("sum=
%d\n",result); break;
}
case 2:
{
result=(a-b); printf("difference=
%d\n",result); break;
}
case 3:
{
result=(a*b); printf("mutiplication=
%d\n",result); break;
}
case 4:
{
result=a/(float)b;
printf("diviosin=%2f\n",result);
break;
}
case 5:
{
sq1=(a*a); printf("square=
%d\n",sq1); sq2=b*b;
printf("second square number=%\n",sq2);
break;
}
}
getch();
}

8
SAMPLE OUTPUT:

Enter two integer :23 45


1.add 2.subraction,3.multiplication,4.division,5.square
Enter the choice: 1
Sum=68
Enter two integer :50 20
1.add 2.subraction,3.multiplication,4.division,5.square
RESULT: Enter the choice: 2
Sum=30
Thus the c program for arithmetic operation using switch case has been executed successfully .
Enter two integer :50 20
1.add 2.subraction,3.multiplication,4.division,5.square
Enter the choice: 3
Sum=1000
Enter two integer :50 20
1.add 2.subraction,3.multiplication,4.division,5.square
Enter the choice: 4
Sum=2.5

9
EX NO:3(a) PROGRAM TO FIND THE GIVEN NUMBER IS ARMSTRONG
DATE: OR NOT

AIM:

To write C program for find the given number is Armstrong or not.

ALGORITHM:

1. Start the program.

2. Declare variable sum,temp,num.

3. Read number from user.

4. Initialize variable sum=0 and temp=num

5. Repeat until num>=0

6. If sum==temp

Print “Armstrong Number”

Else

Print “not Armstrong number”

7. Stop the program.

10
PROGRAM:

#include<stdio.h>
#include<cono.h>
void main()
{
intn,orgnum,r,result=0;
printf("enter a three digit number:");
scanf("%d",&n);
orgnum=n;
while(orgnum!=0)
{
r=orgnum%10;
result+=r*r*r;
orgnum/=10;
}
if(result==n)
printf("%d is an armstrongnumber",n);
else
printf("%d is not an armstrongnumber",n);
getch();
}

11
SAMPLE OUTPUT:

Enter a three digit number:371


371 is an Armstrong number
Enter a three digit number:401
401 is not an Armstrong
number

RESULT:

The program for checking given number is Armstrong or not has been successfully executed.

12
EX NO:3(b) PROGRAM TO PERFORM ARITHMETIC OPERATIONS
DATE:

AIM :

To write a C Program to Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.

ALGORITHM :

1. Start
2. Declare variables
3. Read the Inputs .
4. Calculate Arithmetic operations(+,-,*,/,pow) for the input of two numbers.
5. Display the output of the calculations .
6. Stop

13
PROGRAM :

#include <stdio.h>
#include <conio.h>
int main(){
/* Variable declation */
int firstNumber, secondNumber;
int sum, difference, product;
long square;
float quotient;
/* Taking input from user and storing it in firstNumber and secondNumber */
printf("Enter First Number: ");
scanf("%d", &firstNumber);
printf("Enter Second Number: ");
scanf("%d", &secondNumber);
/* Adding two numbers */
sum = firstNumber + secondNumber;
/* Subtracting two numbers */
difference = firstNumber - secondNumber;
/* Multiplying two numbers*/
product = firstNumber * secondNumber;
/* Dividing two numbers by typecasting one operand to float*/
quotient = (float)firstNumber / secondNumber;
/* returns remainder of after an integer division */
square = firstNumber *firstNumber; printf("\
nSum = %d", sum);
printf("\nDifference = %d", difference);
printf("\nMultiplication = %d", product);
printf("\nDivision = %.3f", quotient);
printf("\n Square= %ld", square);
getch();
return 0;
}

14
SAMPLE OUTPUT:

Enter your first number:25


Enter your second number:4
Sum = 29
Difference = 21
Multiplication = 100
Division = 6.250
Square = 625

RESULT:
Thus a C Program for Arithmetic operations was executed and the output was obtained.

15
EX NO:4(a)
FIND THE LARGEST AND SMALLEST OF THE GIVEN ARRAY
DATE:

AIM:

To write the program to find the largest and smallest number of given array

ALGORITHM:

1. Start the program

2. Enter the size of array

3. Enter the elements of the array

4. Print the array elements

5. Initialize the large and small is equal to the first element of the array

6. Set a loop up to the array size

7. Check the next element greater than the larger.

8. If greater then assign next element to the large

9. Check the next element smaller than the larger. If smaller then assign next element to the small

10. Print the value of large and small after the execution of the loop

11. Stop the program

16
PROGRAM:

#include<stdio.h>
#include<conio.h>
Void main()
{
int a[100],I,small,large,no;
printf(“In how many numbers you want to find….”);
scanf(“%d”,&no);
printf(“Enter the elements of the array…”);
for(i=0;i<no;i++)
scanf(“%d”,&a[i]);
printf(“\nThe elements of the array”);
for(i=0;i<no;i++)
printf(“\n%d”,a[i]);
small=a[0];
large=a[0];
for(i=1;i<no;i++)
{
If(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
Printf(“\nThe largest of the given array is %d”,large);
Printf(“\nThe largest of the given array is %d”,small);
}

17
SAMPLE OUTPUT:

In how many numbers you want to find….5


Enter the elements of the array….
12 34 56 87 43
The elements of the
array 12 34 56 87 43
The largest of the given array is 87
The smallest of the given array is

RESULT:

Thus the largest and smallest number of the given array using c program has been executed successfully.

18
EX NO:4(b)
FIND THE ADDITION OF TWO MATRIXES
DATE:

AIM:

To write a program to add two matrices.

ALGORITHM:

1. Start the program

2. Enter the row and column of the matrix

3. Enter the elements of the A matrix

4. Enter the elements of the B matrix

5. Print the A matrix in the matrix form

6. Print the B matrix in the matrix form

7. Set a loop up to the row

8. Set a inner loop up to the column

9. Add the elements of A and B in column wise and store the result in C matrix

10. After the execution of the two loops Print the value of C matrix

11. Stop the program

19
PROGRAM:
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[25][25],b[25][25],c[25][25],I,j,m,n;
printf(“Enter the rows and columns of two matrixes…\n”);
scanf(“%d”,&m,&n);
printf(“\nEnter the elements of a matrix…”);
for(i=0;i<&m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
}
printf(“\nEnter the elements of B matrix….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
}
printf(“\nThe elements of A matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,a[i][j]);
}
printf(“\nThe elements of B matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,b[i][j]);
}
printf(“\n The addition of two matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
C[i][j]=a[i][j]+b[i][j];
Printf(“\t%d”,c[i][j]);
}
}

20
SAMPLE OUTPUT:

Enter the rows and column of two matrixes…3 3


Enter the elements of A matrix…1 2 3 4 5 6 7 8 9
Enter the elements of B matrix…1 2 3 4 5 6 7 8 9
The elements of A matrix
1 2 3
4 5 6
7 8 9
The elements of B matrix
1 2 3
4 5 6
7 8 9
The addition of two matrixes
2 4 6
8 10 12
14 16 18

RESULT:
Thus the program to add two matrix by using C program has been executed successfully.

21
EX NO:5(a) FIND WHETHER THE GIVEN STRING IS PALINDROME
DATE: OR NOT

AIM:

To write the c program to find whether the given string is palindrome or not

ALGORITHM:

1. Start the program


2. Enter the string
3. Find the string length using thr strlen() function
4. Print the string length
5. Set a loop up to the half of the string length
6. Compare every charcter above the middle character with the below character of the middle
character.
7. If any character equal prints the given string is palindrome
8. If the character is not equal then print the given string is not a palindrome
9. Stop the program.

22
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i,j;
char name[25];
printf(“Enter the string…”);
scanf(“%s”,name);
while(name[len]!=’\0’)
len++; printf(“\n
%d”,len);
for(i=0,j=len-1;i<len/2;i++,j--);
{
If(name[i]!=name[j])
{
Printf(“\nThe given string is not a palindrome”);
Exit(0);
}
}
Printf(“\n The given string is a palindrome”);
}

23
SAMPLE OUTPUT:

Enter the
string…..Malayalam 9
The given string is a palindrome

RESULT:

Thus the C program for finding the given string is palindrome or not has been executed successfully.

24
EX NO:6 SORTING USING PASS BY REFERENCE
DATE:

AIM :

To write a C Program to Sort the list of numbers using pass by reference.

ALGORITHM :

1. Start the program.


2. Declare variables and create an array
3. Read the Input for number of elements and each element.
4. Develop a function to sort the array by passing reference
5. Compare the elements in each pass till all the elements are sorted.
6. Display the output of the sorted elements .
7. Stop the program.

25
PROGRAM :
#include<stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void sortarray(int* arr,int num)
{
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

26
SAMPLE OUTPUT :

Enter the Number of Elements in an array : 5


Enter the Array elements
33
67
21
45
11
After Sorting....
11
21
33
45
67

RESULT :

Thus a C Program Sorting using pass by reference was executed and the output was
obtained.

27
EX NO:7
C PROGRAM FOR TOWERS OF HANOI USING RECURSION
DATE:

AIM:
To write a C Program to Solve towers of Hanoi using recursion.

ALGORITHM:

1. Start the program.

2. Declare variables

3. Read the Input for number of discs.

4. Check the condition for each transfer of discs using recursion.

5. Display the output of the each move .

6. Stop the program.

28
PROGRAM:

#include <stdio.h>

#include <conio.h>

void towerofhanoi(int n, char from, char to, char aux)

if (n == 1)

printf("\n Move disk 1 from peg %c to peg %c", from, to);

return;

towerofhanoi(n-1, from, aux, to);

printf("\n Move disk %d from peg %c to peg %c", n, from, to);

towerofhanoi(n-1, aux, to, from);

int main()

int n;

clrscr();

printf("Enter the number of disks : ");

scanf("%d",&n); // Number of disks

towerofhanoi(n, 'A', 'C', 'B'); // A, B and C are names of peg

getch();

return 0;

29
SAMPLE OUTPUT :

Enter the number of disks : 3


Move disk 1 from peg A to peg
C Move disk 2 from peg A to
peg B Move disk 1 from peg C
to peg B Move disk 3 from peg
A to peg C Move disk 1 from
peg B to peg A Move disk 2
from peg B to peg C Move disk
1 from peg A to peg C

RESULT :

Thus a C Program Towers of Hanoi using Recursion was executed and the output was

obtained.

30
EX NO:8 PROGRAM SALARY SLIP OF EMPLOYEES USING
DATE: POINTERS AND STRUCTURES

AIM:

To write a C Program to Generate salary slip of employees using structures and

pointers.

ALGORITHM:

1. Start the program

2. Declare variables

3. Read the number of employees .

4. Read allowances, deductions and basic for each employee.

5. Calculate net pay= (basic+ allowances)-deductions

6. Display the output of the Pay slip calculations for each employee.

7. Stop the program

31
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Struct emp
{
Int empno;
char name[10], answer ;
int bpay, allow, ded, npay ;
struct emp *next;

};

void main()

int I,n=0;

int more_data = 1;

struct emp e *current_ptr, *head_ptr;

clrscr() ;

head_ptr = (struct emp *) malloc (sizeof(struct emp));

current_ptr = head_ptr;

while (more_data)

printf("\nEnter the employee number : ") ;

scanf("%d", & current_ptr->empno) ;

printf("\nEnter the name : ") ;

scanf("%s",& current_ptr->name) ;

printf("\nEnter the basic pay, allowances & deductions : ") ;

scanf("%d %d %d", & current_ptr ->bpay, & current_ptr ->allow, current_ptr


->ded) ;

32
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;

n++;

printf("Would you like to add another employee? (y/n): ");

scanf("%s", answer);

if (answer!= 'Y')

current_ptr->next = (struct eme *) NULL;

more_data = 0;

else

current_ptr->next = (struct emp *) malloc (sizeof(struct emp));

current_ptr = current_ptr->next;

printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;

current_ptr = head_ptr;

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

printf("%d \t %s \t %d \t %d \t %d \t %d \n", current_ptr->empno,

current_ptr->name, current_ptr->bpay, current_ptr->allow, current_ptr->ded,

current_ptr->npay) ;

current_ptr=current_ptr->next;

getch() ;

33
SAMPLE PROGRAM :

Enter the number of employees : 2


Enter the employee number : 101
Enter the name : Arun
Enter the basic pay, allowances & deductions : 5000 1000
250 Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500
750 Emp.No. Name Bpay Allow Ded Npay
101 Arun 5000 1000 250 5750
A 102 Babu 7000 1500 750 7750

RESULT :

Thus a C Program Salary slip of employees was executed and the output was obtained

34
EX NO:9 PROGRAM FOR INTERNAL MARKS OF STUDENTS
DATE:

AIM :

To write a C Program to Compute internal marks of students for five different subjects using structures
and functions.

ALGORITHM:

1. Start the program


2. Declare variables
3. Read the number of students .
4. Read the student mark details
5. Calculate internal mark by i=total of three test marks / 3 for each subject per
student.
6. Display the output of the calculations for all the students .
7. Stop the program

35
PROGRAM:

#include<stdio.h>
#include<conio.h>
struct stud{
char name[20];
long int rollno;
int marks[5,3];
int i[5];
}students[10];
void calcinternal(int);
int main(){
int a,b,j,n;
clrscr();
printf("How many students : \n");
scanf("%d",&n); for(a=0;a<n;+
+a){
clrscr();
printf("\n\nEnter the details of %d student : ", a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
total=0; for(b=0;b<=4;+
+b){ for(j=0;j<=2;++j){
printf("\n\nEnter the test %d mark of subject-%d : ",j+1, b+1);
scanf("%d", &students[a].marks[b,j]);
}
}
}
calcinternal(n);
for(a=0;a<n;++a)
{ clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);

36
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n ");
for(b=0;b<5;b++){
printf("\n\n\t Subject %d internal \t\t :\t %d", b+1, students[a].i[b]);
}
printf("\n\n \n");
getch();
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a)
{ for(b=0;b<5;b++)
{ total=0; for(j=0;j<=2;+
+j){
total += students[a].marks[b,j];
}
students[a].i[b]=total/3;
}
}
}

37
SAMPLE OUTPUT :

How many students : 1


Enter the details of 1 student :
Enter student 1 Name : H.Xerio
Enter student 1 Roll Number : 536435
Enter the test 1mark of subject-1 : 46
Enter the test 2 mark of subject-1 : 56
Enter the test 3 mark of subject-1 : 76
Enter the test 1 mark of subject-2 : 85
Enter the test 2mark of subject-2 : 75
Enter the test 3mark of subject-2 : 75
Enter the test 1mark of subject-3 : 66
Enter the test 2 mark of subject-3 : 86
Enter the test 3 mark of subject-3 : 70
Enter the test 1 mark of subject-4 : 25
Enter the test 2mark of subject-4 : 35
Enter the test 3mark of subject-4 : 61
Enter the test 1 mark of subject-5 : 45
Enter the test 2mark of subject-5 : 75
Enter the test 3mark of subject-5 : 60
Mark Sheet
Name of Student : H.Xerio Roll No : 536435

subject 1 internal : 59
subject 2 internal : 78
subject 3 internal : 74
subject 4 internal : 40
subject 5 internal : 60

RESULT :
Thus a C Program for Internal marks of students was executed and the output was obtained.

38
EXNO:10
PROGRAM WHICH COPIES ONE FILE TO ANOTHER
DATE:

AIM :

To write a C program to copy the contents of one file to another.

ALGORITHM :

1. Start the program

2. read command line arguments

3. check if no of arguments =3 or not. If not print invalid no arguments

4. open source file in read mode

5. if NULL pointer, then print source file can not be open

6. open destination file in write mode

7. if NULL pointer, then print destination file can not be open

8. read a character from source file and write to destination file until EOF

9. Close source file and destination file

10. Stop the program.

39
PROGRAM :

#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
FILE *ft,*fs;
int c=0;
clrscr();
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
{
printf("Source file opening error\n");
exit(1);
}
else if(ft==NULL)
{
printf("Target file opening error\n");
exit(1);
}
while(!feof(fs))
{
fputc(fgetc(fs),ft);
c++;
}
printf("%d bytes copied from 'a.txt' to 'b.txt'",c);
c=fcloseall();
printf("%d files closed",c);
}

40
SAMPLE OUTPUT :

a.txt

An array is a collection of elements of similar datatypes

57 bytes copied from ‘a.txt’ to ‘b.txt’

2 files closed

RESULT :

Thus the C program To Program which copies one file to another has been executed
successfully.

41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy