Wa0003.
Wa0003.
COLLEGE OF TECHNOLOGY
(An Autonomous Institution, Affiliated to Anna University, Chennai)
Vadakal, Sriperumbudur – 602 105
RECORD NOTEBOOK
Student Name :
Register Number :
Semester / Branch :
Subject Code :
Subject Name :
SRI VENKATESWARAA
COLLEGE OF TECHNOLOGY
(An Autonomous Institution, Affiliated to Anna University, Chennai)
Vadakal, Sriperumbudur – 602 105
Department of Computer Science & Engineering
BONAFIDE CERTIFICATE
This is to certify that this a bonafide record of practical work done
by Register Number
of Semester B.E / M.E. in the
laboratory during the
academic year .
Problem:
Write C programs using I/O Statements.
Aim:
To write C programs using I/O Statements
Algorithm:
Step 1: Start
Step 2: Get the input for radius
Step 3: Calculate area and circumference by using the formula πr2 and 2πr
Step 4: Print the area and circumference
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
getch();
}
Output:
ii) Swapping two numbers
Algorithm:
Step 1: Start
Step 2: Get the input for two numbers
Step 3: Store the first number in temporary variable
Step 4: Store second number to first
Step 5: Store temporary variable in second number
Step 6: Display the two numbers
Step 6: Stop
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int x, y, t;
printf("Enter two integers: ");
scanf("%d%d", &x, &y);
printf("\nBefore Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("\nAfter Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf("\nSquare of the number %d is %d",n,n*n);
printf("\n\nCube of the number %d is %d",n,n*n*n);
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
float cel,c2,f1,faren;
printf("Enter the temperature in Celsius : ");
scanf("%f",&cel);
f1=cel*9/5+32;
printf("The temperature in fahrenheit is : %.2f\n",f1);
printf("\nEnter the temperature in Fahrenheit : ");
scanf("%f",&faren);
c2=(faren-32)*5/9;
printf("the temperature in celsius is :%.2f",c2);
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
main()
{
char name[25];
int idno;
float salary;
printf("Enter ID no, Name, Salary : \n");
scanf("%d%s%f",&idno,name,&salary);
printf("\n ID number :%d",idno);
printf("\n Name : %s",name);
printf("\n Salary : %.2f",salary);
getch();
}
Output:
vi) Square root of a Number
Algorithm:
Step 1: Start
Step 2: Include the math.h header file
Step 3: Get the input as number
Step 4: Using sqrt() function, find the square root of the given number
Step 5: Display the result
Step 6: Stop
Program:
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
double number, result;
printf(" \n Enter a Number to find Square root : ");
scanf("%lf", &number);
result = sqrt(number);
printf("\n Square Root a number is %.2lf",result);
getch();
}
Output:
Result:
Thus the C programs using I/O statements were written, executed and the outputs were
verified successfully.
Ex. No. 1. b) OPERATORS
Date:
Problem:
Write C programs for using C Programming Language Operators
i) Arithmetic Operators
ii) Bitwise Operators
iii) Increment/Decrement Operators
iv) Conditional Operator
Aim:
To write C programs for using Operators in C
i) Arithmetic Operators
Algorithm:
Step 1: Start
Step 2: Get the two numbers
Step 4: Perform the arithmetic operations (+,-,*,/,%)
Step 5: Display the results
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
printf("\nAddition : a + b : %d\n",a+b);
printf("Subtraction : a - b : %d\n",a-b);
printf("Multiplication : a * b : %d\n",a*b);
printf("Division : a / b : %d\n",a/b);
printf("Remainder : a mod b : %d",a%b);
getch();
}
Output:
ii) Bitwise Operators
Algorithm:
Step 1: Start
Step 2: Get the two numbers
Step 4: Perform the Bitwise operations
Step 5: Display the results
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("\nBitwise not ~a: %d\n",~a);
printf("Bitwise right shift a>>b: %d\n",a>>b);
printf("Bitwise left shift a<<b: %d\n",a<<b);
printf("Bitwise and a&b: %d\n",a&b);
printf("Bitwise xor a^b: %d\n",a^b);
printf("Bitwise or a|b: %d",a|b);
getch();
}
Output:
iii) Increment/Decrement operators
Algorithm:
Step 1: Start
Step 2: Get an input.
Step 4: Perform the Increment (++a, a++) and Decrement (--a, a--) operations
Step 5: Display the results
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf("Enter the numebr : ");
scanf("%d",&a);
printf("\nPre increment ++a: %d\n",++a);
printf("Pre decrement --a: %d\n",--a);
printf("Post increment a++: %d\n",a++);
printf("Post decrement a--: %d",a--);
getch();
}
Output:
iv) Conditional Operator
Algorithm:
Step 1: Start
Step 2: Get two numbers
Step 3: Find the greatest number using Conditional Operator a>b?a:b;
Step 4: Display the results
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,greatest;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
greatest=a>b?a:b;
printf("\n Using Conditional Operator a>b?a:b, Greatest number is : %d\n",greatest);
getch();
}
Output:
Result:
Thus the C programs using Operators were written, executed and the outputs were verified
successfully.
Ex. No. 1. c) EXPRESSIONS
Date:
Problem:
Write C programs for the evaluation of the expressions
Aim:
To write C programs for the evaluation of the expressions
Program:
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c,d,x,y,z;
printf("Enter the a value : ");
scanf("%f",&a);
printf("Enter the b value : ");
scanf("%f",&b);
printf("Enter the c value : ");
scanf("%f",&c);
printf("Enter the d value : ");
scanf("%f",&d);
x=(a*b)-c;
printf("\nThe value of x=(a*b)-c : %f\n",x);
y=(b/c)*a;
printf("The value of y=(b/c)*a : %f\n",y);
z=((a-b)/(c+d));
printf("The value of z=((a-b)/(c+d)) : %f",z);
getch();
}
Output:
Program:
#include<stdio.h>
#include<math.h>>
#include<conio.h>
main()
{
int a,b,c,d;
float root1,root2;
printf("Enter the coefficient of x square : ");
scanf("%d",&a);
printf("Enter the coefficient of x : ");
scanf("%d",&b);
printf("Enter the constant value : ");
scanf("%d",&c);
d = b*b - 4*a*c;
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("\n Root 1 Value : %.2f",root1);
printf("\n Root 2 Value : %.2f",root2);
getch();
}
Output:
Result:
Thus the C programs for the evaluation of the expressions were written, executed and the
outputs were verified successfully.
Ex. No. 2 DECISION-MAKING CONSTRUCTS
Date:
Problem:
Write C programs for using Decision-making constructs.
a) if-else
i) Even or Odd
ii) Greatest among Three numbers
b) goto
i) Forward Jump – Calculate the average of the Marks
ii) Backward Jump – Multiplication Table
c) switch-case
i) Arithmetic Operators
d) break-continue
i) Printing Numbers using break
ii) Printing even numbers using continue
Aim:
To write C programs for using Decision-making constructs
i) Even or Odd
Algorithm:
step 1: Start
step 2: Get the input for number
step 3: Divide the number by 2 and check the remainder
step 3.1 : If it is 0, print that the number is even
step 3.2 : Otherwise print that the number is odd
step 4: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("\n%d is an Even Number", number);
else
printf("\n%d is an Odd Number", number);
getch();
}
Output:
ii) Greatest among Three numbers
Algorithm:
Step 1: Start.
Step 2: Declare a, b, c as integer.
Step 3: if (a>b) and (a>c) then display a is greatest.
Step 4: else if (b>c) then display b is greatest
Step 5: else display c is greatest.
Step 6: Stop.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("GREATEST OF THREE NUMBERS");
printf("\n\nEnter the values of A, B and C : ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("\nA = %d IS THE GREATEST",a);
else if(b>c)
printf("\nB = %d IS THE GREATEST",b);
else
printf("\nC = %d IS THE GREATEST",c);
getch();
}
Output:
Ex. No. 2. b) DECISION-MAKING CONSTRUCTS – (goto)
Algorithm:
Step 1: Start
Step 2: Get the total number of marks
Step 3: After getting all the marks, goto statement is used to go for the calculation of the average
Step 4: Display the results
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum,mark;
float average;
printf("Enter the total number of subjects : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the marks of subject %d : ",i);
scanf("%d",&mark);
sum=sum+mark;
if(i==n){
goto CALCULATE;
}
}
CALCULATE:
average=sum/n;
printf("\nAverage is :%.3f",average);
getch();
}
Output:
ii) Backward Jump – Multiplication Table
Algorithm:
Step 1: Start
Step 2: Get the number for Table
Step 3: After getting the range to print the table, goto statement is used to print the table
Step 4: Display the results
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,counter=1,range;
printf("Enter the number for Table: ");
scanf("%d",&n);
printf("\nEnter the range to print: ");
scanf("%d",&range);
TABLE:
while(counter<=range){
printf("%d * %d = %d\n",counter,n,counter*n);
counter++;
goto TABLE;
}
getch();
}
Output:
Ex. No. 2. c) DECISION-MAKING CONSTRUCTS – (switch-case)
i)Arithmetic operators
Algorithm:
Step 1: Start
Step 2: Display the Arithmetic Operations 1. Addition 2. Subtraction 3. Multiplication 4. Division
5. Modulus
Step 3: Get the values of a and b
Step 4: Get the option to perform the arithmetic operations
i) If choice is 1, add the two numbers and print the result.
ii) If choice is 2, subtract b from a and print the result.
iii) If choice is 3, multiply the two numbers and print the result.
iv) If choice is 4, divide a by b and print the result.
v) If choice is 5, perform the modulus operations and print the result.
vi) If choice not between 1 and 5, default case is executed.
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,ch;
printf("\nArithmetic Operations: \n\t1.Addition \n\t 2.Subtraction\n\t 3.Multiplication\n\t
4.Division\n\t 5.Modulus\n");
printf("\nEnter the values of a and b : ");
scanf("%d%d",&a,&b);
printf("\nEnter the option : ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nSum of a and b is : %d",a+b);
break;
case 2:
printf("\nDifference of a and b is : %d",a-b);
break;
case 3:
printf("\nProduct of a and b is : %d",a*b);
break;
case 4:
printf("\nDivision of a by b is : %d",a/b);
break;
case 5:
printf("\nRemainder of a and b is : %d",a%b);
break;
default:
printf("\nEnter the choice from 1 to 5");
break;
}
getch();
}
Output:
Ex. No. 2. d) DECISION-MAKING CONSTRUCTS – (break-continue)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,i;
printf("Enter the Number Range: ");
scanf("%d",&a);
printf("Enter the number to break: ");
scanf("%d",&b);
for(i=1;i<=a;i++){
if(i==b){
break;
}else{
printf("%d\n",i);
}
}
getch();
}
Output:
ii)Printing even numbers using continue
Algorithm:
Step 1: Start
Step 2: Get the number to print the even numbers
Step 3: Display the results
Step 4: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
printf("Even Numbers\nEnter the Number : ");
scanf("%d",&n);
for(i=0;i<=n;i++){
if(i%2!=0){
continue;
}else{
printf("%d\n",i);
}
}
getch();
}
Output:
Result:
Thus the C programs using Decision-making constructs were written, executed and the
output were verified successfully.
Ex. No. 3 LOOPS
Date:
Problem:
Write C programs using Loops.
a) for
i) Sum of n Numbers
ii) Print Star Pyramid
b) while
i) Reverse of a number
ii) Palindrome number or not
c) do-while
i) Sum of digits of a number
Aim:
To write C programs using Loops
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,i;
printf("Enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("\nSum of n numbers is : %d",sum);
getch();
}
Output:
ii)Print star pyramid
Algorithm:
Step 1: Start
Step 2: Get the number of rows to print the star pyramid
Step 3: Use for loops
Step 4: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k;
printf("\tSTAR PYRAMID\n\nEnter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=n-i;j>=1;j--){
printf(" ");
}
for(k=1;k<=i;k++){
printf("* ");
}
printf("\n");
}
getch();
}
Output:
Ex. No. 3. b) LOOPS (while)
i)Reverse of a number
Algorithm:
Step 1: Start
Step 2: Get an input
Step 3: Find the reverse of the number using the statements
rem=n%10
rev=(rev*10)+rem
n=n/10
Step 4: Print the reverse number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,rev=0,rem;
printf("Enter the number: ");
scanf("%d",&n);
while(n!=0){
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf("\nReverse of the number is : %d",rev);
getch();
}
Output:
ii)Palindrome number or not
Algorithm:
Step 1: Start
Step 2: Get an input
Step 3: Find the reverse of the number using the statements
rem=n%10
rev=(rev*10)+rem
n=n/10
Step 4: Check whether the given number is equal to the reverse number
i) If they are equal, then print that the number is Palindrome
ii) If they are not equal, then print that the number is not Palindrome
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,rev=0,rem,original;
printf("Enter a number : ");
scanf("%d",&n);
original=n;
while(n!=0){
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
if(original==rev){
printf("\nThe number is palindrome");
}else{
printf("\nThe number is not palindrome");
}
getch();
}
Output:
Ex. No. 3. c) LOOPS (do-while)
i)Sum of digits of a number
Algorithm:
Step 1: Start
Step 2: Get a number and initialize sum=0
Step 3: Find the sum of digits of a number using the statements
rem=n%10
sum=sum+rem
n=n/10
Step 4: Print the sum of digits of the number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,rem;
printf("Enter a number: ");
scanf("%d",&n);
do{
rem=n%10;
sum=sum+rem;
n=n/10;
}while(n!=0);
printf("\nThe Sum of digits of the number is : %d",sum);
getch();
}
Output:
Result:
Thus the C programs using Loops (for, while, do-while) were written, executed and the
output were verified successfully.
Ex. No. 4 ARRAYS
Date:
Problem:
Write C programs using Arrays.
a) i) 1D
1. Sum of Numbers
2. Linear Search
ii) 2D
1. Matrix Addition
2. Matrix Multiplication
b) Multi-dimensional arrays
1. Matrix Subtraction using 3D Array
c) Traversal
1. Selection Sort
Aim:
To write C programs using Arrays.
Ex. No. 4. a) i) 1) One Dimensional Array – Sum of Numbers
Algorithm:
Step 1: Start
Step 2: Get a number of elements in an array
Step 3: Get the values in the array
Step 4: Find the sum of values in the array using the statement sum=sum+a[i]
Step 5: Print the sum of values in the array
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,a[10],sum=0;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("\nEnter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
printf("\nSum of the numbers is : %d",sum);
getch();
}
Output:
Ex. No. 4. a) i) 2) One Dimensional Array – Linear Search
Algorithm:
Step 1: Start
Step 2: Get a number of elements in an array and a search element
Step 3: Compare the elements in the array with the search element using the statement if(a[i]==s)
i) If the element is present in the array, print that the element is found
ii) If the element is not present in the array, print that the element is not found
Step 4: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,l=0,a[10],s;
printf("Enter the number of elements in the Array : ");
scanf("%d",&n);
printf("\nEnter the elements : ");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter an element to Search : ");
scanf("%d",&s);
for(i=0;i<=n-1;i++)
{
if(a[i]==s)
{
l=1;
break;
}
}
if(l==1)
{
printf("\nThe element is found.");
}
else
printf("The element is not found.");
getch();
}
Output:
Ex. No. 4. a) ii) 1) Two Dimensional Array – Matrix Addition
Algorithm:
Step 1: Start
Step 2: Get the number of rows and columns in the matrices A and B
Step 3: Perform the addition using the statement a[i][j]+b[i][j]
Step 4: Display the result
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ar,ac,br,bc,cr,cc,i,j,a[10][10],b[10][10];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d",&ar,&ac);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d",&br,&bc);
cr=ar,cc=ac;
printf("\n");
for(i=0;i<=ar-1;i++)
{
for(j=0;j<=ac-1;j++)
{
printf("Enter the value of row %d and column %d of matrix A: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n");
for(i=0;i<=br-1;i++)
{
for(j=0;j<=bc-1;j++)
{
printf("Enter the value of row %d and column %d of matrix B: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("\n\nAddition of Matrix A and B:\n\n");
for(i=0;i<=cr-1;i++){
for(j=0;j<=cc-1;j++){
printf("%d\t",a[i][j]+b[i][j]);
}
printf("\n");
getch();
}
Output:
Algorithm:
Step 1: Start
Step 2: Get the number of rows and columns in the matrices A and B
Step 3: Perform the multiplication using the statement c[i][j]+=a[i][k]*b[k][j];
Step 4: Display the result
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ar,ac,br,bc,cr,cc,i,j,k,a[10][10],b[10][10],c[10][10];
printf("Enter the row and column of A : \n");
scanf("%d%d",&ar,&ac);
printf("Enter the row and column of B : \n");
scanf("%d%d",&br,&bc);
if(ac==br)
{
cr=ar,cc=bc;
printf("\nEnter the values of Matrix A\n\n");
for(i=0;i<=ar-1;i++){
for(j=0;j<=ac-1;j++){
printf("Enter a value %d %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the values of Matrix B\n\n");
for(i=0;i<=br-1;i++){
for(j=0;j<=bc-1;j++){
printf("Enter a value %d %d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
Output:
Ex. No. 4. b) Multi-Dimensional Array – Matrix Subtraction using 3D Array
Algorithm:
Step 1: Start
Step 2: Get the number of rows and columns in the matrices A and B
Step 3: Perform the subtraction using the statement a[i][j][k]-b[i][j][k]
Step 4: Display the result
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ax,ay,az,bx,by,bz,cx,cy,cz,i,j,k,a[10][10][10],b[10][10][10],c[10][10][10];
printf("Enter the dimensions of A : \n");
scanf("%d%d%d",&ax,&ay,&az);
printf("Enter the dimensions of B : \n");
scanf("%d%d%d",&bx,&by,&bz);
cx=ax,cy=ay,cz=az;
for(i=0;i<=ax-1;i++){
for(j=0;j<=ay-1;j++){
for(k=0;k<=az-1;k++){
printf("Enter a %d%d%d :",i+1,j+1,k+1);
scanf("%d",&a[i][j][k]);
}
}
}
printf("\n");
for(i=0;i<=bx-1;i++){
for(j=0;j<=by-1;j++){
for(k=0;k<=bz-1;k++){
printf("Enter b %d%d%d :",i+1,j+1,k+1);
scanf("%d",&b[i][j][k]);
}
}
}
printf("\nSubtraction of matrix A and B:\n");
for(i=0;i<=cx-1;i++){
for(j=0;j<=cy-1;j++){
for(k=0;k<=cz-1;k++){
printf("%d\t",a[i][j][k]-b[i][j][k]);
}
printf("\n");
}
printf("\n");
getch();
}
Output:
Algorithm:
Step 1: Start
Step 2: Get a number of elements in an array.
Step 3: Compare the elements in the array with each other and swap if one element is greater than
other element.
Step 4: Display the sorted array
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,a[10],temp;
printf("\n\tSelection Sort\n\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++) {
for(j=i+1;j<=n-1;j++) {
if(a[j]<a[i]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
printf("\n The sorted array is : \n\n");
for(i=0;i<=n-1;i++){
printf("%d ",a[i]);
}
getch();
}
Output:
Result:
Thus the C programs using Arrays were written, executed and the outputs were verified
successfully.
Ex. No. 5 STRINGS: OPERATIONS
Date:
Problem:
Write C programs for Operations in Strings.
a. String Palindrome
b. String Operations
Aim:
To write C programs for Operations in Strings.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10],s2[10];
int c=1;
printf("Enter a String : ");
scanf("%s",s1);
strcpy(s2,s1); //copy from s1 to s2
strrev(s1); //reversing string : s1
c=strcmp(s1,s2);
if(c==0)
{
printf("The String is Palindrome ");
}
else
printf("The String is not a Palindrome ");
getch();
}
Output:
Ex. No. 5. b) String Operations
Algorithm:
Step 1: Start
Step 2: Perform String Operations 1. Length of a String, 2.Concatenate, 3.Lower Case, 4.Upper Case
Step 3: Get a String and find the length of the string using strlen()
Step 4: Get two strings and perform the concatenation using strcat()
Step 5: Get a String and convert into lower case using strlwr()
Step 6: Get a String and convert into upper case using strupr()
Step 7: Display the results
Step 8: Stop
Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main(){
int choice=0;
char s1[10],s2[10];
printf(" String Operations\n\n1.Length of a String\n2.Concatenate\n3.Lower Case\n4.Upper Case\
n5.Exit\n");
while(choice!=5)
{
printf("\n\nEnter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a string: ");
scanf("%s",s1);
printf("The Length of the String : %d",strlen(s1));
break;
case 2:
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
strcat(s1,s2);
printf("String after concatenation : %s",s1);
break;
case 3:
printf("Enter a string: ");
scanf("%s",s1);
printf("Lower case of the string : %s",strlwr(s1));
break;
case 4:
printf("Enter a string: ");
scanf("%s",s1);
printf("Upper case of the string : %s",strupr(s1));
break;
case 5:
exit(1);
default:
printf("Enter the choice between 1 to 4 ");
break;
}
}
getch();
Output:
Result:
Thus the C programs for operations in Strings were written, executed and the outputs were
verified successfully.
Ex. No. 6 FUNCTIONS
Date:
Problem:
Write C programs using Functions.
a) call, return – Adding Two numbers (Function with Argument and Return Value)
b) Passing parameters by (value, reference) – Swapping two numbers
c) Passing arrays to function – Maximum element in an Array
Aim:
To write C programs using Functions.
Algorithm:
Step 1: Start
Step 2: Declare a function int add(int,int);
Step 3: Get two numbers a and b
Step 4: Call the function add(a,b)
Step 5: Print the result
Step 6: Stop
add(int,int)
Step 1: Add two numbers and assign the result to another variable
Step 2: Return the value of the result.
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int add(int,int); //Function Declaration
int a,b,c;
printf("\tFunction with Argument and Return Value");
printf("\n\nEnter two numbers: ");
scanf("%d %d",&a,&b);
c=add(a,b); //Function Call
printf("\nThe result is %d",c);
getch();
}
Algorithm:
Step 1: Start
Step 2: Declare a function int swap(int x,int y);
Step 3: Get two numbers a and b
Step 4: Call the function swap(a,b)
Step 5: Print the result
Step 6: Stop
swap(int x,int y)
Step 1: Interchange the two numbers
Step 2: Print the result.
Program:
#include<stdio.h>
#include<conio.h>
int swap(int x,int y);
main()
{
int a,b;
printf("\n\tPass by Value\n");
printf("\nEnter two Numbers: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping : a=%d b=%d",a,b);
swap(a,b);
printf("\nAfter Swapping :\n \t\t In Main() : a=%d b=%d",a,b);
getch();
}
int swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\n\nAfter Swapping :\n \t\t In swap() : x=%d y=%d",x,y);
}
Output:
swap(int* x,int* y)
Step 1: Interchange the two numbers
Step 2: Print the result.
Program:
#include<stdio.h>
#include<conio.h>
int swap(int*,int*);
main()
{
int a,b;
printf("\n\tPass by Reference\n");
printf("\nEnter two Numbers: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping : a=%d b=%d",a,b);
swap(&a,&b);
printf("\nAfter Swapping :\n \t\t In Main() : a=%d b=%d",a,b);
getch();
}
int swap(int* x,int* y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("\n\nAfter Swapping :\n \t\t In swap() : x=%d y=%d",*x,*y);
}
Output:
Algorithm:
Step 1: Start
Step 2: Declare a function int max(int*,int);
Step 3: Get the number of elements in the array and get the elements.
Step 4: Call the function max(a,n). a is an Array
Step 5: Print the result
Step 6: Stop
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int n,m,a[100],i;
int max(int*,int);
printf("\n\tPassing Arrays to Function\n");
printf("\nEnter the Number of Elements in the Array : ");
scanf("%d",&n);
printf("\nEnter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
m=max(a,n);
printf("\nMaximum element in an Array is %d\n",m);
getch();
}
int max(int* arr,int num)
{
int max_value, i;
max_value = arr[0];
for(i = 1; i < num; i++)
if(arr[i] > max_value)
max_value = arr[i];
return max_value;
}
Output:
Result:
Thus the C programs using Functions were written, executed and the outputs were verified
successfully.
Ex. No. 7 RECURSION
Date:
Problem:
Write a C program using Recursive Functions.
Aim:
To write a C program using Recursive Functions.
Algorithm:
Step 1: Start
Step 2: Declare a function long int fact(int);
Step 3: Get a number.
Step 4: Call the function fact(n).
Step 5: Print the result
Step 6: Stop
fact(int N)
Step 1: Find the factorial of the number using the recursive call of the function f1=N*fact(N-1)
Step 2: Return the result.
Program:
#include<stdio.h>
#include<conio.h>
long int fact(int);
main()
{
int n;
long int f;
printf("\tRECURSION: FUNCTION\n\n");
printf("Enter the number: ");
scanf("%d",&n);
f=fact(n);
printf("\nFactorial of %d is %ld",n, f);
getch();
}
Result:
Thus the C program using Recursive function was written, executed and the output was verified
successfully.
Ex. No. 8 POINTERS
Date:
Problem:
Write C programs using Pointers.
a) Pointers to functions
b) Arrays, Strings
c) Pointers to Pointers
d) Array of Pointers
Aim:
To write C programs using Pointers.
Array of Pointers
Algorithm:
Step 1: Start
Step 2: Declare the variables;
Step 3: initialize the array of Pointers with the addresses of the variables.
Step 4: Print the result
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
main( )
{ int a=10,b=20,c=30;
int *a1[]={&a,&b,&c};
printf("\nElements are:");
printf("\n%d %d %d",a,b,c);
printf("\n%d %d %d",*a1[0],*a1[1],*a1[2]);
getch();
}
Output:
Result:
Thus the C programs using Pointers were written, executed and the outputs were verified
successfully.
Ex. No. 9 STRUCTURES AND UNION
Date:
Problem:
Write C programs using Structures and Union.
a) Nested Structures
b) Pointers to Structures
c) Arrays of Structures and
d) Unions
Aim:
To write C programs using Structures and Union.
Pointers to Structures
Algorithm:
step 1: Start
step 2: Read the number of employees
step 3: Read allowances, deductions and basic for each employee.
Step 4: Calculate net pay= (basic+ allowances)-deductions
step 5: Display the output of the Pay slip calculations for each employee.
Step 6: Stop
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, HRA, DA, PF, IT, npay ;
};
main()
{
int i, n, allow,ded ;
struct emp *e;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
e = (struct emp*)malloc(sizeof(struct emp) * n);
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number %d: ",i+1) ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay:") ;
scanf("%d", &e[i].bpay) ;
e[i].DA = e[i].bpay*0.2;
e[i].HRA = e[i].bpay*0.1;
e[i].PF = e[i].bpay*0.05;
e[i].IT = e[i].bpay*0.01;
allow=e[i].DA+e[i].HRA;
ded=e[i].PF+e[i].IT;
e[i].npay = e[i].bpay + allow - ded ;
}
printf("\nEmp. No. Name \t Bpay \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf(" %d\t %s\t %d\t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].npay) ;
}
getch();
}
Output:
Union
Algorithm:
step 1: Start
step 2: Declare the variables in Union
Step 3: Read the details of employees
Step 4: Display the results
Step 5: Stop
Program:
union employee
{
char name[10];
int idno;
float salary;
}e;
main()
{
printf(“Enter the name\n”);
scanf(“%s”,e.name);
printf(“Enter the id number\n”);
scanf(“%d”,&e.idno);
printf(“Enter the salary\n”);
scanf(“%f”,&e.salary);
printf(“Name : %s\n”,e.name);
printf(“Id number : %d\n”,e.idno);
printf(“Salary : %f\n”,e.salary);
getch();
return;
}
Output :
Result:
Thus the C programs using Structures and Union were written, executed and the outputs were
verified successfully.
Ex. No. 10 FILES
Date:
Problem:
Write C programs using Files and file Operations.
a)reading and writing,
b)File pointers
c)file operations
d)random access
e) processor directives.
Aim:
To write C programs using Files and file Operations.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * ); void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)
{
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);
}
if (iSelection == 3)
{ PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
getch();
}
} while (iSelection <= 4);
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName,
phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter - 1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL)
{
perror("The following error occurred: ");
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}
}
fclose(pRead);
}
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}
Result:
Thus the C programs using Files and File Operations were written, executed and the outputs
were verified successfully.
Case Study: Ticket Reservation System for Transportation Services
1. Introduction
This case study examines the development of a **Ticket Reservation System** designed to
streamline booking processes for transportation services. The system was implemented as a
**C programming mini-project** to demonstrate the practical application of fundamental
programming concepts while addressing real-world booking challenges in the transport
sector.
2. Problem Statement
3. System Implementation
typedef struct {
int seatNo;
int booked;
char passenger[50];
} TransportTicket;
TransportTicket vehicle[CAPACITY];
void initializeSystem() {
for(int i=0; i<CAPACITY; i++) {
vehicle[i].seatNo = i+1;
vehicle[i].booked = 0;
strcpy(vehicle[i].passenger, "Vacant");
}
}
void showAvailability() {
printf("\nAvailable Seats:\n");
for(int i=0; i<CAPACITY; i++) {
if(!vehicle[i].booked) {
printf("%d ", vehicle[i].seatNo);
}
}
printf("\n");
}
```
4. Sample Output
```
=== TRANSPORT TICKET SYSTEM ===
1. Check Availability
2. Book Ticket
3. Cancel Booking
4. View All Bookings
5. Exit
Choice: 1
Available Seats: 1 2 3 4 5 6 ... 50
Choice: 2
Enter seat number: 15
Passenger name: Riya Sharma
Seat 15 booked for Riya Sharma
Choice: 4
CURRENT BOOKINGS:
Seat 15: Booked (Riya Sharma)
All other seats: Vacant
```
Limitations:
- No database persistence.
- Basic text interface.
- Limited to single vehicle.
- No payment integration.
- Fixed seating capacity.
6. Conclusion
This Ticket Reservation System successfully demonstrates:
- Practical **C programming implementation**.
- Effective **problem-solving approach**.
- Core **programming concept application**.
- Real-world **system development**.
The project serves as both an effective transportation solution and valuable programming
learning tool, showcasing how technical skills can solve practical industry challenges.
Prepared by:
Prabhakaran S
412624202021
IT - Section B
1st Year, II Semester
---