Uploaded by Pavethra Manivel

CS3271 - RECORD FOR CSE

advertisement
CS3271 / PROGRAMMING IN C LABORATORY
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
CS 3271 / PROGRAMMING IN C LABORATORY (R-2021)
I YEAR / II SEMESTER
NAME :
REGISTER NO:
SRI VENKATESWARAA COLLEGE OF
TECHNOLOGY
Vadakkal, Mathur Post, Sriperumbudur 602105
BONAFIDE CERTIFICATE
Certified
that
this
is
the
bonafide
record
work
of
Register No.
for Second Semester, B.E./B.Tech., Programming in C Laboratory
(CS3271) during theacademic year 2022-2023.
STAFF INCHARGE
HEAD OF THE DEPARTMENT
Submitted for the practical examination of Bachelor of Engineering
held at SRI VENKATESWARAA COLLEGE OF TECHNOLOGY on
INTERNAL EXAMINER
EXTERNAL EXAMINER
INDEX
S.No
1a
1b
1c
1d
1e
1f
1g
2a
2b
2c
2d
2e
2f
2g
2h
2i
2j
2k
3a
3b
3c
3d
3e
3f
3g
3h
4a
4b
4c
4d
5a
5b
5c
5d
5e
6a
6b
Date
Topic
Page.No
Programs using I/O statements, Expressions and Operators
Area and Circumference of a circle
1
Swapping two numbers
2
Swapping two numbers without using third
3
variable
Size of all Data Types
4
Biggest of two numbers using Conditional operator
5
Square root of a given number
6
Curved Surface of a Cylinder
7
Programs using Decision Making Statement
Display Maximum number
8
Consonant or Vowel
9
Eligible for voting
11
Display grade of a student
12
Odd or even number
13
Bonus of an employee
14
Biggest of three numbers
16
Prime number
17
Leap Year
18
Positive number or negative number using switch
19
case
Odd or even, Positive or negative using if-else
20
Programs using Looping
Print Positive numbers
21
Palindrome number
23
Fibonacci series
24
Factorial of a given number
25
Pattern matching
26
Sum of first and last digit
27
Pascal Triangle
28
Armstrong Number
29
Programs using Array
Printing array elements
30
Matrix Addition
31
Matrix multiplication
33
Transpose of a matrix
35
Programs using Strings
Concatenation of a string
37
Comparing two strings
38
Copying String
39
String Palindrome
40
Sorting Strings
41
Programs using Function
Mathematical operations using defined function
43
Swapping two numbers using call by value
45
Marks
Staff
Sign
6c
6d
7a
7b
7c
8a
8b
8c
8d
8e
9a
9b
9c
10a
10b
10c
10d
10e
Swapping two numbers using call by reference
Prime, Armstrong or perfect number using
Function
Programs using Recursive Function
Fibonacci series
Power of any number
Factorial of a given number
Programs using Pointers
Biggest among three numbers using pointers
Compare two strings using pointers
Sum of array elements using pointers
Search an element in array using pointers
Program for pointer and pointer to pointer
conversion
Programs using Structure and Union
Employee payroll application using structure
Displaying student average using union
Student details using structure
Programs using File Handling
Store employee information of a file
Display directories and sub-directories
Reading a file stored on the disk
Reverse the contents of the file
Copy contents of one file to another file
46
47
49
50
51
52
53
54
55
57
58
60
62
64
66
67
69
71
Ex.No:1a
AREA AND CIRCUMFERENCE OF A CIRCLE.
Aim:
To write a C program to find the area and circumference of a circle.
Algorithm:
1.
2.
3.
4.
Start the program.
Include the header files.
Get radius of the circle as input.
Using the formula,
a. area = 3.14 * radius * radius
b. circum = 2 * 3.14 * radius
5. Display the result.
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float radius,area,circum;
clrscr();
printf("***** Program to calculate the area and circumference of circle *****\n");
printf("Enter the radius:\n");
scanf("%f",&radius);
area=3.14*radius*radius;
circum=2*3.14*radius;
printf("The area of circle is %f:\n",area);
printf("The circumference of circle is %f:\n",circum);
getch();
}
Output:
***** Program to find the area and circumference of the circle *****
Enter the radius:
4
The area of circle is:
50.24
The circumference of circle is:
25.12
Result:
Thus to write a C program to calculate the area and circumference of a circle was successfully
executed and verified.
1
Ex.No:1b
SWAPPING TWO NUMBERS
Aim:
To write a C program to swap two numbers using third variable.
Algorithm:
1. Start the program
2. Get values for a and b.
3. Declare a third variable temp.
4. Use the following instructions to swap numbers
a. temp=a
b. a=b
c. b=temp
5. Display the result.
6. Stop the program
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter values for a and b:\n");
scanf("%d\n%d",&a,&b);
printf("Before Swapping values:\n");
printf("A=%d\n",a);
printf("B=%d\n",b);
temp=a;
a=b;
b=temp;
printf("After Swapping values:\n");
printf("A=%d\n",a);
printf("B=%d\n",b);
getch();
}
Output:
Enter values for a and b:
23
45
Before Swapping values:
A=23
B=45
After Swapping values:
A=45
B=23
Result:
Thus the C program to swap two numbers was successfully executed and verified.
2
Ex.No:1c
SWAPPING TWO NUMBERS WITHOUT USING THIRD VARIABLE
Aim:
To write a C program to swap two numbers without using third variable.
Algorithm:
1. Start the program.
2. Get the values of a and b.
3. Perform the following operations to swap two numbers.
a=a+b
b=a-b
a=a-b
4. Display the values of a and b after swapping.
5. Stop the program,
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the values for a and b:\n");
scanf("%d%d",&a,&b);
printf("Before Swapping:\n");
printf("A=%d\n",a);
printf("B=%d\n",b);
a=a+b;
b=a-b;
a=a-b;
printf("After Swapping:\n");
printf("A=%d\n",a);
printf("B=%d\n",b);
getch();
}
Output:
Enter the values for a and b:
34
90
Before Swapping:
A=34
B=90
After Swapping:
A=90
B=34
Result:
Thus the C program to perform swapping of two numbers without using third variable is
successfully executed and verified.
3
Ex.No: 1d
SIZE OF ALL DATA TYPES
Aim:
To write a C program to display size of all data types.
Algorithm:
1. Start the program.
2. Use sizeof operator to determine the size of different data types.
3. Display the results using printf ( ) statement.
4. Stop the program.
Program:
#include <stdio.h>
#include <conio.h>
int main( ) {
printf(" short int is %2d bytes \n", sizeof(short int));
printf("
int is %2d bytes \n", sizeof(int));
printf("
int * is %2d bytes \n", sizeof(int *));
printf(" long int is %2d bytes \n", sizeof(long int));
printf(" long int * is %2d bytes \n", sizeof(long int *));
printf(" signed int is %2d bytes \n", sizeof(signed int));
printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n");
printf("
float is %2d bytes \n", sizeof(float));
printf(" float * is %2d bytes \n", sizeof(float *));
printf("
double is %2d bytes \n", sizeof(double));
printf(" double * is %2d bytes \n", sizeof(double *));
printf(" long double is %2d bytes \n", sizeof(long double));
printf("\n");
printf(" signed char is %2d bytes \n", sizeof(signed char));
printf("
char is %2d bytes \n", sizeof(char));
printf("
char * is %2d bytes \n", sizeof(char *));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
}
Output:
short int is 2 bytes
int is 4 bytes
int * is 8 bytes
long int is 4 bytes
long int * is 8 bytes
signed int is 4 bytes
unsigned int is 4 bytes
float is 4 bytes
float * is 8 bytes
double is 8 bytes
double * is 8 bytes
long double is 16 bytes
signed char is 1 bytes
char is 1 bytes
char * is 8 bytes
unsigned char is 1 bytes
Result:
Thus the C program to find the size of all data types was successfully executed and verified.
4
Ex.No: 1e
BIGGEST OF TWO NUMBERS USING CONDITIONAL OPERATOR
Aim:
To write a C program to find the biggest of two numbers using conditional operator.
Algorithm:
1. Start the program.
2. Get the values of and b.
3. Use the following instruction to find biggest among two numbers.
a. big = a>b?a:b
4. Display the result.
5. Stop the program
Program:
#include<stdio.h>
int main()
{
int a,b,big;
printf("Enter the values for a and b:\n");
scanf("%d%d",&a,&b);
big=a>b?a:b;
printf("The greatest of two numbers is %d",big);
return 0;
}
Output:
45
56
The greatest of two numbers is 56
Result:
Thus the C program to find the biggest among two numbers using conditional operator was
successfully executed and verified.
5
Ex.No: 1f
SQUARE ROOT OF A GIVEN NUMBER
Aim:
To write a C program to determine the square root of a given number.
Algorithm:
1. Start the program
2. Get the value of x.
3. Use predefined function sqrt to determine the square root of the given input „x‟.
4. Store the result in another variable „y‟.
5. Display the value of y.
6. Stop the program.
Program:
#include<stdio.h>
#include<math.h>
int main( )
{
int x;
float y;
printf("Enter the value of x:");
scanf("%d",&x);
y=sqrt(x);
printf("The square root of the given number is %0.2
f",y);
}
Output:
Enter the value of x:5
The square root of the given number is 2.24
Result: Thus the C program to determine square root of a given number was successfully executed and
verified.
6
Ex.No: 1g
CURVED SURFACE OF A CYLINDER
Aim:
To write a C program to calculate curved surface of a cylinder.
Algorithm:
1. Start the program.
2. Get the values of radius and height
3. Use the following formula calculate the curved surface of a cylinder
surface_area= (2*22*(r+h)/7)
4. Display the result.
5. Stop the program.
Program:
#include<stdio.h>
int main()
{
float r, h, surfacearea;
r=2.0;
h = 5.0;
surfacearea = (2*22*(r + h))/7;
printf("\n\n Surface Area of Cylinder is : %f", surfacearea);
return (0);
}
Output:
Surface Area of Cylinder is : 44.000000
Result:
Thus the C program to calculate surface area of a cylinder was successfully executed and verified.
7
Ex.No: 2a
DISPLAY MAXIMUM NUMBER
Aim:
To write a C program to display maximum number.
Algorithm:
1. Start the program.
2. Input two variables a and b.
3. Using if construct check if (a>b).
4. If the condition is true, display A is greater.
5. If the condition is false, display B is greater.
6. Stop the program.
Program
#include<stdio.h>
int main()
{
int a,b;
clrscr();
printf("Enter values for a and b:\n");
scanf("%d\n%d",&a,&b);
if(a>b)
{
printf("%d is greater",a);
}
else
{
printf("%d is greater",b);
}
return 0;
}
Output:
Enter values for a and b:
34
23
34 is greater
Result:
Thus the C program to find the biggest among two numbers using if condition was successfully
executed and verified.
8
Ex.No: 2b
CONSONANT OR VOWEL
Aim:
To write a C program to check whether a character is consonant or vowel.
Algorithm:
1. Start the program.
2. Get the character as input.
3. Use switch case, check whether the character is „a‟, „e‟, „i‟, „o‟, „u‟. „A‟, „E‟, „I‟, „O‟, „U‟.
4. If yes, print the result as “Given character is vowel” else print the result as “Given character is
consonant.
5. Stop the program
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
/* Input an alphabet from user */
printf("Enter any alphabet: ");
scanf("%c", &ch);
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
9
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
getch();
}
Output:
Enter any alphabet: d
Consonant
Enter any alphabet: e
Vowel
Result:
Thus the C program to check whether a character is vowel or consonant was successfully
executed and verified.
10
Ex.No: 2c
ELIGIBLE FOR VOTING
Aim:
To write a C program to check whether the given age is eligible for voting or not.
Algorithm:
1. Start the program.
2. Get the age as input.
3. Check using if construct whether age>18.
4. If the condition is true, print the result as “Eligible for voting”
5. If the condition is false, print the result as Not eligible for voting.
6. Display the result and Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int age;
printf("Enter the age:\n");
scanf("%d",&age);
if (age>18)
{
printf("Eligible for voting\n");
}
else
{
printf("Not Eligible for voting\n");
}
return 0;
}
Output:
Enter the age:
34
Eligible for voting
Enter the age:
12
Not Eligible for voting
Result:
Thus the C program to check whether the given age is eligible for voting was successfully
executed and verified.
11
Ex.No:2d
DISPLAY GRADE OF A STUDENT
Aim:
To write a C program to display the grade of a student.
Algorithm:
1. Start the program.
2. Get the average marks as input.
3. Declare the variable grade.
4. Using if-elseif ladder check the condition based on average.
5. If avg >=91 and avg <=100 display the result as O grade.
6. If avg >=81 and avg <=90 display the result as A grade.
7. If avg >=71 and avg <=80 display the result as B grade.
8. If avg >=61 and avg <=70 display the result as B grade.
9. If avg >=51 and avg <=60 display the result as D grade.
10. Else display the result as failed.
11. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int avg;
char grade;
printf("Enter the average:\n");
scanf("%d",&avg);
if((avg>=91)&&(avg<=100))
printf("O Grade");
else if((avg>=81)&&(avg<=90))
printf("A Grade");
else if((avg>=71)&&(avg<=80))
printf("B Grade");
else if((avg>=61)&&(avg<=70))
printf("C Grade");
else if((avg>=50)&&(avg<=60))
printf("D Grade");
else
printf("Failed");
return 0;
}
Output:
Enter the average:
67
C Grade
Result:
Thus the C program to display the grade of a student using if-else ladder was successfully
executed and verified.
12
Ex.No: 2e
ODD OR EVEN
Aim:
To write a C program to determine the given number is even or odd.
Algorithm:
1. Start the program
2. Get the input n.
3. Check the condition using if construct
a. If n%2==0, then display the result as n is even number
b. Else display the result as n is odd number
4. Stop the program.
Program:
#include<stdio.h>
int main()
{
int n;
printf("Enter the value for n:");
scanf("%d",&n);
if(n%2 ==0)
{
printf("%d is even",n);
}
else
{
printf("%d is odd",n);
}
}
Output
Enter the value for n:34
34 is even
Result:
Thus the C program to determine whether the given number is odd or even was successfully
executed and verified.
13
Ex.No: 2f
BONUS OF AN EMPLOYEE
Aim:
To write a C program to calculate bonus of an employee
Algorithm:
1.
2.
3.
4.
Start the program.
Declare the required variables salary, bonus and gender.
Get the input values for salary and gender.
Check the following conditions to calculate the bonus
a. If gender is male and if salary is greater than 10000, then bonus = 5 percent of salary.
b. If gender is male and if salary is lesser than 10000, then bonus=7 percent of salary.
c. If gender is female and if salary is greater than 10000, then bonus = 10 percent of salary.
d. If gender is female and if salary is lesser than 10000, then bonus =12 percent of salary.
5. Display the result based on the estimation of the above condition.
6. Stop the program.
Program:
#include<stdio.h>
#include<string.h>
main()
{
int i,j;
float salary,bonus;
char gender;
printf("Enter M for Male and F for Female\n");
scanf("%c",&gender);
printf("Enter Salary\n");
scanf("%f",&salary);
if(gender=='M' || gender=='m')
{
if(salary>10000)
bonus=(float)(salary*0.05);
else
bonus=(float)(salary*0.07);
}
if(gender=='F' || gender=='f')
{
if(salary>10000)
bonus=(float)(salary*0.1);
else
bonus=(float)(salary*0.12);
}
salary+=bonus;
printf("Bonus=%.2f\nSalary=%.2f\n",bonus,salary);
}
14
Output:
Enter M for Male and F for Female
m
Enter Salary
10000
Bonus=700.00
Salary=10700.00
Result:
Thus the program to display the grade of a student was successfully executed and verified.
15
Ex.No: 2g
BIGGEST OF THREE NUMBERS
Aim:
To write a C program to find the biggest of three numbers.
Algorithm:
1. Start the program.
2. Declare the variables a,b and c.
3. Get the input for the above variables.
4. Check the condition using if construct
a. If a>b and a>c , display the result as “A is greater”.
b. If b>a and b>c, display the result as “B is greater”
c. Else Display C is greater
5. Stop the program.
Program:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter values for a b and c:\n");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("A is greater");
}
else if((b>a)&&(b>c))
{
printf("B is greater");
}
else
{
printf("C is greater");
}
}
Output:
Enter values for a b and c:
23
56
12
B is greater
Result:
Thus the C program to find the greatest of three numbers was successfully executed and verified.
16
Ex.No: 2h
PRIME NUMBER
Aim:
To write a C program to find the given number is prime number or not.
Algorithm:
1. Start the program.
2. Declare the variable n and get the value for n.
3. Determine the value of m using the formula m=n/2.
4. Iterate the for loop till the value of m
5. Check the condition n%i==0, display the result as given number is prime.
6. Else display the result as give number is not prime.
7. Stop the program.
Program:
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
Output
Enter the number to check prime:17
Number is prime
Result:
Thus the C program to determine the given number is prime or not was successfully executed and
verified.
17
Ex.No:2i
LEAP YEAR
Aim:
To write a C program find the given year is a leap year or not.
Algorithm:
1. Start the program.
2. Get the input year.
3. Check the following condition.
4. If the condition is true, the given year is a leap year.
if(year%4= = 0) && (year %100= = 0) && if(year %400 = = 0)
5. Display the result.
6. Stop the program.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
clrscr();
printf("******* Program to find Leap year *******\n");
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 = = 0)
{
if( year%100 = = 0)
{
if ( year%400 = = 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}
Output:
Enter a year: 2000
2000 is a leap year
Result:
Thus the C program to determine the given year is leap year or not was successfully executed and
verified.
18
Ex.No: 2j
POSITIVE NUMBER OR NEGATIVE USING SWITCH CASE
Aim:
To write a C program to determine a given number is positive or negative using switch case
statement.
Algorithm:
1. Start the program
2. Declare the necessary variables.
3. Using switch statement, check the following conditions:
a. Check num>=0
4. Use another switch statement to check the following:
a. Then check num ==0, then display the given number is zero.
b. Else display the given number is positive.
5. Display the result as given number is negative if the condition num>=0 fails.
6. Stop the program.
Program:
#include<stdio.h>
int main() {
int num;
printf("Enter a Number : ");
scanf("%d", &num);
switch (num >= 0)
{
case 1:
switch (num == 0)
{
case 1:
// if 'num == 0', then 'num' is zero. Otherwise positive number
printf("Number is Zero \n");
break;
// We can also have a 'case 0:', but let's use 'default' case
default:
printf("%d number is Positive\n", num);
break;
}
break;
case 0:
printf("%d number is Negative\n", num);
break;
}
return 0;
}
Output:
Enter a Number : 34
34 number is Positive
Result:
Thus the C program to determine whether a number is positive or negative number was
successfully executed and verified.
19
Ex.No:2k
ODD OR EVEN, POSITIVE OR NEGATIVE USING IF STATEMENT
Aim:
To write a C program to determine a given number is odd, even, positive or negative number
using if statement.
Algorithm:
1. Start the program.
2. Declare the necessary variable.
3. Initially check the condition num ==0.
4. If the condition is true, display the result as Number is zero.
5. If the condition is false, check if num>0 and num%2==0.
6. Display the result as Number is positive and even number if the above condition is true.
7. If the condition in step 5 is false, then display the result as Number is positive and odd number.
8. If the condition in step 3 is false, display the result as Number is negative.
9. Stop the program.
Program:
#include<stdio.h>
int main()
{
int num;
printf("Enter any Number : ");
scanf("%d",&num);
if(num == 0)
{
printf("Number is Zero \n");
}
else if((num > 0)&&(num%2==0))
{
printf("Given Number is Positive and Even number \n");
}
else if((num>0)&&(num%2!=0))
{
printf("Given number is Positve and odd number");
}
else
{
printf("Given Number is Negative Number \n");
}
return 0;
}
Output:
Enter any Number : 34
Given Number is Positive and Even number
Enter any Number : 45
Given number is Positve and odd number
Result:
Thus the C program to determine a given number is odd, even, positive or negative number was
successfully executed and verified.
20
Ex.No: 3a
PRINT POSITIVE NUMBERS
Aim:
To write a C program to display numbers from 1 to 10 and in reversed order.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Get the value of n.
4. Use for loop to iterate from1 to n; Use printf statement inside for loop to display the value from 1
to 10.
5. Use for loop to iterate from n, where n=10 to 1; Use printf statement inside for loop to display
values from 10 to 1.
6. Stop the program.
Program:
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the total number of elements:\n");
scanf("%d",&n);
printf("The numbers are:\n");
for(i=1;i<=n;i++)
{
printf("%d\n",i);
}
printf("Printing numbers in reversed order\n");
for(i=10;i>0;i--)
{
printf("%d\n",i);
}
}
Output
Enter the total number of elements:
10
The numbers are:
1
2
3
4
5
6
7
8
9
10
21
Printing numbers in reversed order
10
9
8
7
6
5
4
3
2
1
Result:
Thus the C program to print number from 1 to n and in reversed order was successfully executed
and verified.
22
Ex.No: 3b
PALINDROME NUMBER
Aim:
To write a C program to check whether a number is a palindrome number.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Get the value for n.
4. Assign the value of n to another variable temp.
5. Using while loop check whether n >0
6. If the while loop condition is true, perform the following steps:
a. r=n% 10
b. sum = (sum*10)+r
c. n=n/10
7. Check whether the value of sum and temp is equal.
8. If it is equal, display the result “Given number is palindrome”
9. If it is not equal, display the result “Given number is not palindrome”.
10. Stop the program.
Program:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
printf("The reversed number is %d\n",temp);
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
Output:
enter the number=456
The reversed number is 456
not palindrome
Result:
Thus the C program to determine whether the given number is palindrome or not was executed
successfully and verified.
23
Ex.No: 3c
FIBONACCI SERIES
Aim:
To write a C program to generate Fibonacci Series.
Algorithm:
1. Start the program.
2. Declare the variables first, second, third.
3. Declare the necessary variables.
4. Initialize the variable first=0 and second = 1.
5. Get the input value for the variable limit.
6. Now add the first and second and store the result in the variable third.
7. Repeat Step 6 in a for loop until the condition fails.
8. Now display the Fibonacci series.
9. Stop the Program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int first=0,second=1,third,i,n;
clrscr();
printf("***** Program to display Fibonacci Series*****\n");
printf("Enter the limit:\n");
scanf("%d",&n);
printf("%d\t%d\t",first,second);
for(i=2;i<n;i++)
{
third=first+second;
printf("%d\t",third);
first=second;
second=third;
}
getch();
}
Output:
***** Program to display Fibonacci Series*****
Enter the limit:
5
0
1
1
2
3
Result:
Thus to write a C program to generate Fibonacci Series was successfully executed and verified.
24
Ex.No: 3d
FACTORIAL OF A GIVEN NUMBER
Aim:
To write a C program to find the factorial of a given number.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Get the input value in the variable n.
4. Use for loop to iterate from 1 to n and perform the following instruction.
a. fact=fact *i;
5. Store the result in the variable fact.
6. Display the result and stop the program.
Program:
#include<stdio.h>
int main()
{
int i,fact=1,n;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",n,fact);
return 0;
}
Output
Enter a number: 5
Factorial of 5 is: 120
Result:
Thus the C program to determine the factorial of a given number was successfully executed and
verified.
25
Ex.No:3e
PATTERN PRINTING
Aim:
To write a C program to print half pyramid pattern.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Input the value of row.
4. Use for loop variable i from 1 to row.
5. Use for loop variable j from 1 to i.
6. Print * till the iteration fails.
7. Display the pattern and stop the program.
Program:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 5
*
**
***
****
*****
Result:
Thus the C program to print half pyramid pattern was successfully executed and verified.
26
Ex.No: 3f
SUM OF FIRST AND LAST DIGIT
Aim:
To write a C program to print the sum of first digit and last digit.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Get the input number in the variable num.
4. Determine the last digit of the input number using the formula lastdigit = num %10.
5. Assign the value of num to another variable firstDigit.
6. Use while loop determine the first digit.
7. Check the condition while num>=10 and perform the following statement if the condition is true:
a. num=num/10
8. Again assign the value of num to firstDigit.
9. Now sum the first digit and last digit and print the results.
10. Stop the program.
Program:
#include <stdio.h>
int main()
{
int num, sum=0, firstDigit, lastDigit;
printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &num);
lastDigit = num % 10;
firstDigit = num;
while(num >= 10)
{
num = num / 10;
}
firstDigit = num;
sum = firstDigit + lastDigit;
printf("Sum of first and last digit = %d", sum);
return 0;
}
Output:
Enter any number to find sum of first and last digit: 1234
Sum of first and last digit = 5
Result:
Thus the C program to determine the sum of first and last digit was successfully executed and
verified.
27
Ex.No:3g
PASCAL TRIANGLE
Aim:
To write a C program to print Pascal triangle.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Input the number of rows.
4. Iterate variable i from 0 to row.
5. Iterate another variable from 1 to row –i.
6. Inside this loop, iterate j from 0 to i.
7. Check the condition if (j==0) || (i==0).
8. If the condition is true, print c=1.
9. Print values in the pattern using the formula c=c*(i-j+1)/j.
10. Display the pattern and stop the program,
Program:
#include <stdio.h>
int main()
{
int no_row,c=1,blk,i,j;
printf("Input number of rows: ");
scanf("%d",&no_row);
for(i=0;i<no_row;i++)
{
for(blk=1;blk<=no_row-i;blk++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
Output:
Input number of rows: 3
1
1 1
1 2 1
Result:
Thus the C program to print Pascal triangle pattern was successfully executed and verified.
28
Ex.No:3h
ARMSTRONG NUMBER
Aim:
To write a C program to determine whether a given number is Armstrong number.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Initialize sum =0.
4. Get the input value in the variable num.
5. Assign temp=num and iterate the loop until num!=0 and num=num/10.
6. Perform the following statements inside for loop.
a. r=num%10
b. sum=sum+(r*r*r)
7. Check if sum==temp; If the condition is true, then the given number is Armstrong
8. Else the given number is not Armstrong.
9. Stop the program.
Program:
#include <stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Input a number: ");
scanf("%d",&num);
for(temp=num;num!=0;num=num/10){
r=num % 10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number.\n",temp);
else
printf("%d is not an Armstrong number.\n",temp);
}
Output:
Input a number: 153
153 is an Armstrong number.
Result:
Thus the C program to determine whether the given number is Armstrong or not was successfully
executed and verified.
29
Ex.No: 4a
PRINTING ARRAY ELEMENTS
Aim:
To write a C program to print the array elements in forward and reversed order.
Algorithm:
1. Start the program.
2. Declare an array and initialize the array values from1 to 5.
3. Calculate the length of array using sizeof ( ) operator.
4. Iterate the for loop from 0 to length and print the values of array in forward order.
5. Iterate the for loop from lengh to1and print the values of array in reversed order.
6. Stop the program.
Program:
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr)/sizeof(arr[0]);
printf("Elements of given array: \n");
for (int i = 0; i < length; i++)
{
printf("%d\n ", arr[i]);
}
printf("Printing in reverse order:\n");
for(int i=length-1;i>=0;i--)
{
printf("%d\n",arr[i]);
}
return 0;
}
Output:
Elements of given array:
1
2
3
4
5
Printing in reverse order:
5
4
3
2
1
Result:
Thus the C program to display the array values in forward and reversed order was successfully
executed and verified.
30
Ex.No:4b
MATRIX ADDITION
Aim:
To write a C program to perform Matrix Addition.
Algorithm:
1. Start the program.
2. Declare three two dimensional array.
3. Declare the other necessary variables.
4. Get the input values for Matrix A.
5. Get the input values for Matrix B.
6. Add the two matrices using the formula c[i][j]=a[i][j]+b[i][j] with for loop.
7. Display the C Matrix.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf("***** Program to perform Matrix Addition *****\n");
printf("Enter value for A Matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("Enter value for B Matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Sum of Matrix is .....\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
}
printf("\n");
}
getch();
}
31
Output:
***** Program to perform Matrix Addition *****
Enter the value for A Matrix:
1
2
3
4
5
6
Enter the value for B Matrix:
4
5
6
1
3
2
Sum of Matrix is ….
5
7
9
5
8
8
Result:
Thus the C program to perform Matrix addition was successfully executed and verified.
32
Ex.No:4c
MATRIX MULTIPLICATION
Aim:
To write a C program to perform matrix multiplication.
Algorithm:
1. Start the program
2. Declare the necessary variables.
3. Get the row size and column size as input.
4. Get the A matrix and B matrix as input using for loop by iterating „i‟ variable from 0 to row and
„j‟ variable from 0 to column.
5. To perform matrix multiplication use three for loops.
a. Iterate variable „i‟ from 0 to row.
b. Iterate variable „j‟ from 0 to column; assign mul[i][j] to zero.
c. Iterate variable „k‟ from 0 to column.
d. Perform the following statement to multiply two matrices
mul[i][j]+=a[i][k]*b[k][j];
6. Display the resultant matrix by iterating variable „i‟ from 0 to row and variable „j‟ from 0 to
column.
7. Stop the program.
Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
33
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18
Result:
Thus the C program to perform matrix multiplication was successfully executed and verified.
34
Ex.No: 4d
TRANSPOSE OF A MATRIX
Aim:
To write a C program to perform transpose of a given matrix.
Algorithm:
1. Start the program.
2. Declare a[10][10] as input matrix and transpose[10][10] as output matrix.
3. Get the values of row and column.
4. Get the A matrix as input using for loop by iterating „i‟ variable from 0 to row and „j‟ variable
from 0 to column.
5. Display the original matrix.
6. Find the transpose matrix by executing the following statement inside the for loop by iterating „i‟
variable from 0 to row and „j‟ variable from 0 to column.
a. transpose[j][i]=a[i][j]
7. Display the resultant matrix and stop the program.
Program:
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
35
if(j==r-1)
printf("\n\n");
}
return 0;
}
Output
Enter rows and columns of matrix: 2 3
Enter elements of matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 5
Enter element a23: 6
Entered matrix
1
2
3
4
5
6
Transpose matrix
1
2
3
4
5
6
Result:
Thus the C program to perform transpose matrix was successfully executed and verified.
36
Ex.No: 5a
CONCATENATION OF STRING
Aim:
To write a C program to concatenate two strings without using built-in function.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Using for loop apply the logic to concatenate the strings.
4. Display the result.
5. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[50],b[50];
int i,j,n,k;
clrscr();
printf("***** String Concatenation without using String Function *****\n");
printf("Enter the first string:\n");
scanf("%s",a);
printf("Enter the second string:\n");
scanf("%s",b);
for(i=0,k=0;a[i]!='\0';i++)
k++;
for(j=0,n=0;b[j]!='\0';j++)
n++;
printf("Value of k=%d",k);
printf("Value of n=%d",n);
for(i=k,j=0;i<=n+k;i++,j++)
a[i]=b[j];
printf("The concatenated string is %s",a);
getch();
}
Output:
***** String Concatenation without using String Function *****
Enter the first string
Hi
Enter the second string
Welcome
The concatenated string is hiwelcome
Result:
Thus the C program to perform concatenation of two strings was successfully executed and
verified.
37
Ex.No:5b
COMPARING TWO STRINGS
Aim:
To write a C program to compare two strings without using built-in function.
Algorithm:
1. Start the program.
2. Declare the two input variables and get the input string.
3. Read the input strings and using if condition, compare the two strings.
4. Display the results.
5. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[30], str2[30];
int i;
clrscr();
printf("***** Comparing two Strings without using String Function *****\n");
printf("Enter the first string:\n");
scanf("%s",str1);
printf("Enter the second string:\n");
scanf("%s",str2);
i=0;
while(str1[i]==str2[i] && str1[i]!='\0')
i++;
if(str1[i]>str2[i])
printf("String1 is greater than String2");
else if(str1[i]<str2[i])
printf("String1 is less than String2");
else
printf("Strings are equal");
getch();
}
Output:
***** Comparing two Strings without using String Function *****
Enter the first string:
Hi
Enter the second string:
Hi
Strings are equal
Result:
Thus the C program to compare two strings was successfully executed and verified.
38
Ex.No:5c
COPYING STRING
Aim:
To write a C program to copy string from one variable to another variable without using built-in
function.
Algorithm:
1. Start the program.
2. Declare two variables.
3. Get the input string in one variable.
4. Using while loop read the input string and assign it to the second input variable.
5. Display the copied string.
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100],s2[100];
int i;
clrscr();
printf("***** Copying a String without using String Function *****\n");
printf("Enter the string:\n");
scanf("%s",s1);
i=0;
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("Copied String is %s",s2);
getch();
}
Output:
***** Copying a String without using String Function *****
Enter the string:
India
Copied String is India
Result:
Thus to write a C program to copy a string from one variable to another without using built-in
function was successfully executed and verified.
39
Ex.No:5d
STRING PALINDROME
Aim:
To write a C program to check whether a string is palindrome without using built-in function.
Algorithm:
1. Start the program.
2. Declare the necessary variables.
3. Using for loop, apply the logic for finding whether the given string is palindrome or not.
4. Display the result.
5. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[10];
int i,len,flag=0;
clrscr();
printf("***** Palindrome without using String Function *****\n");
printf("Enter the input string:\n");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]==str[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("The String is Palindrome...\n");
else
printf("The String is not a Palindrome...\n");
getch();
}
Output:
***** Palindrome without using String Function *****
Enter the string:
dad
The string is palindrome.
Result:
Thus to write a C program to determine whether a given string is palindrome was successfully
executed and verified.
40
Ex.No:5e
SORTING STRINGS
Aim:
To write a C program to sort the given names in alphabetic order.
Algorithm:
1. Start the program.
2. Declare the necessary string variables and get the input.
3. Create a dummy variable to swap the string.
4. Using strcpy ( ), arrange the strings in alphabetical order within an appropriate for loop.
5. Display the results.
Program:
#include<stdio.h>
#include<conio.h>
#define ITEMS 5
#define MAXCHAR 20
void main()
{
char string[ITEMS][MAXCHAR],dummy[MAXCHAR];
int i=0,j=0;
printf(“*********** Sorting Strings *************\n”);
clrscr();
printf("Enter the names of %d items",ITEMS);
while(i<ITEMS)
scanf("%s",string[i++]);
for(i=1;i<ITEMS;i++)
{
for(j=1;j<=ITEMS;j++)
{
if(strcmp(string[j-1]),string[j]>0)
{
strcpy(dummy,string[j-1]);
strcy(string[j-1],string[j]);
strcpy(string[j],dummy);
}
}
}
printf(" Sorted order for the given input....\n");
for(i=0;i<ITEMS;i++)
printf("%s",string[i]);
getch();
}
Output:
*********** Sorting Strings *************
Enter the name of 5 items:
India
America
Delhi
41
England
Belgium
Sorted order for the given input…
America
Belgium
Delhi
England
India
Result:
Thus the C program to perform sorting of strings was successfully executed and verified.
42
Ex.No: 6a
MATHEMATICAL OPERATIONS USING USER DEFINED FUNCTION
Aim:
To write a C program to perform mathematical operations using user defined function.
Algorithm:
1. Start the program.
2. Declare the necessary mathematical functions to be performed before main function.
3. Declare two input variables num1 and num2; Get the values for num1 and num2.
4. Call the mathematical function one by one inside the main function.
5. Close the main function.
6. Write the function definition of mathematical operation add, subtract, multiplication and division
respectively.
7. Display the results and stop the program.
Program:
#include<stdio.h>
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("%d + %d = %d\n", num1, num2, add(num1, num2));
printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
printf("%d / %d = %d\n", num1, num2, divide(num1, num2));
return 0;
}
int add(int n1, int n2)
{
int result;
result = n1 + n2;
return result;
}
int subtract(int n1, int n2)
{
int result;
result = n1 - n2;
return result;
}
43
int multiply(int n1, int n2)
{
int result;
result = n1 * n2;
return result;
}
int divide(int n1, int n2)
{
int result;
result = n1 / n2;
return result;
}
Output:
Enter two numbers: 100
10
100 + 10 = 110
100 - 10 = 90
100 * 10 = 1000
100 / 10 = 10
Result:
Thus the C program to perform mathematical operations using user defined function was
successfully executed and verified.
44
Ex.No:6b
SWAPPING TWO NUMBERS USING CALL BY VALUE
Aim:
To write a C program to perform swapping of two numbers using call by value.
Algorithm:
1. Start the program
2. Declare the function swap before the main function.
3. Declare the variable a and b; Get the values for a and b.
4. Print the values performing the swapping operation.
5. Call the swap function and close the main function.
6. Define the function swap with the following instructions.
a. Declare a third variable temp.
b. temp=a; a=b; b=temp;
7. Display the result and stop the program.
Program:
#include <stdio.h>
void swap(int , int);
int main()
{
int a,b;
printf("Enter the values of a and b:\n");
scanf("%d\n%d",&a,&b);
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output:
Enter the values of a and b:
34
45
Before swapping the values in main a = 34, b = 45
After swapping values in function a = 45, b = 34
After swapping values in main a = 34, b = 45
Result:
Thus the C program to perform swapping of two numbers using call by value was successfully
executed and verified.
45
Ex.No:6c
SWAPPING OF TWO NUMBERS USING CALL BY REFERENCE
Aim:
To write a C program to perform swapping of two numbers using call by reference
Algorithm:
1. Start the program
2. Declare the function swap with pointer parameters before the main function.
3. Declare the variable a and b; Get the values for a and b.
4. Print the values performing the swapping operation.
5. Call the swap function by passing the address of the input variables a and b.
6. Close the main function.
7. Define the function swap with the following instructions.
a. Declare a third variable temp.
b. temp=*a; *a=*b; *b=temp;
8. Display the result and stop the program.
Program:
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
Output:
Enter the values for a and b:
34
68
Before swapping the values in main a = 34, b = 68
After swapping values in function a = 68, b = 34
After swapping values in main a = 68, b = 34
Result:
Thus the C program to perform swapping of two numbers using call by reference was successfully
executed and verified.
46
Ex.No:6d
PRIME, ARMSTRONG OR PERFECT NUMBER USING FUNCTION
Aim:
To write a C program to find prime number, Armstrong number and perfect number using the
concept of function.
Algorithm:
1. Start the program.
2. Declare three functions to find a given number is prime, Armstrong and perfect number.
3. Declare the input variable and get the value for the input variable „num‟.
4. Write if conditions to call the function and print the results.
5. Write function definition for prime number, Armstrong number and perfect number.
6. Stop the program.
Program:
#include <stdio.h>
#include <math.h>
int isPrime(int num);
int isArmstrong(int num);
int isPerfect(int num);
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(isPrime(num))
{
printf("%d is Prime number.\n", num);
}
else
{
printf("%d is not Prime number.\n", num);
}
if(isArmstrong(num))
{
printf("%d is Armstrong number.\n", num);
}
else
{
printf("%d is not Armstrong number.\n", num);
}
if(isPerfect(num))
{
printf("%d is Perfect number.\n", num);
}
else
{
printf("%d is not Perfect number.\n", num);
}
return 0;
}
int isPrime(int num)
{
47
int i;
for(i=2; i<=num/2; i++)
{
if(num%i == 0)
{
return 0;
}
}
return 1;
}
int isArmstrong(int num)
{
int lastDigit, sum, originalNum, digits;
sum = 0;
originalNum = num;
digits = (int) log10(num) + 1;
while(num > 0)
{
lastDigit = num % 10;
sum = sum + round(pow(lastDigit, digits));
num = num / 10;
}
return (originalNum == sum);
}
int isPerfect(int num)
{
int i, sum, n;
sum = 0;
n = num;
for(i=1; i<n; i++)
{
if(n%i == 0)
{
sum += i;
}
}
return (num == sum);
}
Output:
Output 1
Output 2
Enter any number: 6
Enter any number: 153
6 is not Prime number.
153 is not Prime number.
6 is Armstrong number.
153 is Armstrong number.
6 is Perfect number.
153 is not Perfect number.
Output 3
Enter any number: 17
17 is Prime number.
17 is not Armstrong number.
17 is not Perfect number.
Result:
Thus the C program to find whether a given number is prime, Armstrong and perfect number was
successfully executed and verified.
48
Ex.No: 7a
FIBONACCI SERIES USING RECURSIVE FUNCTION
Aim:
To write a C program to generate Fibonacci series using recursive function.
Algorithm:
1. Start the program.
2. Declare the function fibonacci using one parameter.
3. Declare the input variable n and get the value for n.
4. Using for loop, iterate the value from 1 to n and call the function.
5. Write the function definition for generating Fibonacci series.
a. Check the condition: if input number „n‟ is equal to zero; If true, return 0 as result.
b. Check the condition: if input number „n‟ is equal to one; If true, return 1 as result.
c. Else perform the following by calling Fibonacci function again
(( Fibonacci(n-1) + Fibonacci(n-2) )
6. Display the result and stop the program
Program:
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Enter the number:\n");
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++; }
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Output:
Enter the number:
5
Fibonacci series
0
1
1
2
3
Result:
Thus the C program to generate Fibonacci series using recursive function was successfully
executed and verified.
49
Ex.No:7b
POWER OF ANY NUMBER USING RECURSION
Aim:
To write a C program to find power of any number using recursive function.
Algorithm:
1. Start the program.
2. Declare the function pow.
3. Declare the input variables expo and base; declare the output variable power.
4. Get the values for base and exponent.
5. Call the function pow.
6. Write the function definition to find power of any number.
a. If expo equals to zero then return the result as 1.
b. If expo is greater than1, then perform base *pow(base,expo -1) using recursive function.
c. If expo is less than 1, then return result as 1 / ppw(base, -expo)
7. Display the result and stop the program.
Program:
#include <stdio.h>
double pow(double base, int expo);
int main()
{
double base, power;
int expo;
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%d", &expo);
power = pow(base, expo);
printf("%.2lf ^ %d = %0.2f", base, expo, power);
return 0;
}
double pow(double base, int expo)
{
/* Base condition */
if(expo == 0)
return 1;
else if(expo > 0)
return base * pow(base, expo - 1);
else
return 1 / pow(base, -expo);
}
Output:
Enter base: 4
Enter exponent: 4
4.00 ^ 4 = 256.00
Result:
Thus the C program to perform power of any number using recursive function was successfully
executed and verified.
50
Ex.No: 7c
FACTORIAL OF A GIVEN NUMBER USING RECURSIVE FUNCTION
Aim:
To write a C program to find factorial of a given number using recursive function.
Algorithm:
1. Start the program.
2. Declare the function fact( ).
3. Declare the variables x and n.
4. Get the input value „n‟.
5. Call the function fact(n).
6. In the function definition, check the value of „n‟ is equal to zero. If yes, return the result as 1.
7. Else perform the step n*fact(n-1) recursively.
8. Display the result and stop the program.
Program:
#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}
Output:
Enter the Number to Find Factorial :5
Factorial of 5 is 120
Result:
Thus the C program to find a factorial of a given number using recursive function was successfully
executed and verified.
51
Ex.No:8a
BIGGEST AMONG THREE NUMBERS USING POINTERS
Aim:
To write a C program to find biggest among three numbers using pointers.
Algorithm:
1. Start the program.
2. Declare three input variables and three pointer variables.
3. Assign the address of input variables to pointer variables respectively.
4. Use if else ladder to find the biggest among three numbers using pointer variables.
5. Display the appropriate result and Stop the program.
Program:
#include<stdio.h>
int main()
{
int a,b,c,*pa, *pb, *pc;
printf("Enter three numbers:\n");
scanf("%d%d%d", &a,&b,&c);
/* Referencing */
pa= &a;
pb= &b;
pc= &c;
if(*pa > *pb && *pa > *pc)
{
printf("Largest is: %d", *pa);
}
else if(*pb > *pc && *pb > *pc)
{
printf("Largest is : %d", *pb);
}
else
{
printf("Largest = %d", *pc);
}
return 0;
}
Output:
Enter three numbers:
34
23
45
Largest = 45
Result:
Thus the C program to find biggest of three numbers using pointers was successfully executed
and verified.
52
Ex.No:8b
COMPARE TWO STRINGS USING POINTERS
Aim:
To write a C program to compare two strings using Pointers.
Algorithm:
1. Start the program.
2. Declare two input string variables and two pointer variables.
3. Declare variable „i‟ and variable „equal‟=0.
4. Get two strings as input in the variables string1 and string2 respectively.
5. Now assign the input variables to the two pointer variables.
6. Using while loop check the condition (*str1 == *str2).
a. If the condition is true, check the if condition: if(*str1== „\0‟ || *str2== „\0‟) terminate
the iteration.
b. Else increment the pointer variables.
7. Finally check if(*str1 == „\0‟ && *str2== „\0‟). If the condition is true, display the result as both
strings are equal.
8. Else print Strings are not equal.
9. Stop the program.
Program:
#include <stdio.h>
int main()
{
char string1[50],string2[50],*str1,*str2;
int i,equal = 0;
printf("Enter The First String: ");
scanf("%s",string1);
printf("Enter The Second String: ");
scanf("%s",string2);
str1 = string1;
str2 = string2;
while(*str1 == *str2)
{
if ( *str1 == '\0' || *str2 == '\0' )
break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
}
Output:
Enter The First String: hi
Enter The Second String: hello
Both Strings Are Not Equal.
Enter The First String: hello
Enter The Second String: hello
Both Strings Are Equal.
Result:
Thus the C program to compare two strings using pointers was successfully executed and
verified.
53
Ex.No:8c
SUM OF ARRAY ELEMENTS USING POINTERS
Aim:
To write a C program to find the sum of array elements using pointers.
Algorithm:
1. Start the program.
2. Declare an array and required variable „size‟ also declare a pointer variable *ptr.
3. Declare and initialize variable sum =0
4. Get the size of the array in the input variable „size‟.
5. Enter the array elements using for loop.
6. Now set address of first array element to pointer variable.
7. Use for loop to find the sum of elements inside the array by incrementing the ptr variable.
8. Display the result and stop the program.
Program:
#include <stdio.h>
int main()
{
int arr[100], size;
int *ptr, sum = 0;
printf("Enter the size of the array:\n ");
scanf("%d", &size);
printf("Enter array elements:\n ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
// Set address of first array element to *ptr
ptr = arr;
for (int i = 0; i < size; i++)
{
sum = sum + *ptr;
ptr++; // Increment pointer by one to get next element
}
printf("The sum of array elements is: %d", sum);
return 0;
}
Output:
Enter the size of the array:
5
Enter array elements:
10
20
30
40
50
The sum of array elements is: 150
Result:
Thus the C program to find the sum of array elements using pointers was successfully executed
and verified.
54
Ex.No:8d
SEARCH AN ELEMENT IN ARRAY USING POINTERS
Aim:
To write a C program to search an element in array using pointers.
Algorithm:
1. Start the program.
2. Declare the variables „i‟ and „l‟.
3. Declare a function search with three parameters.
4. Inside main function, declare the necessary input variables.
5. Get the size of array and value of array using for loop.
6. Get the number to be searched inside the array and Call the search function.
7. In search ( ) definition, using for loop check if element to be searched (m) is equal to first element
of array. If yes, print element found.
8. Iterate the same step till the end of array and display the appropriate results.
Program:.
#include<stdio.h>
int i,l;
int search(int ,int *,int);
int main()
{
int n,m;
printf("enter the size of array:\n");
scanf("%d",&n);
int a[n];
printf("enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to be searched:\n");
scanf("%d",&m);
search(n,a,m);
return 0;
}
int search(int n,int *a,int m)
{
for(i=0;i<n;i++)
{
if(m==a[i])
{
l=1;
break;
}
}
if(l==1)
{
printf("%d is present in the array",m);
} else
{
printf("%d is not present in the array",m);
}
}
55
Output:
enter the size of array:
5
enter the elements:
12
6
34
56
7
enter the element to be searched:
45
45 is not present in the array
enter the size of array:
5
enter the elements:
23
34
56
2
45
enter the element to be searched:
2
2 is present in the array
Result:
Thus the C program to search an element in array using pointers was successfully executed and
verified.
56
Ex.No:8e
PROGRAM FOR POINTER AND POINTER TO POINTER OPERATIONS
Aim:
To write a C program for pointer and pointer to pointer conversion.
Algorithm:
1. Start the program.
2. Initialize the variable =789
Program:
#include <stdio.h>
int main()
{
int a = 789;
int* ptr2;
int** ptr1; //pointer to pointer
ptr2 = &a;
// Storing address of ptr2 in ptr1
ptr1 = &ptr2;
printf("Value of a = %d\n", a);
printf("Value of a using single pointer = %d\n", *ptr2);
printf("Value of a using double pointer = %d\n", **ptr1);
return 0;
}
Output:
Value of a = 789
Value of a using single pointer = 789
Value of var using double pointer = 789
Result:
Thus the C program to perform pointer operation and pointer to pointer operation was
successfully executed and verified.
57
Ex.No: 9a
EMPLOYEE PAY ROLL APPLICATION USING STRUCTURE
Aim:
To write a C program to implement payroll application with the given data using structure.
HRA=18% of basic pay, DA=15% of basic pay, PF=10% of basic pay, LIC=7% of basic pay, Deduction=
PF+LIC, Gross Salary= Basic Pay+HRA+DA, Net Salary=Gross Salary-Deduction.
Algorithm:
1. Start the program.
2. Define a structure emp and declare the necessary variables for an employee.
3. Get the input values.
4. Calculate the HRA, DA, PF, LIC, Deduction, Gross Salary and Net Salary with the given
formula.
5. Display the results.
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno;
char name[15];
int bpay;
}e[10];
void main()
{
int i,n;
float hra,da,pf,lic,deduction,Gross,NetSalary;
clrscr();
printf("***** Payroll Program Using Structures *****\n");
printf("Enter the number of employees:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the employee number:\n");
scanf("%d",&e[i].empno);
printf("Enter the name:\n");
scanf("%s",e[i].name);
printf("Enter the basic pay:\n");
scanf("%d",&e[i].bpay);
hra=0.18*e[i].bpay;
da=0.15*e[i].bpay;
pf=0.10*e[i].bpay;
lic=0.07*e[i].bpay;
deduction=e[i].bpay+pf+lic;
Gross=e[i].bpay+hra+da;
NetSalary=Gross-deduction;
}
printf("The Employee Details are....\n");
for(i=0;i<n;i++)
{
58
printf("Empno\t Name\t hra\t da\t pf\t lic\t deduction\t Gross\t NetSalary\n");
printf("%d\t%s\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\n",e[i].empno,e[i].name,hra,da,pf,lic,d
eduction,Gross,NetSalary);
}
getch();
}
Output:
***** Payroll Program Using Structures *****
Enter the number of employees:
2
Enter the employee number:
1
Enter the name:
James
Enter the basic pay:
20000
Enter the employee number:
2
Enter the name:
Vasanth
Enter the basic pay:
30000
Empno
Name
1
James
2
Vasanth
Deduction
3400
5100
HRA
3600
5400
Gross Salary
26600
39900
DA
3000
4500
PF
2000
3000
LIC
1400
2100
Net Salary
23200
34800
Result:
Thus to write a C program to implement payroll application using structure was successfully
executed and verified
59
Ex.No:9b
DISPLAYING STUDENT AVERAGE USING UNION
Aim:
To write a C program to display student average using union.
Algorithm:
1. Start the program.
2. Define a union student and declare the necessary variables for an employee.
3. Get the input values.
4. Display the student name, subject and percentage
5. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
void main()
{
union student record1;
union student record2;
clrscr();
printf("***** Displaying Student Average *****\n");
strcpy(record1.name, "John");
strcpy(record1.subject,"Computer");
record1.percentage=86.50;
printf("Union record1 values....\n");
printf("Name %s\n",record1.name);
printf("Subject %s\n",record1.subject);
printf("Percentage",record1.percentage);
printf("Union record2 values....\n");
strcpy(record2.name,"Peter");
printf("Name %s\n",record2.name);
strcpy(record2.subject,"Chemistry");
printf("Subject %s\n",record2.subject);
record2.percentage=98.34;
printf("Percentage %f\n",record2.percentage);
getch();
}
Output:
***** Payroll Program Using Union *****
Union record1 values:
name: John
subject: Computer
percentage:86.50
60
Unio record2 values:
Name: Peter
Subject: Chemistry
Percentage: 98.34
Result:
Thus to write a C program to display the student average using union was successfully executed
and verified.
61
Ex.No: 9c
STUDENT DETAILS USING STRUCTURE
Aim:
To write a C program to print student details using structure.
Algorithm:
1. Start the program.
2. Declare a structure student with required data members.
3. Inside main function, get the total number of students.
4. Using for loop, get the student details name, rollno marks and grade by accessing structure data
members.
5. Use else if ladder to determine the grade of the student.
6. Display the appropriate result and Stop the program.
Program:
/* Program to process marks of students and display Grades */
#include <stdio.h>
#include <conio.h>
struct student{
char name[25];
int rollno;
int marks;
char grade;
};
main()
{
struct student stud[15];
int i,n;
/* Get the number of students */
printf("ENTER THE NUMBER OF STUDENTS:");
scanf("%d",&n);
/* Get the value of Students marks*/
for(i=0;i<n;i++)
{
printf("ENTER STUDENT INFORMATION:\n");
printf("NAME:");
scanf("%s",stud[i].name);
printf("ROLLNO:");
scanf("%d",&stud[i].rollno);
printf("MARKS(In Percentage):");
scanf("%d",&stud[i].marks);
printf("\n");
}
/* Print student information*/
for(i=0;i<n;i++)
{
if(stud[i].marks <= 50)
{
stud[i].grade = 'F';
}
else if(stud[i].marks > 50 && stud[i].marks <= 55)
{
stud[i].grade = 'D';
62
}
else if(stud[i].marks > 55 && stud[i].marks <= 60)
{
stud[i].grade = 'C';
}
else if(stud[i].marks > 60 && stud[i].marks <= 75)
{
stud[i].grade = 'B';
}
else if (stud[i].marks > 75 && stud[i].marks <= 90)
{
stud[i].grade = 'A';
}
else if(stud[i].marks > 90)
{
stud[i].grade = 'S';
}
}
for(i = 0;i<40;i++)
printf("_");printf("\n");
printf("Name\tRollNo\tMarks\tGrade\n");
for(i = 0;i<40;i++)
printf("_");printf("\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%c\n",stud[i].name,stud[i].rollno,stud[i].marks,stud[i].grade);
}
for(i = 0;i<40;i++)
printf("_");printf("\n");
getch();
return 0;
}
Output:
ENTER THE NUMBER OF STUDENTS:2
ENTER STUDENT INFORMATION:
NAME:Ravi
ROLLNO:1
MARKS(In Percentage):56
ENTER STUDENT INFORMATION:
NAME:Kumar
ROLLNO:2
MARKS(In Percentage):89
________________________________________
Name RollNo Marks Grade
________________________________________
Ravi 1
56 C
Kumar 2
89 A
________________________________________
63
Ex.no: 10a
STORE EMPLOYEE INFORMATION IN A FILE
Aim:
To write a C program to store employee information in a file
Algorithm:
1. Start the program.
2. Create a File pointer fp and declare the necessary variables.
3. Open a file emp.txt in write mode.
4. Get the input name,age and salary as input.
5. Use fprintf( ) to write the input to the emp.txt file.
6. Open the created file from the folder C:\turboc3\bin and display the output.
7. Stop the program.
Program:
#include <stdio.h>
int main()
{
FILE *fptr;
char name[20];
int age;
float salary;
/* open for writing */
fptr = fopen("emp.txt", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
}
Output:
Output in turbo c window
Enter the name
emp
Enter the age
34
Enter the salary
45000
64
File created in bin folder
Output in the created notepad file
Result:
Thus the C program to create a file and write the contents of the file was successfully executed
and verified.
65
Ex.No: 10b
DISPLAY DIRECTORIES AND SUB-DIRECTORIES
Aim:
To write a C program to display directories and sub-directories.
Algorithm:
1. Start the program.
2. Include dirent.h header file to use opendir function.
3. Create a pointer variable *dir to open directory.
4. Use readdir ( ) in a while loop to print directory name, sub-directory and file names,
5. Display the result.
6. Stop the program.
Program:
#include <stdio.h>
#include <dirent.h>
int main(void){
struct dirent *files;
DIR *dir = opendir(".");
if (dir == NULL){
printf("Directory cannot be opened!" );
return 0;
}
while ((files = readdir(dir)) != NULL)
printf("%s", files->d_name);
closedir(dir);
return 0;
}
Output:
...10a.c
10a.exe
10B.C
10B.exe
1BSWAP.BAK
1BSWAP.C
1BTEMP.BAK
1BTEMP.C
1c.c
1D.BAK
1D.C
1e.c
1f1.c
1g.c
2A.BAK
2A.C2
Result:
To write a C program to display the directory and sub-directory names was successfully executed
and verified.
66
Ex.No:10 c
READING A FILE STORED ON THE DISK
Aim:
To write a C program to read a file stored on the disk.
Algorithm:
1. Start the program.
2. Create a File pointer.
3. Get the filename to open in read mode.
4. If file does not exist, print cannot open the file.
5. Use fgetc( ) to get the first character of the file content.
6. Iterate the while loop until the character becomes EOF.
7. Print the character and read the next character using while loop.
8. Display the result and Stop the program.
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened\n");
gets(filename);
fptr = fopen (filename, "r"); /*open for reading*/
if (fptr == NULL)
{
printf("Cannot open file\n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
return 0;
}
67
Output:
Result:
Thus the C program to read a file on the stored disk was successfully executed and verified.
68
Ex.No: 10d
REVERSE THE CONTENTS OF FILE
Aim:
To write a C program to print and reverse the contents of the file.
Algorithm:
1. Start the program.
2. Create a file pointer.
3. Declare the necessary variables.
4. Open the file “emp.txt” in read mode.
5. Use fseek ( )function to set the file pointer.
6. Assign the current position of the file pointer the pos variable.
7. Initialize i=0. Using while loop check the condition i<pos;
a. If the condition is true, increment i value, move the pointer through fseek( ), read the
character through fgetc( ) and print the character.
8. Display the result and stop the program.
Program:
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
int i,pos;
fp=fopen("emp.txt","r");
if(fp==NULL)
{
printf("File does not exist..");
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
//printf("Current postion is %d\n",pos);
i=0;
while(i<pos)
{
i++;
fseek(fp,-i,SEEK_END);
//printf("%c",fgetc(fp));
ch=fgetc(fp);
printf("%c",ch);
}
return 0;
}
69
Output:
Result:
Thus the C program to reverse the contents of a file was successfully executed and verified.
70
Ex.No: 10e
COPY CONTENTS OF ONE FILE TO ANOTHER FILE
Aim:
To write a C program to copy contents of one file to another file.
Algorithm:
1. Start the program.
2. Create two file pointers, Declare the necessary variables.
3. Get the name of the file to be read; Open the file in read mode.
4. Open another file in write mode.
5. Check both file pointers does not return NULL.
6. Read the contents from first file using fgetc( ). Use while loop to iterate and read the entire
contents.
7. Use another while loop; Use fputc( ) to print the contents in second file.
8. Display the result and stop the program.
Program:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
71
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
New file “employee.txt” created in C:\TurboC3\bin folder
72
Contents in old file “emp.txt”
Contents in New copied file “employee.txt”
73
Result:
Thus to write a C program to copy contents of one file to another file was successfully executed
and verified.
74
Download