C Programming Lab Manual.docx
C Programming Lab Manual.docx
DATE:
Aim:
To write an algorithm and pseudocode for finding the area of square and circle
Algorithm:
STEP 1: Start the program
STEP 2: Get the length of a square and radius of a circle
STEP 3: area of square = length * length and area of circle = pi * r * r
STEP 4: print area of square and area of circle
STEP 5: stop the program
Flowchart:
Pseudocode :
START
DISPLAY "Enter the Length and radius - "
READ length, radius
area of square = length * length
area of circle = pi * radius * radius
DISPLAY area of square, area of circle
END
Result :
Thus an algorithm and pseudocode for finding the area of square and circle is written.
EX NO: 1B GIVEN NUMBER IS EVEN OR ODD
DATE:
Aim:
To write an algorithm and pseudocode for finding whether the given number is even or odd.
Algorithm:
STEP 6: Start the program
STEP 7: Get the number to find even or odd
STEP 8: rem = number %2
STEP 9: if rem == 0 print even
STEP 10: else print odd
STEP 11: stop the program
Flowchart:
Pseudocode :
START.
DISPLAY "Enter the Number - "
READ number.
IF number MOD 2 = 0 THEN.
DISPLAY "Number is Even"
ELSE.
DISPLAY "Number is Odd"
END IF.
Result :
Thus an algorithm and pseudocode for finding whether the given number is even or odd is
written.
EX NO: 2A FINDING AVERAGE OF MARKS
DATE:
Aim:
To write a c program to find the average of a student's marks.
Algorithm:
STEP 12: Start the program
STEP 13: Get student marks to find average
STEP 14: Find total = m1+m2+m3
STEP 15: Calculate average = total/3
STEP 16: Print the average
STEP 17: stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3;
int tot;
float avg;
clrscr();
scanf(“%d %d %d”,&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3;
printf(“%f”,avg);
getch();
}
Output:
87
87
97
90.0000
Result:
Thus a C program to find the average of a student's marks is written and executed
successfully.
EX NO:2B SWAPPING (WITH TEMPORARY VARIABLE)
DATE:
Aim:
To swap the values of two variables using a C program.
Algorithm:
STEP 1: Start the program
STEP 2: Get the values of x and y
STEP 3: Print the entered values of x and y
STEP 4: Swap the values of x and y by
t=x
x=y
y=t
STEP 5: print after swapping values of x and y
STEP 6: Stop the program.
Flowchart :
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,t;
clrscr();
printf("enter the value of x=");
scanf("%d",&x);
printf("enter the value of y=");
scanf("%d",&y);
t=x;
x=y;
y=t;
printf("\n after swapping the value:x=%d,y=%d",x,y);
getch();
}
Output:
Enter the value of x=4
Enter the value of y=7
After swapping the value : x=7 y=4
Result:
Thus a C program to swap the values of two variables is written and executed successfully.
EX NO: 2B SWAPPING (WITHOUT A THIRD VARIABLE)
DATE:
Aim:
To write a C program to swap values of two variables without using a third variable.
Algorithm:
STEP 1: Start the program
STEP 2: Get the values of a and b.
STEP 3: Print the values of a and b before swapping
STEP 4: Swap the values of a and b using
a=a+b
b=a-b
a=a-b
STEP 5: Print the values of a and b after swapping
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
printf(“enter the value of x=”);
scanf(“%d”,&x);
printf(“/nenter the value of y=”);
scanf (“%d”,&y);
x=x+y;
y=x-y;
x=x-y;
printf(“/n after swapping the value: x=%d, y=%d”,x,y);
getch();
}
Output:
Enter the value of x=4
Enter the value of y=7
After swapping the value:
x=7 y=4.
Result:
Thus a C program for swapping the values of two variables is written and executed successfully.
EX NO:2C CONVERSION OF CELSIUS TO FAHRENHEIT
DATE:
Aim:
To write a C program to convert the temperature in Celsius to Fahrenheit using the c program.
Algorithm:
STEP 1: Start the program
STEP 2: Get temperature in Celsius
STEP 3: Convert Fahrenheit=((Celsius*9)/5)+32
STEP 4: Print temperature in Fahrenheit
STEP 5: stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float Fahrenheit, Celsius;
celsius=24
fahrenheit=((Celsius*9)/5)+32;
printf(“/n/ntemperature in Fahrenheit is:%f”,Fahrenheit);
getch();
}
Output:
Temperature in Fahrenheit : 75.199997
Result:
Thus a C program to convert the temperature in Celsius to Fahrenheit is written and executed
successfully.
EX NO: 3A FINDING LEAP YEAR OR NOT
DATE:
Aim:
To find whether the year is leap year or not using C program
Algorithm:
STEP 1: Start the program
STEP 2: Get the year to find leap year or not
STEP 3: Check for leap year through year%400==0, year%100!=0 and year%4==0
STEP 4: If true print leap year, else print not leap year
STEP 5: stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0&&year%100!=0&&year%4==0)
{
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
getch();
}
Output:
Enter a year : 2012
2012 is a leap year
Enter a year : 2021
2021 is not a leap year
Result:
Thus a C program to find the year is leap year or not is written and executed successfully
EX NO: 3B ELIGIBLE TO VOTE OR NOT
DATE:
Aim:
To write a C program whether a person is eligible to vote or not given his age.
Algorithm:
STEP 1: Start the program
STEP 2: Enter the age of the person as age
STEP 3: Check whether the age is above 18 or not
STEP 4: If age is greater than 18 print “eligible to vote
STEP 5: Else print “not eligible to vote”
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf(“Enter your age:”);
scanf(“%d”,&age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
g:
printf(“Eligible to vote\n”);
getch();
exit();
s:
printf(“Not Eligible to vote”);
getch();
}
Output:
Enter your age: 22
Eligible to vote
Enter your age: 12
Not Eligible to vote
Result:
Thus a C program to check whether a person is eligible to vote or not is written, executed and
output is verified.
EX NO:3C MENU DRIVEN CALCULATOR
DATE:
Aim:
To write a C program to implement a menu driven calculator
Algorithm:
STEP 1: Start the program
STEP 2: Enter the arithmetic operation has to be performed in calculator
STEP 3: Enter two numbers for performing arithmetic operation
STEP 4: If addition is chosen add the two numbers and save the result
STEP 5: If subtraction is chosen subtract the two numbers and save the result
STEP 6: If Multiplication is chosen multiply the two numbers and save the result
STEP 7: If division is chosen divide the two numbers and save the result
STEP 8: Print the result
STEP 9: Stop the program
Flow Chart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
int x,y;
float z;
clrscr();
printf(“1. Addition\n”);
printf(“2.Subraction\n”);
printf(“3.Multiplication\n”);
printf(“4.Division\n”);
printf(“Enter choice:”);
scanf(“%d”,&choice);
printf(“Enter the input values:”);
scanf(“%d”,&x);
scanf(“%d”,&y);
switch(choice){
case 1:
z=x+y;
break;
case 2:
z=x-y;
break;
case 3:
z=x*y;
break;
case 4:
z=(float)x/y;
break;}
Printf(“The result is: %f”,z);
}
getch();
}
Output:
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter choice: 1
Enter input values: 5 4
The result is 9
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter choice: 2
Enter input values: 5 4
The result is 1
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter choice: 3
Enter input values: 5 4
The result is 20
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter choice: 4
Enter input values: 5 4
The result is 1.25
Result:
Thus a C program to implement a Menu driven calculator is written, executed and output is
verified.
EX NO:3D SUM OF EVEN NUMBERS
DATE:
Aim:
To write a C program to find sum of even numbers
Algorithm:
STEP 1: Start the program
STEP 2: Read a positive number upto which finding sum
STEP 3: Check whether the number is even or not
STEP 4: If even add to find sum
STEP 5: Print the sum
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,I,sum=0;
clrscr();
printf(“enter a positive number:\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
if(i%2==1)
{
continue;
}
sum+=i;
}
printf(“sum of even numbers from 1 to %d=%d”,n,sum);
getch();
}
Output:
Enter a positive number : 20
Sum of even numbers from 1 to 20 = 110
Result:
Thus a C program to find the sum of even numbers is written, executed and output is verified.
EX NO:4A FIBONACCI SERIES
DATE:
Aim:
To write a C program to generate Fibonacci series
Algorithm:
STEP 1: Start the program
STEP 2: n=10 is assigned to generate the Fibonacci series
STEP 3: series is printed by adding first two numbers to print as third number and so on
STEP 4: Fibonacci series is printed for n=10
STEP 5: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int I,n=10,f1=0,f2=1,f3;
clrscr();
printf(“%d\t%d”,f1,f2);
for(i=2;i<n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf(“%d\t”,f3);
}
getch();
}
Output:
0 1 1 2 3 5 8 13 21 34
Result:
Thus C program to generate Fibonacci series is written, executed and output is verified
successfully
EX NO:4B FINDING ARMSTRONG NUMBER OR NOT
DATE:
Aim:
To write a C program to find whether a number is an Armstrong number or not.
Algorithm:
STEP 1: Start the program
STEP 2: Read the number to find whether Armstrong number or not
STEP 3: Find the cube of every digit in the number
STEP 4: Find the sum of all cubed digits
STEP 5: Check whether the sum is equal to the number, if true print Armstrong number
STEP 6: Else print not Armstrong number
STEP 7: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d=0,sum=0,temp;
clrscr();
printf(“Enter a number:\n”);
scanf(“%d”,&n);
temp=n;
while( n>0)
{
d=n%10;
sum=sum+d*d*d;
n=n/10;
}
If(sum==temp)
{
printf(“Armstrong number”);
}
else
{
printf(“Not an armstrong number”);
}
getch();
}
Output:
Enter a number : 153
Armstrong number
Result:
Thus a C program to find whether a number is Armstrong or not is written, executed and
output is verified.
EX NO:4C FINDING FACTORIAL OF A NUMBER
DATE:
Aim:
To write a C program to find factorial of a given number
Algorithm:
STEP 1: Start the program
STEP 2: Enter the number to find factorial of given number
STEP 3: Find the factorial using do-while
STEP 4: Print the value
STEP 5: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,f=1;
clrscr();
printf(“\n Enter the number:”);
scanf(“%d”,&n);
do
{
f=f*i;
i++;
}while(i<=n);
printf(“\nThe factorial of %d is %d”,n,f);
getch();
}
Output:
Enter the number : 5
The factorial of 5 is 120
Result:
Thus C program to find factorial of given number is written is written, executed and output is
verified
EX NO: 5A FINDING MEAN FOR ARRAY OF ELEMENTS
DATE:
Aim :
To write a C program to perform mean of ‘n’ numbers using one dimensional array
Algorithm :
STEP 1: Start the program
STEP 2: Enter no. of elements for which mean value has to be found
STEP 3: Enter ‘n’ values in an array to find mean
STEP 4: Find sum by adding the array elements
STEP 5: Find the mean
STEP 6: Print mean
STEP 7: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
int i,a[50],sum=0,n;
float avg;
clrscr();
printf(“Enter no of elements:”);
scanf(“%d”,&n);
printf(“Enter the values:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
for(i=0;i<n;i++)
printf(“\na[%d]=%d”,i,a[i]);
printf(\nSum=%d”,sum);
avg=sum/n;
printf(“\nMean=%d”,avg);
getch();
}
Output:
Result:
Thus a C program to perform the mean of ‘n’ numbers using a one dimensional array is
written, executed and output is verified.
EX NO:5B FINDING MATRIX MULTIPLICATION
DATE:
Aim:
To write a C program to find matrix multiplication using two dimensional arrays
Algorithm:
STEP 1: Start the program
STEP 2: Get values of two 3X3 matrices to be multiplied in two 2 dimensional arrays
STEP 3: Apply necessary for loop to multiply rows with columns
STEP 4: Store the final values in a 3X3 matrix using 2d array
STEP 5: Print the values in matrix form
STEP 6: Stop the program
Flowchart :
Program:
#include<stdio.h>
#include<conio.h>
void main() {
int i, j, k;
int a[3][3], b[3][3], mul[3][3];
printf("Enter elements of the first matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
mul[i][j] = 0;
for (k = 0; k < 3; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", mul[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter the elements of first matrix : 2 5 6 4 8 7 1 6 9
Enter the elements of second matrix : 8 7 5 1 9 4 3 7 1
39 101 36
61 149 59
41 124 38
Result:
Thus C program to find matrix multiplication using two dimensional array is written,
executed and output is verified
EX NO:6A FINDING PALINDROME OR NOT
DATE:
Aim:
To write a C program to find whether the given string is palindrome or not
Algorithm:
STEP 1: Start the program
STEP 2: Read the string to be checked palindrome or not
STEP 3: Compare the string with the reversed string
STEP 4: Print as palindrome if equal.
STEP 5: Print as not palindrome if not equal
STEP 6: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include <string.h>
void main() {
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++) {
if (string1[i] != string1[length - i - 1]) {
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome\n", string1);
} else {
printf("%s is a palindrome\n", string1);
}
getch();
}
Output:
Enter a string : madam
madam is palindrome
Result:
Thus a C program to find whether a string is palindrome or not is written, executed and output
is verified.
EX NO:6B STRING MANIPULATION
DATE:
Aim:
To write a C program to implement string manipulation functions using string library
functions
Algorithm :
STEP 1: Start the program
STEP 2: Initialize the string variables to which string length, string copy, string
concatenate, string reverse and string compare are performed
STEP 3: String length is calculated using strlen() function and print the result
STEP 4: String copy is performed by strcpy() function and print the result
STEP 5: String concatenate is performed by strcat() function and print the result
STEP 6: String reverse is performed by strrev() function and print the result
STEP 7: String compare is performed by strcmp() function and print the result
STEP 8: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s1[] = "computer";
char s2[]=”science”;
char s[20];
int len,cmp;
clrscr();
strcat(s1, s2);
printf("concatenated string is: %s ", s1);
strcpy(s, s1);
printf(“the copied string is: %s”, s);
len = strlen(s);
printf(“ the length of the string is:%d”,len);
strrev(s2);
printf(“reversed string is : %s”,s2);
cmp=strcmp(s,s1);
printf(“compared value is : %d”,cmp);
getch();
}
Output:
Concatenated string is : computerscience
Copied string is : computerscience
The length of the string is : 15
Reversed string is : ecneics
Compared value is : 0
Result:
Thus C program to implement string manipulation functions are written, executed and output
is verified
EX NO: 6C SORTING OF STRINGS IN ALPHABETICAL ORDER
DATE:
Aim:
To write a C program to sort given strings in alphabetical order
Algorithm:
STEP 1: Start the program
STEP 2: Get the names to be sorted in alphabetical order
STEP 3: Compare the strings and arrange in alphabetical order
STEP 4: Print the sorted names
STEP 5: Stop the program
Flow chart:
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(){
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :");
scanf("%d",&n);
printf("Enter names in any order:");
for(i=0;i<n;i++){
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("The sorted order of names are:”);
for(i=0;i<n;i++){
printf("%s",str[i]);
}
}
Output:
Enter number of names:
5
Enter names in any order:
Prabhu Lakshmi Ram Anand Babu
The sorted order of names are:
Anand Babu Lakshmi Prabhu Ram
Result:
Thus a C program to sort given strings in alphabetical order is written, executed and output is
verified.
EX NO: 7A FUNCTIONS FOR ARITHMETIC OPERATIONS
DATE:
Aim:
To write a C program to calculate arithmetic operations using functions
Algorithm:
STEP 1: Start the program
STEP 2: get the input a,b
STEP 3: do the multiplication,addition, subtraction ,division
STEP 4: perform the arithmetic operation
STEP 5: print the arithmetic operation performed STEP 6: stop
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
int addition(int a, int b) {
int sum = a + b;
return sum;
}
int subtract(int a, int b) {
int difference = a - b;
return difference;
}
int multiply(int a, int b) {
int product = a * b;
return product;
}
float division(float a, float b) {
float quotient = a / b;
return quotient;
}
void main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Arithmetic operations on %d and %d:\n", num1, num2);
printf("Addition: %d\n", addition(num1, num2));
printf("Subtraction: %d\n", subtract(num1, num2));
printf("Multiplication: %d\n", multiply(num1, num2));
printf("Division: %.6f\n", division(num1, num2));
getch();
}
Output:
Enter the first number: 8
Enter the second number: 3
Arithmetic operations on 8 and 3:
Addition: 11
Subtraction: 5
Multiplication: 24
Division: 2.666667
Result:
Thus C program to calculate arithmetic operations using functions is written, executed and
output is verified
EX NO: 7B SWAPPING (CALL BY VALUE)
DATE:
Aim:
To write a C program to swap values of variables using call by value
Algorithm:
STEP 1: Start the program
STEP 2: Print the values of a and b before swapping
STEP 3: Pass the values as parameters to swap function
STEP 4: After swapping print the values of a and b
STEP 5: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
printf("After swap: value of a: %d\n", x);
printf("After swap: value of b: %d\n", y);
}
void main() {
int a = 100;
int b = 200;
printf("Before swap: value of a: %d\n", a);
printf("Before swap: value of b: %d\n", b);
swap(a, b);
getch();
}
Output:
Before swap: value of a: 100
Before swap: value of b: 200
After swap: value of a:200
After swap: value of b:100
Result:
Thus C program to swap values using call by value is written, executed and output is verified
EX NO: 7C SWAPPING(CALL BY REFERENCE)
DATE:
Aim:
To write a C program to swap values of variables using call by reference
Algorithm:
STEP 1: Start the program
STEP 2: Print the values of a and b before swapping
STEP 3: Pass the addresses as parameters to swap function
STEP 4: After swapping print the values of a and b
STEP 5: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void main() {
int a = 100;
int b = 200;
printf("Before swap: value of a: %d\n", a);
printf("Before swap: value of b: %d\n", b);
swap(&a, &b);
printf("After swap: value of a: %d\n", a);
printf("After swap: value of b: %d\n", b);
getch();
}
Output:
Before swap: value of a: 100
Before swap: value of b: 200
After swap: value of a:200
After swap: value of b:100
Result:
Thus C program to swap values using call by value is written, executed and output is verified
EX NO: 7D FINDING LARGEST NUMBER IN AN ARRAY
DATE:
Aim:
To write a C program to find the largest number by passing array to function
Algorithm:
STEP 1: Start the program
STEP 2: Read the number of elements
STEP 3: Store the elements in an array
STEP 4: Pass array to a function to find largest number
STEP 5: Compare the values and find the largest value
STEP 6: Print the largest number
STEP 7: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int big(int [],int);
void main()
{
int a[SIZE],n,I,b;
clrscr();
printf(“Enter the size of array:”);
scanf(“%d”,&n);
printf(“\n Enter elements:/n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
b=big(a,n);
printf(“/nLargest number:%d”,b);
getch();
}
int big(int a[],int n)
{
int b,i;
b=a[0];
for(i=0;i<n;i++)
if(a[i]>b)
b=a[i];
return b;
}
Output:
Enter size of array : 5
Enter elements : 4 5 9 12 1
Largest number : 12
Result:
Thus a C program to find the largest number with passing array to function is written,
executed and output is verified.
EX NO: 8 POWER OF A NUMBER USING RECURSION
DATE:
Aim:
To write a C program to find power of a number using recursion
Algorithm:
STEP 1: Start the program
STEP 2: Read the base integer number
STEP 3: Read the power integer number
STEP 4: Power of base number is calculated using recursion
STEP 5: Print the result
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
int power(int base,int p);
void main()
{
int base,p,result;
clrscr();
printf(“Enter the base number:”);
scanf(“%d”,&base);
printf(“Enter power number:”);
scanf(“%d”,&p);
result=power(base,p);
printf(“%d^%d=%d”,base,p,result);
getch();
}
int power(int base,int p)
{
if(p!=0)
return(base*power(base,p-1));
else
return 1;
}
Output:
Enter the base number: 2
Enter power number: 4
2^4 = 16
Result:
Thus C program to find power of a number using recursion is written, executed and output is
verified
EX NO: 9A BIGGEST OF GIVEN THREE NUMBERS
DATE:
Aim:
To write a C program to find biggest of given 3 numbers using pointers to function
Algorithm:
STEP 1: Start the program
STEP 2: Read the three numbers
STEP 3: Pass the addresses to function
STEP 4: Find largest number by comparing the numbers
STEP 5: Print the largest number
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
biggest(int *,int *,int *)
void main()
{
int a,b,c;
clrscr();
printf(“Enter three numbers:\n”);
scanf(“%d%d%d”,&a,&b,&c);
biggest(&a,&b,&c);
}
biggest(int *pa,int *pb,int *pc)
{
if(*pa>*pb&&*pa>*pc)
{
printf(“Biggest = %d”,*pa);
}
else if(*pb>*pc&&*pb>*pc)
{
printf(“Biggest = %d”,*pb);
}
else
{
printf(“Biggest = %d”,*pc);
}
getch();
}
Output:
Enter three numbers: 56 76 98
Biggest: 98
Result:
Thus a C program to find the biggest of three numbers is written, executed and output is
verified.
EX NO: 9B ARRAYS AND POINTERS
DATE:
Aim:
To write a C program to read and write array elements using pointers .
Algorithm:
STEP 1: Start the program
STEP 2: Read the number of elements
STEP 3: Read the elements in an array
STEP 4: Store the array address in a pointer
STEP 5: Print the array elements by incrementing the pointer
STEP 6: Stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int *a,i,n;
int b[5];
clrscr();
printf("Enter the no of elements:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<5;i++)
scanf("%d",&b[i]);
a=b;
for(i=0;i<5;i++){
printf("b[%d]\t\t%d\n",i,*a);
a++;
}
getch();
}
Output:
Enter the no. of elements : 5
Enter the array elements : 2 4 5 7 9
b[0] 2
b[1] 4
b[2] 5
b[3] 7
b[4] 9
Result:
Thus C program to read and write array elements using pointer is written, executed and output
is verified
EX NO: 9C COMPARE STRINGS USING POINTERS
DATE:
Aim:
To write a C program to compare strings using pointers
Algorithm:
STEP 1: Start the program
STEP 2: Initialise two strings to be compared
STEP 3: Pass the strings as pointers
STEP 4: Compare and return 1 if equal
STEP 5: Return 0 if not equal
STEP 6: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
int compare(char* str1, char* str2) {
while (*str1 == *str2) {
if (*str1 == '\0' && *str2 == '\0')
return 1;
str1++;
str2++;
}
return 0;
}
int main() {
char str1[] = "Hello";
char str2[] = "World";
if (compare(str1, str2))
printf("%s and %s are Equal", str1, str2);
else
printf("%s and %s are not Equal", str1, str2);
return 0;
}
Output:
Hello and World are not Equal
Result:
Thus a C program to compare two strings using a pointer is written,executed and verified.
EXNO: 9D POINTER TO POINTER
DATE: 16.5.24
Aim:
To write a C program print values of pointer to pointer
Algorithm:
STEP 1: Start the program
STEP 2: Initialize a value to a variable
STEP 3: Store the variable to pointer variable
STEP 4: Now store the pointer variable to pointer to pointer variable
STEP 5: Print the values
STEP 6: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
void main() {
int val;
int *ptr;
int **ptr_to_ptr;
val = 10;
*ptr = &val;
**ptr_to_ptr = &ptr;
printf("Value of val: %d\n", val);
printf("Value using ptr: %d\n", *ptr);
printf("Value using ptr_to_ptr: %d\n", **ptr_to_ptr);
getch();
}
Output:
Value of val: 10
Value using ptr: 10
Value using ptr_to_ptr : 10
Result:
Thus C program to print values using pointer to pointers is written, executed and output is
verified
EX NO: 10 STUDENT RECORD USING STRUCTURE
DATE: 22.5.24
Aim:
To write a C program to write student record using structure
Algorithm:
STEP 1: Start the program
STEP 2: Define a structure for creating student record
STEP 3: Initialise a structure variable to access structure member variables
STEP 4: Obtain values for member variables
STEP 5: print the student record
STEP 6: stop the program
Flowchart:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};
void main()
{
int i = 0;
struct Student student[5];
student[0].roll_number = 1;
student[0].name = "Anand";
student[0].age = 12;
student[0].total_marks = 78.50;
student[1].roll_number = 2;
student[1].name = "Babu";
student[1].age = 10;
student[1].total_marks = 56.84;
student[2].roll_number = 3;
student[2].name = "Daisy";
student[2].age = 11;
student[2].total_marks = 87.94;
student[3].roll_number = 4;
student[3].name = "Ganga";
student[3].age = 12;
student[3].total_marks = 89.78;
student[4].roll_number = 5;
student[4].name = "Ishwarya";
student[4].age = 13;
student[4].total_marks = 78.55;
printf("Student Records:\n\n");
for (i = 0; i < n; i++)
{
printf("\tName = %s\n", student[i].name);
printf("\tRoll Number = %d\n", student[i].roll_number);
printf("\tAge = %d\n", student[i].age);
printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
}
getch();
}
Output:
Student Records:
Name = Anand
Roll Number = 1
Age = 12
Total Marks = 78.50
Name = Babu
Roll Number = 2
Age = 10
Total Marks = 56.84
Name = Daisy
Roll Number = 3
Age = 11
Total Marks = 87.94
Name = Ganga
Roll Number = 4
Age = 12
Total Marks = 89.78
Name = Ishwarya
Roll Number = 5
Age = 13
Total Marks = 78.55
Result:
Thus C program to get student records and print the record is written, executed and output is
verified.
EX NO : 11 DEFINING A UNION
DATE :
Aim :
To write a C program to declare and initialise a Union
Algorithm:
STEP 1: Start the program
STEP 2: Define a union MyUnion
STEP 3: Define member variables and union variable
STEP 4: Initialize a value
STEP 5: Print the value
STEP 6: Stop the program
Flowchart:
Program:
#include <stdio.h>
#include<conio.h>
void main() {
union MyUnion myVar;
myVar.intValue = 42;
printf("Value stored in intValue: %d\n", myVar.intValue);
getch();
}
Output:
Value stored in intValue : 42
Result :
Thus C program to declare and initialise Union is written, executed and output is verified.
EX NO: 12A EMPLOYEE RECORD USING FILES
DATE:
Aim:
To create a file to write and read employee records using the C program.
Algorithm:
STEP 1: start the program
STEP 2: create a file pointer to open a file
STEP 3: open a file for write access
STEP 4: write the employee data in the file
STEP 5: close the file
STEP 6: open the file for read access
STEP 7: read the contents of the file
STEP 8: print the employee data
STEP 9: close the file
STEP 10: stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
int i, n, empno;
float bpay, allow, ded;
char name[10];
clrscr();
fptr = fopen("EMPLOYEE.DAT", "w");
printf("Enter the number of employees : ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\nEnter the employee number : ");
scanf("%d", &empno);
printf("\nEnter the name : ");
scanf("%s", name);
printf("\nEnter the basic pay, allowances & deductions : ");
scanf("%f %f %f", &bpay, &allow, &ded);
fprintf(fptr, "%d %s %f %f %f \n", empno,name,bpay,allow,ded);
}
fclose(fptr);
fptr = fopen("EMPLOYEE.DAT", "r");
printf("\nEmp. No.Name\t\t Bpay\t\t Allow\t\t Ded\t\t Npay\n\n");
for(i = 0; i < n; i++)
{
fscanf(fptr,"%d%s%f%f%f\n", &empno,name,&bpay,&allow,&ded);
printf("%d \t %s \t %.2f \t %.2f \t %.2f \t %.2f \n", empno, name, bpay, allow, ded, bpay +
allow - ded);
}
fclose(fptr);
getch();
}
Output:
Result:
Thus a C program to create a file for writing and reading employee data is written, executed
and output is verified.
EX NO: 12B READ AND PRINT CONTENTS OF FILES
DATE:
Aim:
To write a C program to read and print contents of a file
Algorithm:
STEP 1: Start the program
STEP 2: create a file pointer to open a file
STEP 3: open a file for read access
STEP 4: read the contents of the file
STEP 5: print the contents of file
STEP 6: close the file
STEP 7: stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fp = fopen("Jtp.txt","r");
if(!fp)
{
printf("Error in opening file\n");
return 0;
}
//The file pointer always starts at the beginning of the file.
printf("Position of the pointer : %ld\n",ftell(fp));
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything we find till we get to the finish.
printf("%c",ch);
}
printf("Position of the pointer : %ld\n",ftell(fp));
//It will return to its previous location below using the rewind() function.
rewind(fp);
printf("Position of the pointer : %ld\n",ftell(fp));
fclose(fp);
getch();
}
Output:
Result:
Thus a C program to read and print the contents of a file is written, executed and output is
verified
EX NO: 12C RANDOM FILE ACCESS
DATE:
Aim:
To write a C program to access file contents randomly.
Algorithm:
STEP 1: start the program
STEP 2: create file pointer to open the file
STEP 3: open the file for read access
STEP 4: using fseek() move the pointer through the file contents
STEP 5: print the contents of file
STEP 6: close the file
STEP 7: stop the program
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fp = fopen("jtp.txt","r");
if(!fp)
{
printf("Error: File cannot be opened\n");
return 0;
}
//Move 3 bytes forward, so if we print all the way through, we won't see the first 6 bytes.
fseek(fp, 8, 0);
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything up until the end.
printf("%c",ch);
}
fclose(fp);
getch();
}
Output:
Result:
Thus a C program to access file contents randomly is written, executed and output is verified.
EX NO: 13 PREPROCESSOR DIRECTIVES
DATE:
Aim:
To write a C program to find the area of a rectangle using preprocessor directives.
Algorithm:
STEP 1: start the program
STEP 2: define preprocessor directive for finding area
STEP 3: read length and breadth
STEP 4: call area function
STEP 5: print area
STEP 6: stop the program
Flowchart:
Program:
// C Program to illustrate function like macros
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
void main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of rectangle is: %d", area);
getch();
}
Output:
Area of rectangle is: 50
Result:
Thus a C program to find the area of a rectangle using preprocessor directives is written,
executed and output is verified.
EX NO: 14 COMMAND LINE ARGUMENTS
DATE:
Aim:
To write a C program for implementing Command line arguments.
Algorithm:
STEP 1: start the program
STEP 2: define main function with arguments argument count and argument list
STEP 3: print the argument count
STEP 4: print the argument list
STEP 5: stop the program
Flowchart:
Program:
// C program named mainreturn.c to demonstrate the working
// of command line argument
#include <stdio.h>
#include<conio.h>
// defining main with arguments
void main(int argc, char* argv[])
{
int i;
clrscr();
printf("You have entered %d arguments:\n", argc);
for (i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
getch();
}
Output:
you have entered 1 arguments:
C:\TURBOC3\SOURCE\MAINRETURN.EXE
Result:
Thus a C program to implement Command line arguments is written, executed and output is
verified.