c program lab final (1)
c program lab final (1)
c program lab final (1)
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:
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:
ALGORITHM:
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:
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:
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:
9
EX NO:3(a) PROGRAM TO FIND THE GIVEN NUMBER IS ARMSTRONG
DATE: OR NOT
AIM:
ALGORITHM:
6. If sum==temp
Else
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:
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:
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:
5. Initialize the large and small is equal to the first element of the array
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
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:
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:
ALGORITHM:
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
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:
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:
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 :
ALGORITHM :
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 :
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:
2. Declare variables
28
PROGRAM:
#include <stdio.h>
#include <conio.h>
if (n == 1)
return;
int main()
int n;
clrscr();
getch();
return 0;
29
SAMPLE OUTPUT :
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:
pointers.
ALGORITHM:
2. Declare variables
6. Display the output of the Pay slip calculations for each employee.
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;
clrscr() ;
current_ptr = head_ptr;
while (more_data)
scanf("%s",& current_ptr->name) ;
32
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
n++;
scanf("%s", answer);
if (answer!= 'Y')
more_data = 0;
else
current_ptr = current_ptr->next;
current_ptr = head_ptr;
current_ptr->npay) ;
current_ptr=current_ptr->next;
getch() ;
33
SAMPLE PROGRAM :
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:
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 :
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 :
ALGORITHM :
8. read a character from source file and write to destination file until EOF
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
2 files closed
RESULT :
Thus the C program To Program which copies one file to another has been executed
successfully.
41