0% found this document useful (0 votes)
21 views

Lab Reports c programming

The document outlines a series of programming experiments, each detailing an algorithm, flowchart, and C program for various basic tasks such as displaying text, performing arithmetic operations, and using control structures. Each experiment includes a description of the steps involved, the corresponding flowchart, and the complete C code along with expected outputs. The experiments cover fundamental programming concepts suitable for beginners.

Uploaded by

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

Lab Reports c programming

The document outlines a series of programming experiments, each detailing an algorithm, flowchart, and C program for various basic tasks such as displaying text, performing arithmetic operations, and using control structures. Each experiment includes a description of the steps involved, the corresponding flowchart, and the complete C code along with expected outputs. The experiments cover fundamental programming concepts suitable for beginners.

Uploaded by

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

Experiment -01

Write a program and algorithm and draw the follow chart for displaying the statement
"Hello World!".

Algorithm:
 Step 1: Start
 Step 2: Display Hello World!
 Step 3: Stop

Follow Chart:

Start

Print "Hello World!"

End

Program:
#include<stdio.h>
#inclide<conio.h>
void main()
{
printf("Hello World!");
getch();
}

Output:

~1~ Shiv Prasad Sharma


Experiment – 02

Write a program for adding two variables, display the result and draw the follow chart.

Algorithm:
 Step 1: Start
 Step 2: Input the value of a and b
 Step 3: Calculate c=a+b
 Step 4: Display c
 Step 5: Stop

Follow Chart:

Start

Input a and b

c=a+b

Display c

End

Program:
#include <stdio.h>
#include<conio.h>

int main()
{

~2~ Shiv Prasad Sharma


int a, b, c;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
c = a + b;
printf("The sum of %d and %d is %d.\n", a,b,c);
return 0;
}

Output:

~3~ Shiv Prasad Sharma


Experiment – 03

Write an algorithm and draw the follow chart to find the area of circle.

Algorithm:

 Step 1: Start
 Step 2: Input Radius of circle
 Step 3: Calculate Area = pi*r*r
 Step 4: Display Area of circle
 Step 5: Stop

Flow Chart:

Start

Input the radius of circle

Area = pi*r*r

Display Area of circle

End

Program:

#include<stdio.h>
#include<conio.h>
#define PI 3.14159
int main()

~4~ Shiv Prasad Sharma


{
float radius, area;
printf("Enter the radius of circle:");
scanf("%f",&radius);
area=PI*radius*radius;
printf("The area of circle is : %f\n",area);
return 0;
}

Output:

~5~ Shiv Prasad Sharma


Experiment – 04

Write an algorithm and draw the flow chat to find the area of rectangle.

Algorithm:
 Step 1: Start
 Step 2: Input length and width
 Step 3: Calculate Area = length and width
 Step 4: Print Area
 Step 5: Stop

Flow Chart:

Start

Input length and width

Area = length * width

Display Area of rectangle

End

Program:

#include <stdio.h>
int main()
{
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);

~6~ Shiv Prasad Sharma


printf("Enter the width of the rectangle: ");
scanf("%f", &width);

area = length*width;
printf("The area of the rectangle with length is : %f\n", area);

return 0;
}

Output:

~7~ Shiv Prasad Sharma


Experiment – 05

Write an algorithm for division of two variables and display the result also draw the follow
chart.

Algorithm:
 Step 1: Start
 Step 2: Input the value of a and b
 Step 3: Calculate c=a/b
 Step 4: Display c
 Step 5: Stop

Flow Chart:

Start

Input the value of a and b

c = a/b

Display c

End

Program:
#include <stdio.h>
#include<conio.h>

int main()
{
int a, b, c;

~8~ Shiv Prasad Sharma


printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
c = a / b;
printf("The division of %d and %d is %d.\n", a,b,c);
return 0;
}

Output:

~9~ Shiv Prasad Sharma


Experiment – 06

Write an algorithm for multiply of two variables and display result and draw a follow
chart.

Algorithm:
 Step 1: Start
 Step 2: Input the value of x and y
 Step3: Calculate z = x*y
 Step 4: Display z
 Step 5: Stop

Flow Chart:

Start

Input the value of x and y

z = x*y

Display z

End

Program:

#include <stdio.h>
#include<conio.h>

int main()
{

~ 10 ~ Shiv Prasad Sharma


int x, y, z;
printf("Enter the first number: ");
scanf("%d", &x);
printf("Enter the second number: ");
scanf("%d", &y);
z = x*y;
printf("The multiplication of %d and %d is %d.\n", x,y,z);
return 0;
}

Output:

~ 11 ~ Shiv Prasad Sharma


Experiment – 07

Write an algorithm for subtraction of two variables and display the result and draw the
flow chat.

Algorithm:
 Step 1: Start
 Step 2: Input the value of a and b
 Step 3: Calculate c = a-b
 Step 4: Display c
 Step 5: Stop

Flow chart:

Start

Input the value of a and b

c = a-b

Display c

End

Program:

#include <stdio.h>
#include<conio.h>

int main()
{

~ 12 ~ Shiv Prasad Sharma


int a, b, c;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
c = a - b;
printf("The Subtraction of %d and %d is %d.\n", a,b,c);
return 0;
}

Output:

~ 13 ~ Shiv Prasad Sharma


Experiment – 08

Write a program to calculate simple interest and draw the flow chart.

Algorithm:
 Step 1: Start
 Step 2: Input principle, time and rate
 Step 3: Calculate s = (p*t*r)/100
 Step 4: Display s
 Step 5: Stop

Flow Chart:

Start

Input p, t and r

s = (p*t*r)/100

Display s

End

Program:

#include <stdio.h>
#include<conio.h>

int main()
{
int p, t, r, s;

~ 14 ~ Shiv Prasad Sharma


printf("Enter the Princple Amount: ");
scanf("%d", &p);
printf("Enter the Time: ");
scanf("%d", &t);
printf("Enter the Rate: ");
scanf("%d", &r);
s = (p*t*r)/100;
printf("The Simple Interest is: %d.\n", s);
return 0;
}

Output:

~ 15 ~ Shiv Prasad Sharma


Experiment – 09

Write an algorithm and draw the flow chart to declare that given age is adult or child.

Algorithm:
 Step 1: Start
 Step 2: Input age
 Step 3: If age>=18 then display "Adult" else display "Child"
 Step 4: Stop

Flow Chart:

Start

Input age

No Yes

Yes If age>=18
Ye s

Display " You Display " You


are a child " are an adult "

End Ye s
Yes

~ 16 ~ Shiv Prasad Sharma


Program:

#include <stdio.h>

int main() {
int age;

printf("Enter your age: ");


scanf("%d", &age);

if (age >= 18) {


printf("You are an adult.\n");
} else {
printf("You are a child.\n");
}

return 0;
}

Output:

~ 17 ~ Shiv Prasad Sharma


Experiment – 10

Write a program to find the given number in odd number or even number and draw flow
chart.

Algorithm:
 Step 1: Start
 Step 2: Input number
 Step 3: If number/2 == 0 then display number in even number else display number is odd
number.
 Step 4: Stop

Flow Chart:

Start

Input Number

No Yes
If
Yes Number/2
Ye s
==0

Display "Number is Display "Number is


odd number." even number."

End
Yes Ye s

~ 18 ~ Shiv Prasad Sharma


Program:
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (number % 2 == 0)
{
printf("%d is an even number.\n", number);
}
else
{
printf("%d is an odd number.\n", number);
}

return 0;
}

Output:

~ 19 ~ Shiv Prasad Sharma


Experiment – 11

Write a program to find the greater number among two numbers.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int max, a,b;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
max=a>b ? a:b;
printf("The greatest number is %d",max);

getch();

Output:

~ 20 ~ Shiv Prasad Sharma


Experiment – 12

Write a program to find greatest number among two number using term and condition.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int max, a,b;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
printf("%d is greatest number.",a>b?a:b);

getch();

Output:

~ 21 ~ Shiv Prasad Sharma


Experiment – 13

Write a program to find greatest number among 3 numbers.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
printf("Enter Third number:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("%d is a greatest number",a);
}
else
{
printf("%d is greatest number",c);
}
}
else
{
if (b>c)
{
printf("%d is a greatest number",b);
}
else
{
printf("%d is the greatest number",c);
}
}
getch();
}

~ 22 ~ Shiv Prasad Sharma


Output:

~ 23 ~ Shiv Prasad Sharma


Experiment – 14

Write a program to find the given number is positive or negative or zero.

Program:

#include<stdio.h>
#include<conio.h>
void main ()

{
int n;
printf("Enter a number:");
scanf("%d", &n);
if (n>0)
{
printf("%d is a positive number",n);

}
else if (n<0)
{
printf("%d is negative number",n);
}
else
{
printf("%d is a Zero",n);
}
getch();

Output:

~ 24 ~ Shiv Prasad Sharma


Experiment – 15

Write a program to find given numer is zero or not.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
if (a==0)
{
printf("You have enter Zero.");
}
else
{
printf("You number is not equal to zero.");
}
getch();
}

Output:

~ 25 ~ Shiv Prasad Sharma


Experiment - 16

Write a program to print address of variable using pointer.

Program:

#include<stdio.h>
#include<conio.h>
void main ()

{
int n=10;
int *p;
p=&n;
printf("\n%d, %d, %d",n,*p,p);

getch();

Output:

~ 26 ~ Shiv Prasad Sharma


Experiment – 17

Write a program to show the use of pointer to pointer.

Program:

#include<stdio.h>
#include<conio.h>
void main ()

{
int a=3000;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&ptr1;
printf("The value of a is: %d",a);
printf("\nThe value available at ptr1 is: %d",*ptr1);
printf("\nThe value available ate ptr2 is: %d",**ptr2);
getch();

Output:

~ 27 ~ Shiv Prasad Sharma


Experiment – 18

Write a program to print 1 to n numbers using while loop.

Program:

#include<stdio.h>
#include<conio.h>
void main ()

{
int i,n;
printf("Enter the number you want to print the series:");
scanf("%d",&n);
while (i<=n)
{
printf("%d ",i);
i++;
}
getch();

Output:

~ 28 ~ Shiv Prasad Sharma


Experiment – 19

Write a program to create 2X2 array and insert the element in it and display them.

Program:
#include <stdio.h>
int main()
{
int arr[2][2];
int i, j;
printf("Enter elements for the 2x2 array:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("Enter element at position [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("\nThe 2x2 array is:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Output:

~ 29 ~ Shiv Prasad Sharma


Experiment – 20
Write a program to print order number fill n.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1, n;
printf("Enter the number to fill you want to print the odd numbers: ");
scanf("%d", &n);
while (i<=n)
{
if (i%2!=0)
{
printf("\n%d",i);
i++;
}
getch();
}
}

Output:

~ 30 ~ Shiv Prasad Sharma


Experiment – 21

Write a program to concatenate two string using library function.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
char l_name[10];
printf("Enter your name: ");
scanf("%s",&name);
printf("\nEnter your last name: ");
scanf("%s",&l_name);
strcat(name, " ");
strcat(name,l_name);
printf("\nYour complete name is: %s",name);
return 0;
}

Output:

~ 31 ~ Shiv Prasad Sharma


Experiment – 22

Write a program to print the string in uppercase.


Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10];
printf("Enter any text: ");
scanf("%s",&a);
strupr(a);
printf("\nYou enter the text as %s",a);
getch();

Output:

~ 32 ~ Shiv Prasad Sharma


Experiment – 23

Write a program to input two number and defining them are positive or negative using go
to.

Program:

#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;

printf("Enter the first number: ");


scanf("%d", &num1);
if (num1 < 0)
goto negative;

printf("Enter the second number: ");


scanf("%d", &num2);

if (num2 < 0)
goto negative;

printf("\nBoth numbers are positive");


getch();
return 0;

negative:
printf("\nBoth numbers are negative");
return 0;
}

Output:

~ 33 ~ Shiv Prasad Sharma


Experiment – 24

Write a program to create the file "Shiv.txt" and write text "Welcome to my file".

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *ptr;
ptr=fopen("D:\\Shiv.txt","w");
if (ptr==NULL)
{
printf("\nFile is not found");
exit(1);
}
else
{
printf("\nFile has been successfully created");
}
fputs("\nWel-come to my file",ptr);
fclose(ptr);
getch();
}

Output:

~ 34 ~ Shiv Prasad Sharma


Experiment – 25

Write a program to read the content of a Shiv.txt file.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
char s[100];
FILE *ptr;
ptr=fopen("D:\\test.txt","r");
if (ptr==NULL)
{
printf("\nFile cannot be opened");
exit(1);
}
fgets(s,100,ptr);
printf("\nThe content of the file is:\t%s",s);
fclose(ptr);
getch();
}

Output:

~ 35 ~ Shiv Prasad Sharma


Experiment – 26

Write a program to print even number from 2 to n using continue statement.

Program:

#include <stdio.h>

int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 2)
{
printf("There are no even numbers less than 2.\n");
return 0;
}
printf("Even numbers from 2 to %d:\n", n);
for (int i = 2; i <= n; i++)
{

if (i % 2 != 0)
{

continue;
}
printf("%d ", i);
}

return 0;
}

utput:O

~ 36 ~ Shiv Prasad Sharma


Experiment – 27

Write a program to print even number fill 10 using for loop.

Program:

#include <stdio.h>

int main()
{
printf("First 10 even numbers:\n");
int i;
for ( i = 2; i <= 20; i += 2)
{
if (i%2==0)
{
printf("%d ", i);
}
}
return 0;
}

Output:

~ 37 ~ Shiv Prasad Sharma


Experiment – 28

Write a program to generate the Fibonacci series using recursive function.

Program:

#include<stdio.h>
#include<conio.h>
long fib(int);
void main()
{
int terms,i;
printf("\nEnter the terms you want to print the fabonacci series:\t");
scanf("%d",&terms);
for(i=1;i<=terms;i++)
printf("\t%d",fib(i));
getch();
}
long int fib(int x)
{
if (x==1)
{
return 0;
}
else if (x==2)
{
return 1;
}
else
{
return (fib(x-1)+fib(x-2));
}
}

Output:

~ 38 ~ Shiv Prasad Sharma


Experiment – 29

Write a program to print the series 1, 2, 3, ………… n.

Program:

#include <stdio.h>

int main()
{
int n;
printf("Enter the last number for series: ");
scanf("%d", &n);
printf("\nSeries from 1 to %d:\n", n);
for (int i = 1; i <= n; i++)
{
printf("%d ", i);
}
printf("\n");

return 0;
}

Output:

~ 39 ~ Shiv Prasad Sharma


Experiment – 30

Write a program to read integer type of element from keyboard and store them in array
and also display them.

Program:

#include <stdio.h>
#include <conio.h>

int main()
{
int arr[5];
int i;

printf("Enter the 5 elements of the array: ");


for (i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}

printf("\nThe elements of the array you entered are: ");


for (i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}

getch();
return 0;
}

Output:

~ 40 ~ Shiv Prasad Sharma


Experiment – 31

Write a program to print your name for five times using for loop.

Program:

#include <stdio.h>

int main()
{
for (int i = 0; i < 5; i++)
{
printf("Shiv Prasad Sharma\n");
}

return 0;
}

Output:

~ 41 ~ Shiv Prasad Sharma


Experiment – 32

Write a program to insert n elements in an array and display in reverse order.

Program:

#include <stdio.h>

int main()
{
int arr[100];
int i, n;
printf("Enter the number of elements you want to insert: ");
scanf("%d", &n);

for (i = 0; i < n; i++)


{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("\nElements in reverse order:\n");
for (i = n - 1; i >= 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}

Output:

~ 42 ~ Shiv Prasad Sharma


Experiment – 33

Write a program to print the multiplication table of n.

Program:

#include <stdio.h>

int main() {
int n, i;
printf("Enter the number: ");
scanf("%d", &n);
i = 1;
printf("Multiplication table of %d:\n", n);
while (i <= 10) {
printf("%d * %d = %d\n", n, i, n * i);
i++;
}

return 0;
}

Output:

~ 43 ~ Shiv Prasad Sharma


Experiment – 34

Write a program to find the factorial of any number using recursive function.

Program:

#include<stdio.h>
#include<conio.h>
long int fact(int);
void main()
{
int num;
long int factorial;
printf("\nEnter the number you want to find the factorial of:");
scanf("%d",&num);
factorial=fact(num);
printf("\nThe factorial of %d is: %d",num,factorial);
getch();
}
long int fact(int x)
{
if (x==1)
{
return 1;
}
else
{
return (x*fact(x-1));
}
}

Output:

~ 44 ~ Shiv Prasad Sharma


Experiment – 35

Write a program to find the name of day by number using switch case statement.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter the number you want to display the day: ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nIt's Sunday");
break;
case 2:
printf("\nIt's Monday");
break;
case 3:
printf("\nIt's Tuesday");
break;
case 4:
printf("\nIt's Wednesday");
break;
case 5:
printf("\nIt's Thursday");
break;
case 6:
printf("\nIt's Friday");
break;
case 7:
printf("\nIt's Saturday");
default:
printf("\nYou enter wrong choice");
}
getch();
return (0);
}

~ 45 ~ Shiv Prasad Sharma


Output:

~ 46 ~ Shiv Prasad Sharma


Experiment – 36

Write a program to result sheet with total mark, percentage and division.

Program:

#include<stdio.h>
#include<conio.h>
void main()

{
float nep, eng, math, phy, chem, bio, comp;
printf("Enter the mark in Nepali:");
scanf("%f",&nep);
printf("Enter the mark in English:");
scanf("%f",&eng);
printf("Enter the mark in Math:");
scanf("%f",&math);
printf("Enter the mark in Physical:");
scanf("%f",&phy);
printf("Enter the mark in Chemestry:");
scanf("%f",&chem);
printf("Enter the mark in Biology:");
scanf("%f",&bio);
printf("Enter the mark in Computer:");
scanf("%f",&comp);
int total;
total = nep+eng+math+phy+chem+bio+comp;
printf("\nYour total mark is:%d",total);
int percentage;
percentage = (nep+eng+math+phy+chem+bio+comp)/7;
printf("\nYour percentage is:%d",percentage);
if(percentage>=80)
{
printf("\nYou Score Distinction.");
}
else if(percentage>=60)
{
printf("\nYou Score First Division.");
}
else if(percentage>=40)
{
printf("\nYou Score Second Division.");

~ 47 ~ Shiv Prasad Sharma


}
else
{
printf("\nYou are Fail.");
}
return 0;
}

Output:

~ 48 ~ Shiv Prasad Sharma


Experiment – 37

Write a program to show the use of function call by reference.

Program:

#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int x,y;
x=50; y=70;
printf("\nBefore function calling, a and b are: %d and %d",x,y);
swap(&x,&y);
printf("\nAfter function calling, a and b are: %d and %d",x,y);
getch();
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return 0;
}

Output:

~ 49 ~ Shiv Prasad Sharma


Experiment – 38

Write a program to show the use of function call by value.

Program:

#include <stdio.h>
#include <conio.h>

void swap(int *, int *);

int main() {
int x = 50, y = 70;

printf("Before function calling, X and Y are: %d and %d\n", x, y);


swap(&x, &y);
printf("After function calling, X and Y are: %d and %d\n", x, y);

getch();
return 0;
}

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

Output:

~ 50 ~ Shiv Prasad Sharma


Experiment – 39
Write a program to insert the details of students like name and roll number and display
them.
Program:
#include <stdio.h>
typedef struct
{
char name[50];
int roll_number;
} Student;
int main()
{
Student students[100];
int num_students;
printf("Enter the number of students: ");
scanf("%d", &num_students);
for (int i = 0; i < num_students; i++)
{
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].roll_number);
}
printf("\nDetails of all students:\n");
for (int i = 0; i < num_students; i++)
{
printf("Student %d\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].roll_number);
}
return 0;
}
Output:

~ 51 ~ Shiv Prasad Sharma


Experiment – 40

Write a program to illustrate the array of structure.

Program:
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
struct student s[3];
int i;
for (i=0;i<3;i++)
{
printf("Enter the name, roll number, marks of student: ");
scanf("%s %d %f",s[i].name,&s[i].roll,&s[i].marks);
}
printf("\nInputed details are: ");
for(i=0;i<3;i++)
{
printf("\nName: %s",s[i].name);
printf("\nRoll: %d",s[i].roll);
printf("\nMarks: %0.2f",s[i].marks);
}
getch();
}

Output:

~ 52 ~ Shiv Prasad Sharma


Experiment – 41

Write a program to write string in the file.

Program:
#include <stdio.h>
int main()
{
FILE *file;
char str[100];
file = fopen("output.txt", "w");
if (file == NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Enter a string to write to the file: ");
fgets(str, sizeof(str), stdin);
fprintf(file, "%s", str);
fclose(file);
printf("String successfully written to file.\n");
return 0;
}

Output:

~ 53 ~ Shiv Prasad Sharma


Experiment – 42

Write a program to read the string in reverse order.

Program:

#include <stdio.h>
#include <string.h>

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int length = strlen(str);
printf("String in reverse order: ");
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");

return 0;
}

Output:

~ 54 ~ Shiv Prasad Sharma


Experiment – 43

Write a program to read the name and mark of n numbers of student and store them in a
file.

Program:

#include <stdio.h>

int main() {
FILE *file;
char filename[100];
int n,i;
printf("Enter the filename to store student details: ");
scanf("%s", filename);
file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
char name[100];
int marks;

printf("Enter name of student %d: ", i + 1);


scanf("%s", name);
printf("Enter marks of student %d: ", i + 1);
scanf("%d", &marks);
fprintf(file, "%s %d\n", name, marks);
}
fclose(file);

printf("Student details successfully stored in file.\n");

return 0;
}

~ 55 ~ Shiv Prasad Sharma


Output:

~ 56 ~ Shiv Prasad Sharma

You might also like