PC Lab Manual 2022 Final
PC Lab Manual 2022 Final
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,d;
clrscr();
printf(“Enter any integer:”);
scanf(“%d”, &n); while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
Printf(“sum of individual digits is %d”,sum);
getch();
}
OUTPUT:
Enter any integer: 1234
Sum of individual digits is: 10
RESULT:
Thus the program to find sum of single digit of a number was successfully executed and verified.
1
EX.NO: 2 EXPRESSION EVALUATION
DATE :
AIM:
To write a C program to evaluate the given expression
ALGORITHM:
Step-1 Start the program.
Step-2 Input the values for declared variables
Step-3 Substitute the values in expression and calculate the results.
Step-4 Print the results
Step-5 Stop
PROGRAM:
/* Expression Evaluation*/
#include<stdio.h>
main()
{
int a,b,c,d;
float x,y,z;
printf ("Enter the values for a,b,c,d\n");
scanf("%d%d%d%d", &a,&b,&c,&d);
x = (a * b) – c;
y = (b/c) * a;
z = (a - b) / (c + d)
printf(" The value of x is %f “ ,x );
printf(“The value of y is %f “ ,y );
printf(”The value of z is %f “,z );
}
OUTPUT:
Enter the value for a,b,c,d
a= 5; b = 6; c= 12;d=2
x = 28.000; y =
2.5000 z =
0.0555
RESULT:
Thus the program to evaluate the given expression was performed and executed successfully.
2
EX.NO: 3 DECISION MAKING
DATE:
AIM:
To write a C program to check whether given Number is odd or even.
ALGORITHM:
1: Declare a variable to get a Number
2: Read the input
3: Get the remainder of given number using modulo operator
4: If remainder is 0 prints “Even Number”, else print “Odd Number”.
PROGRAM:
#include<stdio.h>
main()
{
int a,rem;
printf("Enter a Number\n");
scanf("%d",&a);
rem=a%2;
if(rem==0)
printf("The Given Number is Even"); else
printf("The Given Number is Odd");
}
OUTPUT:
Enter a Number 13
The Given Number is Odd
RESULT:
Thus the program to check whether given Number is odd or even was performed and
executed successfully.
3
EX.NO: 4 FIBONACCI SERIES
DATE :
AIM:
To write a C program to generate the first n terms of the sequence.
ALGORITHM
1. Read the number of terms n
2. Initialize a 0, b 1
3. print a and b values
4. for i 3 to n
a. increment the i value
b. c a+b
c. print c value
d. a b
e. b c
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{int a=0,b=1,c,n,i;clrscr();
printf(“Enter no. of
terms:”);scanf(“%d”, &n);
printf(“The Fibonacci
sequenceis:”);
printf(“%d%d”, a,b);
for(i=3;i<=n;i++)
{
c=a+b; printf(“%d”,c);
a=b;
b=c;
}
getch();
}
OUTPUT:
Enter no of items: 5
The Fibonacci sequence is0 1 1 2 3
RESULT
Thus the program for Fibonacci series was successfully executed and verified.
4
EX.NO:5 THE GIVEN YEAR IS LEAP YEAR OR NOT
DATE:
AIM:
To write a C program to check whether the given year is leap year or not.
ALGORITHM:
Step 1: Start the Program
Step 2: Enter the year
Step 3: check the given year is divisible by 4, it denotes leap year.
Step 4: Otherwise it is not leap year.
Step 5: print the result
Step 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter the year\n");
scanf("%d",&year);
if(year%4==0 &&year%100 !=0|| year%400==0)
printf("leap year");
else
printf("not leap year");
getch();
}
OUTPUT:
Enter the year = 2016
Leap year
RESULT:
Thus the C program for the given year is leap year or not was performed and
executed successfully.
5
EX.NO:6 DEMONSTRATE ARITHMETIC OPERATIONS (USING SWITCH…CASE)
DATE:
AIM:
To write a C program for demonstrating arithmetic operations using switch case statement.
ALGORITHM:
Step1: Start the program
Step 2: Display menu showing addition, subtraction, multiplication and division
operation.
Step-3: Get the values for two variables
Step 4: Obtain the choice from the user and accordingly switch over to
particular
block.
Step 5: Display the result.
Step 6: If the user wishes to continue repeat steps
2 and3 Step 7: Stop
PROGRAM:
/* Program to demonstrate arithmetic operations */
#include<stdio.h>
#include<conio.h
> void main()
{
int a, b, c,
n;
clrscr();
printf(“1. Addition\n”);
printf(“2. Subtraction\n”);
printf(“3. Multiplication\n”);
printf(“4. Division\n”);
printf(“5.Square of a
No\n:”);
printf(“0. Exit\n”);
printf(“Enter your choice :
“); scanf(“%d”,&n);
printf(“Enter the two numbers
:”); scanf(“%d%d”,&a,&b);
switch(n)
{
case 1:
c = a + b;
printf(“Addition :%d\n”,c);
break;
case 2:
c = a – b;
printf(“Subtraction
:%d\n”,c); break;
case 3:
c = a * b;
printf(“Multiplication :%d\n”,c);
6
break;
case 4:
c = a / b;
printf(“Division :%d\n”,c);
break;
case 5:
c=a*a;
printf(“Square : %d\n”,a);
case
0:
exit(0)
;
break;
}
getch();
}
OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square
0. Exit
Enter Your Choice : 1
Enter the 2 nos a and
b: 28
Addition : 10.
Enter Your Choice : 2
Enter the 2 nos a and
b: 52
Subtraction : 3.
Enter Your Choice : 3
Enter the 2 nos a and
b: 28
Multiplication : 16.
Enter Your Choice : 4.
Enter the 2 nos a and
b: 84
Division : 2.
Enter your Choice: 5
Enter the number a :
2
Square : 4
Enter Your Choice : 0.
Exit.
RESULT:
7
Thus the C program for demonstrating arithmetic operations using switch case
statement was performed and executed successfully.
8
RESULT:
Thus the program to check whether the given number is present or not was performed
and executed successfully.
10
RESULT:
Thus the program to sort the numbers based on weight in the increasing order was
performed and executed successfully.
EX.NO:9 HEIGHT AND WEIGHT OF THE PERSONS
DATE:
AIM:
To write a C program to check the numbers of persons height, weight and average height
ALGORITHM:
Step-1 Take the height of a person as input and store it in the variable height.
Step-2 If the variable height is lesser than 150 cm, then print the output as “Dwarf”.
Step-3 If the variable height is lesser than or equal to 165 cm and greater than or equal to
150 cm,
then print the output as “Average Height”.
Step-4 If the variable height is lesser than or equal to 195 cm and greater than 165 cm,
then print the
output as “Taller”.
Step-5 Stop.
PROGRAM :
#include < stdio.h>
void main()
{
float height;
printf(“Enter the height (in centimeters) \n”);
scanf(“%f”,&height );
if (height < 150.0)
printf(‘Dwarf\n”);
else if ((height>=150.0)&& (height <=165.0)
printf(“average height \n”);
else if ((height > 165.0) && (height <=195.0))
printf(“Taller \n”);
else
printf(“Abnormal height “);
}
OUTPUT:
Enter the Height (in centimetres)
165
Average Height
Enter the Height (in centimetres)
140
Dwarf
11
Enter the Height (in centimetres)
190
Taller
RESULT:
Thus the C program to check the numbers of persons height, weight and average
height was performed and executed successfully.
RESULT:
Thus the C program for student internal marks was executed successfully.
EX.NO:20 TELEPHONE DIRECTORY
DATE:
AIM:
To write a C program for telephone directory to perform insert, delete and append
operations.
ALGORITHM:
Step-1: Read the record number,name and age of a person
Step-2: Insert the biodata in to the file
Step-3: Display the file
Step-4:Stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct biodata{
int recno,age;
char name[20],sex;
float salary;
};
void main()
{
void addData(void);
void delData(void);
void showAll(void);
void showRecord(void);
void alterData(void);
char choice;
clrscr();
while(1){
clrscr();
17
textcolor(BLACK);
cprintf(" B I O - D A T A\r\n");
printf("\n\n*****CHOOSE YOUR CHOICE*****\n");
printf("1) ADD DATA\n");
printf("2) DELETE DATA\n");
printf("3) SHOW ALL\n");
printf("4) SHOW RECORD\n");
printf("5) ALTER DATA\n");
printf("6) Exit \n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call add data
addData();
break;
case'2' : //call delete databreak;
case'3' : //call show all data
showAll();
break;
case'4' : //call show record
showRecord();
break;
case'5' : //call alter databreak;
case'6' :
case 27 : clrscr();
gotoxy(25,10);
_setcursortype(_NOCURSOR);
textcolor(LIGHTMAGENTA);
cprintf("THANKS FOR USING THIS SOFTWARE");
getch();
exit(1);
}
}
}
//Adding Record to Filevoid addData(){
FILE *fp;
struct biodata obj;
fp = fopen("biodata.txt","a+t");
clrscr();
printf("\n*****ADDING DATA*****\n");
printf("\nEnter Record No : ");
scanf("%d",&obj.recno);
printf("Enter Name : ");
fflush(stdin);
scanf("%s",obj.name);
printf("Enter age : ");
scanf("%d",&obj.age);
fflush(stdin);
printf("Enter Sex : ");
scanf("%c",&obj.sex);
printf("Enter Salary : ");
scanf("%f",&obj.salary);
18
fscanf(stdin,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(fp,"%d %s %d %c %f",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
fclose(fp);
}
void showRecord(){
FILE *fp;
struct biodata obj;
int rec;
long pos;
fp = fopen("biodata.txt","r");
clrscr();
printf("\n*****SHOWING SPECIFIC RECORD*****\n");
printf("\nEnter Record No : ");
scanf("%d",&rec);
pos = rec * sizeof(obj);
fseek(fp,pos,SEEK_SET);
if(feof(fp)==0)
printf("\n\nNO DATA FOUND\n");
else{
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
printf("\n\n\tRecord No : %d\n",obj.recno);
printf("\tName : %s\n",obj.name);
printf("\tAge : %d\n",obj.age);
printf("\tSex : %c\n",obj.sex);
printf("\tSalary : %f\n",obj.salary);
}
getch();
fclose(fp);
}
void showAll(){
FILE *fp;
struct biodata obj;
int totrec,i;
fp = fopen("biodata.txt","r");
clrscr();
fseek(fp,0,SEEK_END);
totrec=ftell(fp)/sizeof(obj);
printf("\n*****SHOWING ALL RECORD*****\n");
printf("\n\nRecord_No\tName\t\tAge\tSex\tSalary\n\n");
printf("\n\n%d\n",totrec);
for(i=1;i<=totrec;i++){
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(stdout,"%-15d %-15s %-8d %-2c %10.2f\n",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
}
getch();
fclose(fp);
}
OUTPUT:
BIO-DATA
CHOOSE YOUR CHOICE
("1) ADD DATA\n");
19
("2) DELETE DATA\n");
("3) SHOW ALL\n");
("4) SHOW RECORD\n");
("5) Exit
1
("\n*****ADDING DATA*****\n");
("\nEnter Record No : ");
234
("Enter Name : ");
Raja
("Enter age : ");
35
("Enter Sex : ");
F
("Enter Salary : ");
35000
RESULT:
Thus the program for telephone details was executed successfully.
EX.NO:21 BANK ACCOUNT DETAILS
DATE:
AIM:
To write a C program to count the number of account holders whose balance is less than
the minimum balance using sequential access file.
ALGORITHM:
Step-1: Read the account holder’s name
Step-2: Check the account holder’s balance
Step-4: Whether the balance is low means display the account holders name.
Step-5.Stop
PROGRAM:
\* Program in C to show the bank operation using structure with array and Function. *\
# include < stdio.h >
# include < conio.h >
void creation( ) ;
void deposit( ) ;
void withdraw( ) ;
void lowbal( ) ;
int a = 0 , i = 1001 ;
struct bank
{
int no ;
char name[20] ;
float bal ;
float dep ;
} s[100];
int main( )
{
20
int ch ;
do
{
printf(" \n**************************** ") ;
printf(" \nBANKING ") ;
printf(" \n**************************** ") ;
printf(" \n1. Create New Account ") ;
printf(" \n2. Cash Deposit ") ;
printf(" \n3. Cash Withdraw ") ;
printf(" \n4. Low Balance Enquiry ") ;
printf(" \n5. Exit ") ;
printf(" \nEnter your choice : ") ;
scanf("%d ",& ch) ;
switch ( ch)
{
case 1 : creation( ) ;
break ;
case 2 : deposit( ) ;
break ;
case 3 : withdraw( ) ;
break ;
case 4 : lowbal( ) ;
break ;
case 5 : ;
break ;
defalut : printf(" Choice a Valid option !! ") ;
break ;
getch( ) ;
}
} while( ch != 5 ) ;
}
void creation( )
{
printf(" \n**************************** ") ;
printf(" \nNEW ACCOUNT CREATION ") ;
printf(" \n**************************** ") ;
printf(" \nYour Account Number is :%d ",i) ;
s[a].no = i ;
printf(" \nEnter your Name : ") ;
scanf("%s ",& s[a].name) ;
printf(" \nYour Deposit is Minimum Rs.500") ;
s[a].dep=500 ;a++ ;
i++ ;
getch( ) ;
}
void deposit( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH DEPOSIT ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
21
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nDeposited Amount : ") ;
scanf("%f ",& &aa) ;
s[m].dep+=aa ;
printf("\nThe Balance in Account is : %f ",s[m].dep) ;
getch( ) ;
}
else
{
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
}
void withdraw( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH WITHDRAW ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nWithdraw Amount : ") ;
scanf("%f ",& aa) ;
if ( s[m].dep < aa+500)
{
printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM BALANCE ") ;
getch( ) ;
}
else
{
s[m].dep-=aa ;
printf("\nThe Balance Amount in Account is : %f ",s[m].dep) ;
}
22
}
else
{
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
getch( ) ;
}
void lowbal( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nFOLLOWING ACCOUNT HOLDER'S BALANCE IS LESS THAN 1000 ") ;
printf(" \n**************************** ") ;
for ( b = 0 ; b < a ; b++)
{
if ( s[b].dep < 1000))
{
printf("\n\n Account Number : %d ",s[b].no) ;
printf("\n Name : %s ",s[b].name) ;
}
}
getch( ) ;
}
OUTPUT:
23
RESULT:
Thus the C program for account holder’s minimum balance was checked and executed
successfully.
1.Add Details :
2.Ticket number id (Check details):
3.Ticket number (Check Status):
4.Exit :
Enter ur choice :
2
Enter ticket Id :
11
================== Customers Record ==================
Ticket id : 11
Name : local
Train name : oliver
Seat number : 12
29
Date (dd/mm/yy) : 12 / 6 / 2016
Depature time (hour/minute) : 2 : 50
Confirmation Status : Waiting
RESULT:
Thus the program for railway reservation system was executed successfully.
EX.NO:23 SINGLY LINKED LIST EXAMPLE PROGRAM ( INSERT, DELETE, DISPLAY AND COUNT)
DATE:
AIM:
Write a c program to implement singly Linked List using Functions.
30
ALGORITHM:
1. Singly Linked List Example - All Operations Example Program Using Functions in C
2. Data Structure Programs,
3. C Linked List Examples
4. Singly Linked List Example - Insert,Delete,Display and Count Operations
PROGRAM
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main() {
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
31
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
32
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue) {
if (pos == 1) {
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
} else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
} else {
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
} else
33
printf("\nInvalid Position \n\n");
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
Sample Output:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
34
Enter Element for Insert Linked List :
100
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
200
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
300
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
400
Options
1 : Insert into Linked List
2 : Delete from Linked List
35
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
500
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 300 # # 400 # # 500 #
No Of Items In Linked List : 5
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:4
No Of Items In Linked List : 5
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
36
No Of Items In Linked List : 5
Display Linked List :
Enter Position for Delete Element :
3
Deleted Successfully
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 400 # # 500 #
No Of Items In Linked List : 4
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
No Of Items In Linked List : 4
Display Linked List :
Enter Position for Delete Element :
6
Invalid Position
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
37
Others : Exit()
Enter your option:
5
(program exited with code: 0)
Press any key to continue . . .
RESULT:
Thus the C program was successfully executed and verified.
EX.NO:24 FIND THE GCD OF TWO NUMBERS USING RECURSIVE AND NON RECURSIVE
FUNCTIONS
DATE:
AIM:
To write a C program to find the GCD(greatest common divisor) of two given integers by
using recursive and Non-recursive functions.
38
RECURSIVE ALGORITHM:
1. Define the recursive function
2. Read the a,b values
3. Call the recursive function
4. if n>m
5. then return the function with parameters m,n
6. if n==0
7. then return m
8. else return the function with parameters n,m%n.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned int GCDRecursive(unsigned m, unsigned n);
int main(void)
{
int a,b; clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is %d\n",a,b,GCDRecursive(a,b));
getch();
}
/* Recursive Function*/
unsigned int GCDRecursive(unsigned m, unsigned n)
{
if(n>m)
return GCDRecursive(n,m);
if(n==0)
return m;
else
return GCDRecursive(n,m%n);
}
OUTPUT:
Enter the two numbers whose GCD is to be found 18 6 GCD of 18 and 6 Using Recursive Function
is 6
NON-RECURSIVE ALGORITHM:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
39
Sub program: GCD(a,b)
Step 1: initialize the p=1, q, remainder
Step 2: remainder=p-(p/q*q)
Step 3: remainder=0 return q else goto step 4
Step 4: GCD(q,remainder) return to main program
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int gcdnonrecursive(int m,int n)
{
int remainder; remainder=m-(m/n*n);
if(remainder==0) return n;
else gcdnonrecursive(n,remainder);
}
void main()
{
int a,b,igcd; clrscr();
printf("enter the two numbers whose gcd is to be found:");
scanf("%d%d",&a,&b);
printf("GCD of %d",gcdnonrecursive(a,b));
getch();
}
OUTPUT:
Enter the two numbers whose gcd is to be found:5,25 GCD of a,b is : 5
Enter the two numbers whose gcd is to be found:36,54 GCD of a,b is : 18
RESULT:
Thus the C program was successfully executed and verified.
EX.NO:25 FIND THE FACTORIAL OF A GIVEN INTEGER USING RECURSIVE AND NON
RECURSIVE FUNCTION
DATE:
AIM:
To write a C program to find the factorial of a given integer by using recursive and non-
recursive functions.
40
1. Recursive Algorithm:
2. Define the recursive function
3. Read the number n
4. if n is equal to 0
5. then print “factorial of 0 is 1”
6. else call the recursive function
7. print the factorial value.
PROGRAM:
#include<stdio.h> #include<conio.h>
unsigned int factorial(int n); void main()
{
int n,i;
long int fact; clrscr();
printf("Enter the number: "); scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1\n"); else
printf("Factorial of %d Using Recursive Function is %d\n",n,factorial(n));
getch();
}
/* Recursive Function*/ unsigned int factorial(int n)
{
return n>=1 ? n * factorial(n-1) : 1;
}
OUTPUT:
Enter number: 5
Factorial of 5 using recursive function is: 120
NON-RECURSIVE ALGORITHM: MAIN PROGRAM
Step 1: start
Step 2: read n
Step 3: call the sub program fact(n)
Step 4: print the f value
Step 5: stop
RESULT:
Thus the C program was successfully executed and verified.
45
46
RESULT:
Thus the C program was successfully executed and verified.
PROGRAM:
#include <stdio.h>
int main()
{
int a, b, c, d, mat[10][10], trans[10][10]; //declaration of variable
printf(" Enter the number of rows and columns of matrix: ");
scanf("%d%d",&a,&b); // inputting order of matrix
printf(" Enter the elements of matrix \n");
for( c = 0 ; c < a ; c++ )// loop to input the matrix
{
for( d = 0 ; d < b ; d++ )
47
{
scanf("%d",&mat[c][d]);
}
}
for( c = 0 ; c < a ; c++ ) // loop to transpose the matrix
{
for( d = 0 ; d < b ; d++ )
{
trans[d][c] = mat[c][d];
}
}
printf(" The traspose of entered matrix is:-\n");
for( c = 0 ; c < b ; c++ ) // loop for printing transposed matrix
{
for( d = 0 ; d < a ; d++ )
{
printf("%d\t",trans[c][d]);
}
printf("\n");
}
return 0;
}
OUTPUT:
48
RESULT:
Thus the C program was successfully executed and the output is verified.
PROGRAM:
#include<stdio.h
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,imp,rp;
clrscr();
49
printf(“Enter a,b,c:”);
scanf(“%f%f%f”,&a,&b,&
c);d=b*b-4.0*a*c;
if(d= =0)
{
Printf(“roots are real and equal”);
r1=-b/2*a;
r2=r1; printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d>0)
{
Printf(“roots are real and unequal”);
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d<0)
{
d=-d;
printf(“roots are complex”);
rp=-b/2*a;
imp=sqrt(d)/2*a;
printf(“root1=%f+i%f”,rp,imp);
printf(“root2=%f-i%f”,rp,imp);
}
getch();
}
OUTPUT:
Enter a,b & c: 1 5 3
Roots are real &
unequal
50
RESULT:
Thus the C program was successfully executed and verified.
AIM:
To write a C program to swap two numbers using call by reference.
ALGORITHM:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(&a,&b)
Step 3a: Start fuction
Step 3b: Assign t ← *x
Step 3c: Assign *x ← *y
Step 3d: Assign *y ← t
Step 3e: End function
Step 4: Print x and y.
Step 5: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void swap(int *,int *); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf( "n a = %d b = %d", a, b );
swap(&a,&b); // call by reference
51
printf("n After swapping");
printf( "n a = %d b = %d", a, b );
getch();
}
void swap( int *x, int *y )
{
int t;
t = *x;
*x = *y;
*y = t;
}
OUTPUT:
Before swapping a=10 b=20
After swapping a=20 b=10
RESULT:
Thus the C program to swap two numbers using call by reference is written and
executed successfully.
OUTPUT:
File created is passed as parameter: File is copied
RESULT:
Thus the C Program was successfully executed and output is verified.
55