0% found this document useful (0 votes)
14 views61 pages

Manual

Uploaded by

cajilajayareeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views61 pages

Manual

Uploaded by

cajilajayareeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Ex No: 1 Implementation of I/O statements, operators and expressions

Aim:
To write C programs to implement I/O statements, operators and expressions.

Algorithm: 1 (a) Convert kilometers per hour to miles per hour


Step 1: Start
FORMULA:
Step 2: Get a float value kilo meter per hour as input
1 km/h = 0.621371 mph
Step 3: Calculate, miph = (kmph * 0.62)
Step 4: Display the value of mile per hour in miph
Step 5: Stop

Program:
#include <stdio.h>
void main()
{
float kmph, miph;
printf("Enter kilometers per hour: ");
scanf("%f", &kmph);
miph = (kmph * 0.62);
printf("%0.2f miles per hour\n", miph);
}
Output:
Enter kilometers per hour: 150
93.21 miles per hour
Algorithm: 1 (b) Convert the given temperature from Centigrade to Fahrenheit
Step 1: Start
Step 2: Get the temperature value in Centigrade as input
Step 3: Calculate, fah = ((9.0 / 5.0) * centi) + 32.0
Step 4: Display the Fahrenheit value in fah
Step 5: Stop

Program:
#include <stdio.h>
void main()
{
float fah, centi;
printf("Enter temperature in Centigrade ");
scanf("%f", &centi);
fah = ((9.0 / 5.0) * centi) + 32.0;
printf(".2%f degrees Fahrenheit\n", fah);
}
Output:
Enter temperature in Centigrade 89.34
192.81 degrees Fahrenheit

Algorithm: 1 (c) Convert the given time in minutes to Hours and Minutes
Step 1: Start
Step 2: Get the total minutes as input
Step 3: Divide total minutes by 60 and take Quotient as hours, remainder as Minutes.
Step 4: Display the result in hours and minutes format.
Step 5: Stop
Program:
#include <stdio.h>
void main()
{
int totalmin, hour,min;
printf("Enter Minutes: ");
scanf("%d", &totalmin);
hour = totalmin / 60;
min = totalmin % 60;
printf("%d Hours \t %d Minutes", hour, min);
}
Output:
Enter Minutes: 345
5 Hours 45 Minutes

Result:
Thus the above programs to implement I/O statements, operators and expressions were
done and executed successfully.
Ex No: 2 Implementation of Decision Making Constructs

Aim:
To write C programs to implement Decision Making Constructs such as if – else and
switch.

Algorithm: 2 (a) Check a given number is even or odd


Step 1: Start
Step 2: Get a number as input
Step 3: If the number is divided by 2 and the remainder is equal to zero then,
Step 3.1: Display the number is Even
Step 3.2: otherwise Display the number is Odd.
Step 4: Stop

Program:
#include <stdio.h>
void main()
{
int num;
printf("Enter a Number : ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is an even Number", num);
else
printf("%d is an odd Number", num);
}
Output:
Execution: 1
Enter a Number: 45
45 is an odd Number

Execution: 2
Enter a Number: 20
20 is an even Number

Algorithm: 2 (b) Find a given year is a leap year or not


Step 1: Start
Step 2: Get a year as input
Step 3: If the year is divided by 4 or 400 and the remainder is equal to zero then,
Step 3.1: Display the Year is Leap Year
Step 4: If the year is divided by 100 and the remainder is equal to zero then,
Step 4.1: Display the Year is not a Leap Year.
Step 5: Stop

Program:
#include <stdio.h>
void main()
{
int year;
printf("Enter a year : ");
scanf("%d", &year);
if ((year % 4) == 0)
printf("%d is a leap year.\n", year);
else if ((year % 400) == 0)
printf("%d is a leap year.\n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year.\n", year);
else
printf("%d is not a leap year \n", year);
}

Output:
Execution: 1
Enter a year : 2024
2024 is a leap year.

Execution: 2
Enter a year : 1400
1400 is a leap year.

Algorithm: 2 (c) Program to accept a coordinate point in a XY coordinate system and


determine in which quadrant the coordinate point lies
Step 1: Start
Step 2: Get two co-ordinates of graph as input
Step 3: Check if( c1 > 0 && c2 > 0) Display the points lies in the First quadrant
Step 4: otherwise if( c1 < 0 && c2 > 0) Display the points lies in Second quadrant
Step 5: otherwise if( c1 < 0 && c2 < 0) Display the points lies in Third quadrant
Step 6: otherwise if( c1 > 0 && c2 < 0) Display the points lies in Fourth quadrant
Step 7: otherwise Display the points lies in the Origin
Step 8: Stop
Program:
#include <stdio.h>
void main()
{
int c1,c2;
printf("Input the values for X and Y coordinate : ");
scanf("%d %d",&c1,&c2);
if( c1 > 0 && c2 > 0)
printf("The coordinate point (%d, %d) lies in First quadrant",c1,c2);
else if( c1 < 0 && c2 > 0)
printf("The coordinate point (%d, %d) lies in Second quadrant",c1,c2);
else if( c1 < 0 && c2 < 0)
printf("The coordinate point (%d, %d) lies in Third quadrant",c1,c2);
else if( c1 > 0 && c2 < 0)
printf("The coordinate point (%d, %d) lies in Fourth quadrant",c1,c2);
else
printf("The coordinate point (%d, %d) lies at the origin",c1,c2);
}

Output:
Execution: 1
Input the values for X and Y coordinate : 3 -6
The coordinate point (3,-6) lies in Fourth quadrant
Execution: 2
Input the values for X and Y coordinate : 0 0
The coordinate point (0,0) lies at the origin
Algorithm: 2 (d) Find roots of a Quadratic Equation
Step 1: Start
Step 2: Get three values as input
Step 3: Calculate d=b*b-4*a*c
Step 4: Check if (d==0)
Step 4.1: Calculate x1=x2= -b/(2.0*a)
Step 4.2: Display values of x1 and x2 with Roots are Equal
Step 5: otherwise if(d>0)
Step 5.1: Calculate x1=(-b+sqrt(d))/(2*a)
Step 5.2: Calculate x2=(-b-sqrt(d))/(2*a)
Step 5.3: Display values of x1 and x2 with roots are Real and Different
Step 6: otherwise Display Root are imaginary
Step 7: Stop

Program:
#include <stdio.h>
void main()
{
int a, b, c, d;
float x1, x2;
printf("Enter three values : ");
scanf("%d %d %d", &a, &b, &c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal\n");
x1=-b/(2.0*a);
x2=x1;
printf("Root1= %f\n",x1);
printf("Root2= %f",x2);
}
else if(d>0)
{
printf("Both roots are real and different\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("Root1= %f\n",x1);
printf("Root2= %f",x2);
}
else
printf("Root are imaginary");
}

Output:
Execution: 1
Enter three values : 1 2 1
Both roots are equal
Root1= -1.000000
Root2= -1.000000
Execution: 2
Enter three values : 2 4 1
Both roots are real and different
Root1= -0.292893
Root2= -1.707107
Execution: 3
Enter three values : 4 3 2
Root are imaginary

Algorithm: 2 (e) Arithmetic Calculator using switch statement


Step 1: Start
Step 2: Get three values a, b, choice as input
Step 3: Based on the choice value any one of the switch case will execute.
Step 4: If choice is 1 Addition will be done by case 1
Step 5: If choice is 2 Subtraction will be done by case 2
Step 6: If choice is 3 Multiplication will be done by case 3
Step 7: If choice is 4 Division will be done by case 4
Step 8: otherwise default case will get execute.
Step 9: Stop

Program:
#include <stdio.h>
void main()
{
int a, b;
int choice;
printf(" 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d", &a, &b);
printf("Enter your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Sum = %d", a+b);
break;
case 2:
printf("Difference = %d", a-b);
break;
case 3:
printf("Product = %d", a*b);
break;
case 4:
printf("Quotient = %d", a/b);
break;
default :
printf(" Enter Your Correct Choice");
break;
}
}

Output:
Execution: 1
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter the values of a & b: 24 5
Enter your Choice : 4
Quotient = 4
Execution: 2
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 23 78
Enter your Choice : 6
Enter Your Correct Choice

Algorithm: 2 (f) Find a Character is Vowel or Consonant


Step 1: Start
Step 2: Get a character as input.
Step 3: Based on the input character, If it is a, e, i, o, u, A, E, I, O, U Display the character is
Vowel.
Step 8: otherwise default case will get execute and Display the character is Consonant.
Step 9: Stop

Program:
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is an Vowel", ch);
break;
default:
printf("%c is a Consonant", ch);
}
}

Output:
Execution: 1
Enter any character: a
a is an Vowel
Execution: 2
Enter any character: g
g is a Consonant

Result:
Thus the above programs to implement Decision Making Constructs such as if – else and
switch statements were done and executed successfully.
Ex No: 3 Implementation of Looping Constructs

Aim:
To write C programs to implement Looping constructs such as for, while, do-while loops.

Algorithm: 3(a) check a given number is an Armstrong number or not


Step 1: Start
Step 2: Get a number as input and assign temp=num.
Step 3: Initialize sum=0 and n=0
Step 4: Repeat num=num/10; and ++n till num!=0
Step 5: Assign num=temp
Step 6: Repeat the steps 6.1 to 6.3 till num>0
Step 6.1: r = num % 10
Step 6.2: sum = sum+ pow(r, n)
Step 6.3: num = num/10
Step 7: Check if(sum== temp) Display Number is Armstrong
Step 8: otherwise Display Number is not Armstrong
Step 9: Stop

Program:
#include <stdio.h>
#include <math.h>
void main()
{
int num, temp, r, sum = 0, n = 0 ;
printf(" Input an integer : ");
scanf("%d", &num);
temp = num;
while (num!=0)
{
num=num/10;
++n;
}
num=temp;
do
{
r = num % 10;
sum = sum+ pow(r, n);
num = num/10;
} while(num>0);
if(sum == temp)
printf("%d is an Armstrong number", sum);
else
printf("%d is not an Armstrong number", sum);
}
Output: Execution: 1
Enter a number: 153
153 is an Armstrong number
Execution: 2
Enter a number: 172
172 is not an Armstrong number
Algorithm: 3(b) check a given number is an palindrome number or not
Step 1: Start
Step 2: Get a number as input and assign temp=num.
Step 3: Initialize sum=0
Step 4: Repeat the steps 4.1 to 4.3 till num>0
Step 4.1: r = num % 10
Step 4.2: sum = sum* 10+r
Step 4.3: num = num/10
Step 5: Check if(sum== temp) Display Number is Palindrome
Step 6: otherwise Display Number is not Palindrome
Step 7: Stop

Program:
#include <stdio.h>
void main()
{
int num, r, sum=0,temp;
printf("Enter a number: ");
scanf("%d", &num);
temp=num;
do
{
r=num % 10;
sum=sum*10+r;
num=num/10;
} while(num>0);
if(sum==temp)
printf("%d is an Palindrome number", temp);
else
printf("%d is not an Palindrome number", temp);
}

Output: Execution: 1
Enter a number: 121
121 is an Palindrome number
Execution: 2
Enter a number: 178
178 is not an Palindrome number

Algorithm: 3(c) Print Sum of individual Digits


Step 1: Start
Step 2: Get a number as input
Step 3: Initialize sum=0
Step 4: Repeat the steps 4.1 to 4.3 till num>0
Step 4.1: r = num % 10
Step 4.2: sum = sum + r
Step 4.3: num =num/10
Step 5: Display the Sum of individual digits in variable sum.
Step 6: Stop

Program:
#include <stdio.h>
void main()
{
int num, r, sum=0,temp;
printf("Enter a number: ");
scanf("%d", &num);
do
{
r = num % 10;
sum = sum + r;
num = num / 10;
} while(num>0);
printf("Sum of digits = %d", sum);
}
Output:
Enter a number: 2367
Sum of digits = 18

Algorithm: 3(d) Person eligible to vote or not using goto statement


Step 1: Start
Step 2: Get age as input
Step 3: if(age>=18) goto Vote and Display Person eligible to vote
Step 4: otherwise goto Nonvote and Display Person not eligible to vote
Step 5: Stop

Program:
#include <stdio.h>
void main()
{
int age;
printf("Enter Age of a person\n");
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto Nonvote;
Vote:
printf("Person eligible to vote");
return;
Nonvote:
printf("Person not eligible to vote");
}
Output: Execution: 1
Enter Age of a person
12
Person not eligible to vote
Execution: 2
Enter Age of a person
25
Person eligible to vote

Algorithm: 3(e) Find a Number is Prime or not


Step 1: Start
Step 2: Get a number as input
Step 3: Inside a loop check the number is divisible by one and the number if so do c++
Step 4: check if(c==2) Display the number is Prime
Step 5: otherwise Display the number is not Prime
Step 6: Stop

Program:
#include<stdio.h>
void main()
{
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
printf("%d is a Prime number", n);
else
printf("%d is not a Prime number", n);
}
Output: Execution: 1
Enter a number 13
13 is a Prime number
Execution: 2
Enter a number 22
22 is not a Prime number
Algorithm: 3(f) Program using break and continue to find sum of 10 Non-Negative Numbers.
Step 1: Start
Step 2: Inside a loop get 10 numbers as input.
Step 2.1: While getting input if(num<0) continue to get next input number
Step 2.2: otherwise Calculate sum=sum+num and increment c
Step 2.3: if(c==10) Exit the Loop using break
Step 3: Display the value of sum
Step 4: Stop

Program:
#include <stdio.h>
void main()
{
int num, sum = 0, i, j, c=0;
printf("Enter Numbers\n");
for(i = 1; i <= 100; i++)
{
printf("Number %d = ", i);
scanf("%d", &num);
if(num < 0)
{
continue;
}
sum = sum + num;
c++;
if(c==10)
{
break;
}
}
printf("Sum of 10 Non-Negative Numbers = %d", sum);
}
Output:
Enter the Numbers
Number 1 = 3
Number 2 = 4
Number 3 = -5
Number 4 = -6
Number 5 = 2
Number 6 = -6
Number 7 = 7
Number 8 = 8
Number 9 = -1
Number 10 = -45
Number 11 = 6
Number 12 = 8
Number 13 = 7
Number 14 = 4
Number 15 = -3
Number 16 = 9
Sum of 10 Non-Negative Numbers = 58

Result:
Thus the above programs to implement Looping constructs such as for, while, do-while
loops were done and executed successfully.
Ex No: 4 Implementation of Arrays

Aim:
To write C programs to implement Single dimensional, Two dimensional and Multi
dimensional Arrays.

Algorithm: 4(a) Single Dimensional Array (Sorting and Finding Minimum and Maximum
element)
Step 1: Start
Step 2: Get Array size as input
Step 3: Get Array elements as input
Step 4: Using Nested for loops check each element of the arrays.
Step 5: While checking if comparing element is greater than other the swap them and continue
the same with other elements too.
Step 6: After loop ends, Display the Sorted array
Step 7: Display the first and last element as the Minimum and Maximum element.
Step 8: Stop

Program:
#include<stdio.h>
void main()
{
int arr1[100], n, i, j, tmp;
printf("Enter array size: ");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements in Ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\nMaximum array element is %d", arr1[n-1]);
printf("\nMinimum array element is %d", arr1[0]);
}

Output:
Enter array size: 5
Input 5 elements in the array:
67
98
32
45
12
Elements in Ascending order:
12 32 45 67 98
Maximum array element is 98
Minimum array element is 12

Algorithm: 4(b) Two Dimensional Array (Matrix Addition)


Step 1: Start
Step 2: Get the row and column size as input for each matrix
Step 3: Get input for x and y two dimensional arrays using two for loops.
Step 4: if((r1==r2)and(c1==c2)) is True
Step 4.1: Using nested for loops calculate, z[i][j]=x[i][j]+y[i][j]
Step 4.2: Display the resultant matrix
Step 5: otherwise Display Matrix Orders are Different
Step 6: Stop

Program:
#include<stdio.h>
void main()
{
int x[10][10],y[10][10],z[10][10],r1,c1,r2,c2,i,j,k;
printf("Enter first matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r1);
printf("Enter number of columns =");
scanf("%d",&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&x[i][j]);
}
}
printf("Enter second matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r2);
printf("Enter number of columns =");
scanf("%d",&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&y[i][j]);
}
}
if((r1==r2)and(c1==c2))
{
printf("Sum of two matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
z[i][j]=x[i][j]+y[i][j];
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",z[i][j]);
}
printf("\n");
}
}
else
{
printf("The Matrix Orders are Different");
}
}

Output:
Enter first matrix elements
Enter number of rows = 3
Enter number of columns =3
1
2
3
4
5
6
7
8
2
Enter second matrix elements
Enter number of rows = 3
Enter number of columns =3
4
5
6
4
5
6
7
6
5
Sum of two matrices
5 7 9
8 10 12
14 14 7

Algorithm: 4(c) Two Dimensional Array (Matrix Multiplication)


Step 1: Start
Step 2: Get the row and column size as input for each matrix
Step 3: Get input for x and y two dimensional arrays using two for loops.
Step 4: if(c1==r2) is True
Step 4.1: Using nested for loops calculate, z[i][j]+=x[i][k]*y[k][j]
Step 4.2: Display the resultant matrix
Step 5: otherwise Display Matrix Orders are Different
Step 6: Stop

Program:
#include<stdio.h>
void main()
{
int x[10][10],y[10][10],z[10][10],r1,c1,r2,c2,i,j,k;
printf("Enter first matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r1);
printf("Enter number of columns =");
scanf("%d",&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d", &x[i][j]);
}
}
printf("Enter second matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r2);
printf("Enter number of columns =");
scanf("%d",&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d", &y[i][j]);
}
}
if(c1==r2)
{
printf("Product of two matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
z[i][j]=0;
for(k=0;k<c2;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t", z[i][j]);
}
printf("\n");
}
}
else
{
printf("The Matrix Orders are Different");
}
}

Output:
Enter first matrix elements
Enter number of rows = 3
Enter number of columns =3
1
2
3
4
5
6
7
8
9
Enter second matrix elements
Enter number of rows = 3
Enter number of columns =3
2
3
2
3
4
5
6
2
4
Product of two matrices
26 17 24
59 44 57
92 71 90

Algorithm: 4(d) Multi Dimensional Array


Step 1: Start
Step 2: Get input for a three dimensional arrays using three for loops.
Step 3: Display the resultant three dimensional matrix using three for loops again.
Step 4: Stop

Program:
#include<stdio.h>
void main()
{
int i, j, k;
int arr[2][2][2];
printf("Enter elements for 3D Matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
scanf("%d",&arr[i][j][k]);
}
}
}
printf("3D Matrix is \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("-------------\n");
}
}

Output:
Enter elements for 3D Matrix
11
12
13
14
1
2
3
4
3D Matrix is
11 12
13 14
-------------
1 2
3 4
-------------

Result:
Thus the above programs to implement Single dimensional, Two dimensional and Multi
dimensional Arrays were done and executed successfully.
Ex No: 5 Implementation of String Functions

Aim:
To write C programs to implement String Functions.

Algorithm: 5(a) String Concatenation, Comparison, Length, Copy


Step 1: Start
Step 2: Get two strings as input
Step 3: Using strcat() concatenate two string.
Step 4: Using strcpy() copy the one string with other string
Step 5: Using strlen() find length of a string.
Step 6: Using strcmp() compare two string
Step 7: After each action display the result.
Step 8: Stop

Program:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],str3[10];
int len ;
printf("Enter two Strings\n");
scanf("%s %s",str1,str2);
strcat( str1, str2);
printf("Concatenated String : %s\n", str1 );
strcpy(str3, str1);
printf("Copied String: %s\n", str3 );
len = strlen(str1);
printf("Length of the String: %d\n", len );
if(strcmp(str1,str2)==0)
printf("String 1 and String 2 are same”);
else
printf("String1 and String 2 are different”);
}

Output:
Enter two Strings
Hello
Welcome
Concatenated String: HelloWelcome
Copied String: HelloWelcome
Length of the String: 12
String1 and string 2 are different

Algorithm: 5(b) String to Lower case and Upper Case


Step 1: Start
Step 2: Get a string as input
Step 3: Using strupr() convert the string in lower case to upper case and display it.
Step 4: Using strlwr() convert the string in upper case to lower case and display it.
Step 5: Stop

Program:
#include<stdio.h>
#include<string.h>
void main()
{
char s[100];
printf("Enter a string: ");
scanf("%[^\n]",s);
printf("In Upper Case:\n");
puts(strupr(s));
printf("In Lower Case:\n");
puts(strlwr(s));

Output:
Enter a string: Hai WELCOME to C proGRamming
In Upper Case:
HAI WELCOME TO C PROGRAMMING
In Lower Case:
hai welcome to c programming

Algorithm: 5(c) Find number of vowels, consonants, digits and white spaces in a string
Step 1: Start
Step 2: Get a string as input
Step 3: Read the string character by character
Step 4: Check each character for it as vowel, consonant, digit and whitespace and count it using
variable.
Step 5: Display the count value.
Step 6: Stop
Program:
#include<stdio.h>
void main()
{
char str[200];
int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A'
||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U')
{
vowels++;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonants++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if (str[i]==' ')
{
spaces++;
}
else
{
specialCharacters++;
}
}
printf("\nVowels = %d", vowels);
printf("\nConsonants = %d", consonants);
printf("\nDigits = %d", digits);
printf("\nWhite spaces = %d", spaces);
printf("\nSpecial characters = %d", specialCharacters);
}

Output:
Enter a string
Hai 123 How are You?
Vowels = 7
Consonants = 5
Digits = 3
White spaces = 4
Special characters = 1

Result:
Thus the above programs to implement String Functions were done and executed
successfully.

Ex No: 6 Implementation of User-defined Functions


Aim:
To write C programs to implement user-defined Functions.

Algorithm: 6(a) Swap two numbers


Step 1: Start
Step 2: Get two numbers as input
Step 3: Call the function swap() using address of two variables
Step 4: Display the values before and after swapping
Step 5: Stop

Program:
#include<stdio.h>
void swap(int *p , int *q);
void main()
{
int n1,n2;
printf("Enter number 1 : ");
scanf("%d",&n1);
printf("Enter number 2 : ");
scanf("%d",&n2);
printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
}
void swap(int *p, int *q)
{
int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}

Output:
Enter number 1 : 34
Enter number 2 : 56
Before swapping: n1 = 34, n2 = 56
After swapping: n1 = 56, n2 = 34

Algorithm: 6(b) Greatest among three Numbers


Step 1: Start
Step 2: Get three float numbers as input
Step 3: Call the function large() by passing three float values.
Step 4: Inside the function find the largest value and return it to main function.
Step 5: Display the largest value
Step 6: Stop

Program:
#include<stdio.h>
float large(float a, float b, float c)
{
if(a>b && a>c)
return a;
else if( b>c)
return b;
else
return c;
}
void main()
{
float num1, num2, num3, largest;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
largest = large(num1, num2, num3);
printf("Largest number = %.2f",largest);
}

Output:
Enter three numbers: 23.4
78.6
0.2
Largest number = 78.60

Algorithm:
Step 1: Start
Step 2: Get a number as input
Step 3: Call the function Sum() by passing a value
Step 4: Inside the function using a for loop find the sum of n numbers and display it
Step 5: Stop

Program: 6(c) Find Sum of n Numbers


#include <stdio.h>
void Sum(int n);
void main()
{
int i, n;
printf("Enter a Number: ");
scanf("%d", &n);
Sum(n);
}
void Sum(int n)
{
int i, s=0;
for (i = 1; i <= n; i++)
{
s = s + i;
}
printf("\nThe Sum is : %d\n", s);
}

Output:
Enter a Number: 15
The Sum is : 120

Result:
Thus the above programs to implement user defined functions were done and executed
successfully.

Ex No: 7 Implementation of Recursive Functions


Aim:
To write C programs to implement Recursive Functions.

Algorithm:
Program: 7(a) Display Factorial of a Number
#include<stdio.h>
int factorial(int n);
void main()
{
int n, i;
printf("Enter a number: ");
scanf("%d",&n);
printf("Factorial of %d is: %d ", n, factorial(n));
}

int factorial(int n)
{
if((n==1)||(n==0))
return 1;
else
return (n* factorial(n-1));
}
Output:
Enter a number: 5
Factorial of 5 is: 120
Algorithm:
Program: 7(b) Display Fibonacci Series
#include<stdio.h>
int fibonacci(int);
void main()
{
int n, i;
printf("Enter the number of element in series :\n");
scanf("%d",&n);
printf("Fibonacci series is: \n");
for(i=0;i<n;i++)
{
printf("%d\t ",fibonacci(i));
}
}

int fibonacci(int i)
{
if(i==0)
return 0;
else if(i==1)
return 1;
else
return (fibonacci(i-1)+fibonacci(i-2));
}
Output:
Enter the number of element in series:
10
Fibonacci series is:
0 1 1 2 3 5 8 13 21 34

Result:
Thus the above programs to implement Recursive Functions were done and executed
successfully.

Ex No: 8 Implementation of Pointers


Aim:
To write C programs to implement Pointers to functions, Arrays, Strings etc.
Algorithm:
Program: 8(a) Reverse a String
#include<stdio.h>
char* Reverse(char[]);
void main()
{
char s[25],*r;
printf("Enter a string: ");
scanf("%s", s);
r = Reverse(s);
printf("The reversed string is: %s", r);
}
char* Reverse(char s[])
{
static int i;
static char r[25];
if(*s)
{
Reverse(s+1);
r[i++] = *s;
}
return r;
}
Output:
Enter a string: Programming
The reversed string is: gnimmargorP

Algorithm:
Program: 8(b) Transpose of a Matrix using Pointers to Arrays
#include<stdio.h>
int main()
{
int *x[10][10],r,c,i,j;
printf("Enter number of rows = ");
scanf("%d",&r);
printf("Enter number of columns =");
scanf("%d",&c);
printf("Enter the matrix elements \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);
}
}
printf("Transpose of the matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",*(*(x+j)+i));
}
printf("\n");
}
}
Output:
Enter number of rows = 3
Enter number of columns =3
Enter the matrix elements
1
1
1
2
2
2
3
3
3
Transpose of the matrix
1 2 3
1 2 3
1 2 3

Algorithm:
Program: 8(c) Sorting Names using Array of Pointers
#include<stdio.h>
#include<conio.h>
#include<string.h>
void Sort(int n,char *x[]);
void main()
{
char *x[20];
int i,n=0;
printf("Enter no. of String : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}
Sort(n,x);
printf("\nSorted Strings\n");
for(i=0;i<n;i++)
{
printf("%s\t", x[i]);
}
}
void Sort(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);

}
}
Output:
Enter no. of String : 5
Enter the Strings 1 : Sara
Enter the Strings 2 : Beni
Enter the Strings 3 : Veni
Enter the Strings 4 : Anju
Enter the Strings 5 : Devi
Sorted Strings
Anju Beni Devi Sara Veni

Result:
Thus the above programs to implement Pointers were done and executed successfully.

Ex No: 9 Implementation of Structures


Aim:
To write C programs to implement Structure and Union.

Algorithm:
Program: 9(a) Student details using Nested Structure
#include<stdio.h>
struct student
{
char name[30];
int roll;
struct address
{
int houseno;
char street[20];
int pincode;
};
struct address addr;
};
void main()
{
struct student stud;
printf("Enter name and roll number of student:\n");
scanf("%s %d",stud.name, &stud.roll);
printf("Enter street name, house number and Pin Code:\n");
scanf("%s %d %d",stud.addr.street, &stud.addr.houseno, &stud.addr.pincode);
printf("Student detail is:\n");
printf("Name: %s\nRoll: %d\n", stud.name, stud.roll);
printf("Address:\n");
printf("%s,\n%d,\n%d.",stud.addr.street, stud.addr.houseno, stud.addr.pincode);
}
Output:
Enter name and roll number of student:
Anju 23
Enter street name, house number and Pin Code:
NewStreet 234 629003
Student detail is:
Name: Anju
Roll: 23
Address:
NewStreet,
234,
629003.

Algorithm:
Program: 9(b) Employee details using Array of Structure
#include<stdio.h>
struct employee
{
char name[20];
char designation[20];
float salary;
}s[5];
void main()
{
int i;
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Enter name :");
scanf("%s", s[i].name);
printf("Enter designation :");
scanf("%s", s[i].designation);
printf("Enter salary :");
scanf("%f", &s[i].salary);
}
printf("\nThe details of employees are :\n");
printf("NAME\tDESIGNATION\tSALARY\n\n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t%.0f\n", s[i].name, s[i].designation, s[i].salary);
}
}
Output:
Enter details of 5 employees:
Enter name :A
Enter designation :Manager
Enter salary :50000
Enter name :B
Enter designation :AssManager
Enter salary :40000
Enter name :C
Enter designation :Clerk
Enter salary :25000
Enter name :D
Enter designation :Officer
Enter salary :35000
Enter name :E
Enter designation :OfficeManager
Enter salary :38000

The details of employees are :


NAME DESIGNATION SALARY

A Manager 50000
B AssManager 40000
C Clerk 25000
D Officer 35000
E OfficeManager 38000

Algorithm:
Program: 9(c) Student Details using Structure and Pointers
#include <stdio.h>
struct student
{
char name[30];
int roll;
float perc;
};

void main()
{
struct student stu;
struct student *ptr;
ptr= &stu;
printf("Enter details of student: ");
printf("\nName :");
gets(ptr->name);
printf("Roll No :");
scanf("%d",&ptr->roll);
printf("Percentage :");
scanf("%f",&ptr->perc);
printf("\nEntered details: ");
printf("\nName:%s \nRollNo: %d \nPercentage: %.2f\n",ptr->name,ptr->roll,ptr->perc);
}
Output:
Enter details of student:
Name :Anju
Roll No :12
Percentage :87.45

Entered details:
Name:Anju
RollNo: 12
Percentage: 87.45

Algorithm:
Program: 9(d) Implement Union
#include <stdio.h>
union pack
{
char a;
int b;
float c;
};
void main()
{

union pack p;
printf("\nOccupied size by union pack: %d",sizeof(p));
p.a='A';
printf("\nValue of a:%c",p.a);
p.b=10;
printf("\nValue of b:%d",p.b);
p.c=12345.6790;
printf("\nValue of c:%f",p.c);
printf("Initializing values together:\n");
p.a='A';
p.b=10;
p.c=12345.6790;
printf("\nValue of a:%c\n b:%d\n c:%f", p.a,p.b,p.c);
}
Output:
Occupied size by union pack: 4
Value of a: A
Value of b: 10
Value of c: 12345.678711
Initializing values together:
Value of a: ╖
b: 1178658487
c: 12345.678711

Result:
Thus the above programs to implement Structure and Union were done and executed
successfully.

Ex No: 10 Implementation of File


Aim:
To write C programs to implement File Access and Processor Directives.

Algorithm:
Program: 10(a) Store Student details in File
#include<stdio.h>
void main()
{
FILE *f1;
char s[10][10];
int i,n,r[10];
printf("Enter no of students\n");
scanf("%d",&n);
printf("Enter content for file1 : \n");
f1=fopen("f1.txt","w");
for(i=1;i<=n;i++)
{
printf("Enter name and rollno\n");
scanf("%s%d",s[i],&r[i]);
fprintf(f1,"%s\n",s[i]);
fprintf(f1,"%d",r[i]);
}
fclose(f1);
f1=fopen("f1.txt","r");
printf("\nName\tRollnumber\n-----\t----------\n");
for(i=1;i<=n;i++)
{
fscanf(f1,"%s",s[i]);
fscanf(f1,"%d",r[i]);
printf("%s\t%d\n",s[i],r[i]);
}
fclose(f1);
}
Output:
Enter no of students
2
Enter content for file1:
Enter name and rollno
Anju 12
Enter name and rollno
Arya 23
Name Rollnumber
------ --------------
Anju 12
Arya 23

Algorithm:
Program: 10(b) Random Access on File
#include <stdio.h>
void main ()
{
char name [20];
int age;
FILE *fp;
fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", "CProgram", 25);
printf("Current cursor position= %d\n",ftell(fp));
fseek(fp, 5,0);
printf("After seeking cursor position= %d\n",ftell(fp));
rewind (fp);
printf("After Rewind cursor position= %d\n",ftell(fp));
fclose(fp);
fp = fopen ("test.txt","r");
fscanf (fp, "%s %d",name,&age);
printf ("Name: %s\nAge: %d\n", name, age);
fclose(fp);
}
Output:
Current cursor position= 11
After seeking cursor position= 5
After Rewind cursor position= 0
Name: CProgram
Age: 25

Algorithm:
Program: 10(c) Area of Circle using Preprocessor #define
#include<stdio.h>
#include<conio.h>
#define AREA(a) (3.14 * a * a)
void main()
{
float r, x;
clrscr();
printf("Enter radius value: ");
scanf("%f",&r);
x = AREA (r);
printf ("Area of circle = %f", x);
getch();
}
Output:
Enter radius value: 7
Area of circle = 153.86

Algorithm:
Program: 10(d) Biggest among two numbers using #if, #else, #endif
#include <stdio.h>
#include<conio.h>
#define X 10
#define Y 5
void main()
{
clrscr();
#if X>Y
printf("%d is Big", X);
#else
printf("%d is Big", Y);
#endif
getch();
}
Output:
10 is Big

Result:
Thus the above programs to implement File Access and Processor Directives were done
and executed successfully.

You might also like