c-programming-lab-manual
c-programming-lab-manual
Aim:
To write a c program to find the average of a student's marks.
Algorithm:
STEP 1: Start the program
STEP 2: Get student marks to find average
STEP 3: Find total = m1+m2+m3
STEP 4: Calculate average = total/3
STEP 5: Print the average
STEP 6: stop the program
Flowchart:
Star
t
Read
m1,m
2,m3
Tot =
m1+m2+m
3 avg =
tot/3
Print
avg
Stop
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.
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 :
Sta
rt
read
a,b
Print a,b
before
swap
t=a
a=b
b=t
Print a,b
after
swap
Stop
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.
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:
Sta
rt
read
a,b
Print a,b
before
swap
a=a+b
b=a-b
a=a-b
Print a,b
after
swap
Stop
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.
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:
Star
t
Read
Celcius
Fahrenheit =
(celsius*9)/5)+32
Print
farenheit
Stop
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.
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:
Sta
rt
Read
Year
If
year
%400==0,
year%100!
=0 and
year
%4==0 No
yes
Print Leap Print not
Year leap
year
Stop
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
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:
Sta
rt
Read
age
If
age>18
No
yes Print not
Print Eligible Eligible
to vote to vote
Stop
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.
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:
Start
Read a , b
Read
choice
If yes
Choice c=a+b
is add
No
If yes
Choice c=a+b
is Sub
No
If
Choice is yes
c=a+b
Multiply
No
If yes
Choice c=a+b Print c
is Divide
No
Stop
Print Enter correct
choice
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
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.
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:
Start
Read num
If i
is
eve no
n
yes
yes
no i=i+1
Print sum
stop
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.
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:
Start
n=10 f1=0 f2
=1
Print f1 f2
If
i<n No
yes
f3=f2+f1
f1=f2
f2=f3
Print f2
i=i+1
Stop
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
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:
start
Assign sum=0
Read
number ,
Assign temp=
num
Whether
temp=0
Compute
digit=temp%10
Temp=temp/10
If Print num is
num==su not an
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.
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:
star
t
Read
n
i=n
fact=1
If
i<=
n
Print
Fact = fact*i fact
i=i+1 stop
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
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:
St
art
Read No.
of values
‘n’
Enter ‘n’
values in
array
If
mean=su
i< No
m/n
n
yes
Sum = sum + a[i] Print mean
i=i+1 Stop
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.
Aim:
Flowchart :
start
Read
3X3 A
matrix
Read
3X3 B
matrix
Mul[ ]
[ ]=Multiply
rows of A
matrix with
column of B
matrix
Print mul
matrix
stop
Program:
#include<stdio.h>
#include<conio.h>
void main() {
int i, j, k;
int a[3][3], b[3][3], mul[3][3];
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
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:
star
t
Read
string
If
i<len(s
tring)
If
yes
string[i
Flag = no ]! yesFlag = 1
0 =string
[len-1) Print
Print
not
palind
palindr
rome
ome
stop
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.
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:
sta
rt
Declar
es
Read
s1,s2
strcat(s1,s2),strc
py(s,s1),strlen(s)
,strrev(s2),strcm
p(s,s1)
Display
the result
stop
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
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
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:
sta
rt
Declare
variables
i,j,n,str,s
Read (“enter
number of
names:”)
Rea
dn
Read (“enter
names”)
If
strc
mp(s
tr[i],s Yes swap(str[i],str[
tr[j]) j])
>0
N
o
printf(result
s)
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.
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>
Output:
Enter the first number: 8
Enter the second number: 3
Result:
Thus C program to calculate arithmetic operations using functions is written, executed and
output is verified
star
t
Read
a,b
Display a,b
before
swapping
swap(a,b)
Display
a,b after
swapping
stop
Program:
#include <stdio.h>
#include<conio.h>
}
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
Flowchart:
sta
rt
Read
a,b
Display
a,b
before
swappin
g
swap(&a,&
b)
Display
a,b after
swappin
g
sto
p
Program:
#include <stdio.h>
#include<conio.h>
Result:
Thus C program to swap values using call by value is written, executed and output is verified
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:
star
t
Read
n
Read array a
elements
big(a[ ],n)
Find largest in
array
Print largest
stop
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 50
Output:
Enter size of array : 5
Enter elements : 4 5 9 12 1
Largest number : 12
Result:
Thus a C program to find largest number with passing array to function is written, executed
and output is verified.
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:
start
Read base
number
Read power
number
If
power
!=0
Result =
Print result
power*base
power=power-1 stop
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
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:
start
Read a,b,c
biggest(&a,&b,
&c)
Print a
If a
bigger is
yes bigger
no Print b
If b is
bigger yes bigger
Print c
no
is sop
bigger
Program:
#include<stdio.h>
#include<conio.h>
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.
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:
sta
rt
Read n
Read n array
elements
Ptr = a[]
If
i<
n
Print *ptr
ptr=ptr+1 sto
i=i+1 p
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
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:
start
Read
str1,str2
Return =
compare(*str1,*str
2)
Return = no
1
stop
Program:
#include <stdio.h>
#include<conio.h>
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.
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:
start
Read val
ptr=&val
ptr_to_ptr=&pt
r
Print *ptr
Print
*ptr_to_ptr
stop
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
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:
start
struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};
i<n
Print
s[i].name,s[i].rollnumber,
sto
s[i].age,s[i].total_marks p
i=i+1
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 : 10 DEFINING AN 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:
start
union MyUnion {
int intValue;
char charValue;
float floatValue;
};
union MyUnion
myVar;
myVar.intValue =
42;
Print
myVar.intValue
stop
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.
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:
Sta
rt
Create file
pointer
Open ‘employee.dat’
for ‘write’
Write employee
details in file
stop
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.
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:
Star
t
Create file
pointer
stop
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: 11C 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:
Star
t
Create file
pointer
stop
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: 12 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:
start
#define AREA(l, b) (l * b)
Read l ,b
area = AREA(l, b)
Print area
stop
Program:
// C Program to illustrate function like macros
#include <stdio.h>
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.
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:
start
main(argc,argv
[ ])
Print argc
i<arg
c
Print
argv[ ]
i=i+1 stop
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.