Beruflich Dokumente
Kultur Dokumente
Class : I Semester
(AUTONOMOUS)
BASIC C PROGRAMS:
a) Practice DOS Commands
b) Write C program to
1 I. Find the Sum of three numbers. 6-11
II. Exchange (swap) of two numbers by using third variable.
III. Exchange (swap) of two numbers without using third variable.
IV. Print the size of all data types.
BASIC C PROGRAMS:
CONTROL STATEMENTS:
g) Write a program which takes two integers and one arithmetic operator from
the user, and performs the operation and then prints the result by using
switch-case.(Operators : +, -, *,/, %)
ITERATION STATEMENTS:
4 19-23
a) Write a C program to generate the required multiplication table.
ITERATION STATEMENTS:
NESTED LOOPS:
6 22 27-30
333
NESTED LOOPS:
ARRAYS:
i) Sum. ii) Product and display the result in the matrix form.
ARRAYS:
a) Write a C program to read two strings and perform the following operations
without using built-in string Library functions.
POINTERS:
FUNCTIONS:
FUNCTIONS:
STRUCTURES:
14 52-54
a) Declare a structure time that has three fields hr, min, secs. Create two
structure variables start_time and end_time. Input there values from the user.
b) Write a C program to read student roll no, name and marks in six subjects
for n number of students and give class of each student by following the
required conditions.
FILES:
a) Write a program to create a file and write some text data on the file. Then
15 display the contents of the file and also print number of characters, number of 55-57
words, number of lines in the file.
FILES:
a) Write a C program to create a text file and write data on it, then display
every 5th character in that file.
16 b) Write a program to read student records into a file. Record consists of 58-62
rollno, name and marks of a student in six subjects and class. Class field is
empty initially. Compute the class of a student. The calculation of the class is
as per CREC rules. Write the first class, second class, third class and failed
students lists separately to another file.
CD. . :
This command is used to come out from current directory.
Syntax: C: / >CD. .
CD\:
This command is used to come out from all sub directories at once.
Syntax: C : \ > CD\
Dir:
This command is used to display all files and sub – directories under the given directory.
It displays one file on a line with details like file name, extension, size in bytes, data and time
of file creator.
Syntax: C :/> DIR
DIR/P :
This command is used to display the list of files and sub directories page by page.
Syntax: C :/> Dir/P Enter.
DIR/W :
This command is used to display the list of files and sub – directories in a wide format
containing only file names and extensions placing file name side by side.
Syntax: C :/> Dir/w Enter.
Alorithm:
1. Start
2. Read a,b,c
3. Sum=a+b+c
4. Print ‘sum’
5. Stop
Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
clrscr();
printf("\nEnter three numbers :");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
printf("\n Sum of %3d,%3d and %3d is=%4d",a,b,c,sum);
getch();
}
Input:-
Enter three numbers :24 32 65
OUTPUT:-
Sum of 24, 32 and 65 is= 121
Input:-
Enter three numbers :3 5 6
OUTPUT:-
Sum of 3, 5 and 6 is= 14
Input:-
Enter three numbers :1 2 3
OUTPUT:-
Sum of 1 , 2 and 3 is= 6
Input:-
Enter three numbers :4 6 2
OUTPUT:-
Sum of 4, 6 and 2 is= 12
ALORITHM
1. START
2. read x,y
3. temp=x
4. x=y
5. y=temp
6. print x,y
7. STOP
PROGRAM
#include <stdio.h>
int main()
{
int x, y, temp;
temp = x;
x = y;
y = temp;
return 0;
}
O/P
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter two numbers :");
scanf("%d%d",&a,&b);
printf("\n Before swapping, values are a=%3d and b=%3d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping, values are a=%3d and b=%3d",a,b);
getch();
}
Input:-
Enter two numbers :7 9
OUTPUT:-
Before swapping, values are a=7 and b=9
After swapping, values are a=9 and b=7
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
long int l;
unsigned int ul;
Input:-
Enter a integer value :6325
Enter an unsigned value :-958
Enter a long integer value :263547
Enter a float value :36.2541
Enter a double value :23.65874
Enter a character :k
OUTPUT:-
Given values are:
Integer =6325 and its size= 2
Unsigned Integer =64578 and its size=2
Long Integer =263547 and its size=4
Float = 36.25 and its size= 4
Double =23.658740 and its size=8
Character =k and its size=1
PROGRAM
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
OUTPUT:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
EXPERIMENT-3
CONTROL STATEMENTS
A) Write a C program to find whether the given number is odd or even.
ALGORITHM
1. START
2. Read n
3. if(n%2==0) then step4 else step5
4. print “Given number is even number”
PROGRAM
#include<stdio.h>
main()
{
int n;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“\nGiven number is even number”);
else
printf(“\nGiven number is odd number:”);
getch();
}
OUTPUT
Enter the number:5
Given number is odd number
PROGRAM
#include<stdio.h>
main()
{
int a,b;
clrscr();
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“\n First number is greater number”);
else
if((b>a)&&(b>c))
printf(“\n Second number is greater number”);
else
void main()
{
int num;
if(num & 1)
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}
OUTPUT
F) Write a C program to find the maximum of two numbers using Conditional operator.
ALGORITHM
1. START
2. READ a,b
3. C=( a>b)?a:b
4. Print c
5. STOP
PROGRAM
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(“Enter the two numbers:”)
scanf(“%d%d”,&a,&b);
c=( a>b)?a:b;
printf(“\n%d is the maximum number “,c);
getch();
G) Write a program which takes two integers and one arithmetic operator from the user, and
performs the operation and then prints the result by using switch-case. (Operators : +, -, *, /, %)
ALGORITHM
1. START
2. READ a,b,op
3. Switch(op)
a)case op=='+':
i)print ‘a+b’;
ii)break;
b) case op=='-':
i)print ‘a-b’;
ii)break;
c) case op=='*':
i)print ‘a*b’;
ii)break;
d) case op=='/':
i)print ‘a/b’;
ii)break;
e) case op=='%':
i)print ‘a%b’;
ii)break;
4. STOP
PROGRAM
#include<stdio.h>
main()
{
int a,b;
char op;
clrscr();
printf("Enter the two numbers:");
EXPERIMENT-4
ITERATION STATEMENTS
A) Write a C program to generate the required multiplication table.
ALGORITHM
1. START
2. READ n
3. i=1
4. repeat until i<=10 step 5 otherwise step6
5. print n*i
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("enter n values");
scanf("%d",&n);
printf("the multiplication table of %dis:\n",n);
for(i=1;i<=10;i++)
{
printf("%d*%d=%d\n",n,i,n*i);
}
getch();
}
/**********************OUTPUT***********
enter n values 5
the multiplication table of 5is:5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
/**********************OUTPUT***********
enter n values 4
the multiplication table of 4is:
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
ALGORITHM
1. START
2. READ n
3. f=1,i=1
4. Repeat until i<=n step5 otherwise step6
5. a)f=f*I
b)i=i+1
6. print ‘f’
7. STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i=1;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}
Input
Enter a number:5
OUTPUT
Factorial of 5 is = 120
Input
Enter a number:7
OUTPUT
Factorial of 7 is = 5040
Input
Enter a number:8
OUTPUT
Factorial of 8 is = 40320
PROGRAM
#include<stdio.h>
OUTPUT:
Given number is not a prime number
Input:
Enter a number :11
OUTPUT:
Given number is a prime number
Input:
Enter a number : 9
OUTPUT:
Given number is not a prime number
Input:
Enter a number :13
OUTPUT:
Given number is a prime number
ALGORITHM
1. START
2. READ n
3. Repeat until n>0 step4 otherwise step5
4.
a) r=n%10;
b) s=s+r;
c)n=n/10;
5. Print ‘s’
6. STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n ,r ,s=0;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\n Sum of the digits is = %4d",s);
getch();
}
Input:
Enter the number : 256
OUTPUT:
Sum of the digits is = 13
Input:
Enter the number : 113
OUTPUT:
Sum of the digits is = 5
Input:
Enter the number : 222
OUTPUT:
Sum of the digits is = 6
ALGORITHM
1. START
2. READ n
3. k=n
4. Repeat until n>0 step5 otherwise step6
5. a)r=n%10;
b)s=(s*10)+r;
c)n=n/10;
6. if(k==s)then step7 else step8
7. print ‘Given number is Palindrome’
8. print ‘Given number is not Palindrome’
9. STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,k;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
k=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(k==s)
printf("\n Given number is Palindrome");
else
printf("\n Given number is not a palindrome");
}
Input:
Enter a number:125
OUTPUT:
Given number is not a palindrome
Input:
Enter a number:121
Input:
Enter a number:454
OUTPUT:
Given number is a palindrome
Input:
Enter a number:456
OUTPUT:
Given number is not a palindrome
ALGORITHM
1. START
2. READ n
3. t1=0,t2=1,nextterm=0
4. print t1,t2
5. nextterm=t1+t2
6. Repeat until nextterm<=n step7 otherwise step8
7. a)print nextterm
b)t1 = t2;
c)t2 = nextTerm;
d)nextTerm = t1 + t2;
8. STOP
PROGRAM
#include <stdio.h>
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
nextTerm = t1 + t2;
while(nextTerm <= n)
{
printf("%d, ",nextTerm);
return 0;
}
OUTPUT
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Enter a positive integer: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5,
Enter a positive integer: 9
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,
Enter a positive integer: 13
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
EXPERIMENT-6
NESTED LOOPS
ALGORITHM
1. START
2. READ rows
3. i=1
4. Repeat until i<=rows step5 otherwise step6
5. a)J=1
b)Repeat until j<=i step5c otherwise step6d
c) i)Print i
ii) j=j+1
d)i=i+1
6. STOP
PROGRAM
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j)
printf("\n");
}
return 0;
}
OUTPUT
Enter number of rows:3
1
22
3 3 3
ALGORITHM
1. START
2. READ N
3. i=1
4. Repeat until i<=n step5 otherwise step6
5. a)J=1
b)Repeat until j<=10 step5c otherwise step5d
c) i)print i, j, i * j
ii)j=j+1
PROGRAM
#include<stdio.h>
int main () {
int i, j, n;
printf("Enter the value for n:");
scanf("%d", &n);
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=0;
clrscr();
printf("Enter n value upto where prime numbers to be found: ");
scanf("%d",&n);
i=2;
while(i<=n)
{
k=0;
j=2;
while(j<n)
{
if(i%j==0)
k=1;
j++;
}
if(k==1)
printf(“%4d” , i);
i++;
}
}
Input :-
Enter n value upto where prime numbers to be found: 10
OUTPUT:
Required prime numbers are :
2 3 5 7
A number is called strong number if sum of the factorial of its digit is equal to number itself.
For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145
ALGORITHM
1. START
2. READ num
3. sum=0,temp=num
4. Repeat until num!=0 step5 otherwise step6
5. A. i=1,f=1
B. r=num%10
C. Repeat until i<=r stepD otherwise stepE
D. a) f=f*i
b) i=i+1
E. sum=sum+f;
F. num=num/10;
6. if(sum==temp) then step7 else step8
7. print ‘temp’ is a strong number
8. print ‘temp’ is a strong number
9. STOP
PROGRAM
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
return 0;
}
OUTPUT:
Enter a number: 145
145 is a strong number
Enter a number: 40585
40585 is a strong number
B) Write a C program to evaluate the sum of the following series up to ‘n’ terms
ex = 1 + x + x2/2! + x3 /3! + x4 /4! + --------
ALGORITHM
1. START
2. READ x,n
3. sum=1.0, m=1, p=x, i=2
4. Repeat until i<=n step5 otherwise step6
5. A) s=(float)p/m;
B) sum=sum+s;
C) m=m*i;
D) p=x*p;
6. print “sum”
7. STOP
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float sum=1.0,s;
int n,i,x,p,m=1;
clrscr();
printf("\n Enter x value :");
scanf("%d",&x);
printf("\n Enter n value :");
scanf("%d",&n);
p=x;
Input:-
Enter x value :2
Enter n value :3
OUTPUT:-
Sum of the given series is :5.0000
EXPERIMENT-8
ARRAYS
A) Write a C program to find the sum of positive and negative numbers in a given set(Array) of
numbers.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,sp=0,sn=0,i;
clrscr();
printf("\n Enter size of the array :");
scanf("%d",&n);
printf("\n Enter the values of array :");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
if(a[i]<0) // negative
sn=sn+a[i];
else // positive
sp=sp+a[i];
}
OUTPUT:-
In given numbers, sum of positive values : 103
In given numbers, sum of negative values :-67
#include<stdio.h>
#include<conio.h>
void input(int a[8][8],int r,int c);
void OUTPUT(int a[8][8],int r,int c);
void add(int a[8][8],int b[8][8],int s[8][8],int r,int c);
void multiply(int a[8][8],int b[8][8],int s[8][8],int r,int c,int p);
void main()
{
int a[8][8],b[8][8],s[8][8],r1,c1,r2,c2,ch;
clrscr();
printf("\n Enter rows and columns of first matrix:");
scanf("%d%d",&r1,&c1);
printf("\n Enter rows and columns of second matrix:");
scanf("%d%d",&r2,&c2);
printf("\n Enter the elements of first matrix:");
input(a,r1,c1);
printf("\n Enter the elements of second matrix:");
input(b,r2,c2);
printf("\n Elements of first matrix are:");
OUTPUT(a,r1,c1);
printf("\n Elements of second matrix are:");
OUTPUT(b,r2,c2);
if((r1==r2)&&(c1==c2))
{
add(a,b,s,r1,c1);
getch();
}
void input(int a[8][8],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void OUTPUT(int a[8][8],int r,int c)
{
int i,j;
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d",a[i][j]);
}
printf("\n ");
}
}
void add(int a[8][8],int b[8][8],int s[8][8],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
Input
Enter rows and columns of first matrix:3 3
Enter rows and columns of second matrix:3 3
Enter the elements of first matrix: 5 2 6 9 8 7 1 2 1
Enter the elements of second matrix:6 4 7 8 4 1 3 2 5
OUTPUT
Elements of first matrix are:
5 2 6
9 8 7
1 2 1
Elements of second matrix are:
6 4 7
8 4 1
3 2 5
Sum of the two matrices is:
11 6 13
17 12 8
4 4 6
Product of the two matrices is:
64 40 67
139 82 106
25 14 14
EXPERIMENT-9
ARRAYS
A) Write a C program to read matrix and perform the following operations
i) Find the sum of Diagonal Elements of a matrix.
ii) Print Transpose of a matrix.
iii) Print sum of even and odd numbers in a given matrix.
Input:-
Enter the order of the matrix :3
Enter the elemens of the matrix :
5 2 6
8 1 9
4 3 2
EXPERIMENT-10
STRINGS
A) Write a C program to read two strings and perform the following operations without
using built-in string Library functions.
i) String length determination.
ii) Compare Two Strings.
iii) Concatenate Two Strings.
iv) String reversing
PROGRAM
#include<stdio.h>
#include<conio.h>
int stringlength(char *s);
int stringcompare(char *s1,char *s2,int ,int)
void stringconcatenate(char *s1,char *s2,int,int);
void stringreverse(char *s,int);
void main()
{
char str1[20],str2[30];
int n,i,k,l1,l2;
clrscr();
printf("\n Enter the first string:");
scanf("%s",str1);
printf("\n Enter the second string: ");
scanf("%s",str2);
l1=stringlength(str1);
printf("\n Length of first string is=%d",l1);
l2=stringlength(str2);
printf("\n Length of Second string is=%d",l2);
k=stringcompare(str1,str2,l1,l2);
if(k==1)
printf("\n Two strings are equal\n");
OUTPUT:-
Input:-
Enter the first string:balu
Enter the second string: balu
OUTPUT:-
Length of first string is=4
Length of Second string is=4
Reverse of first string is=ulab
Reverse of second string is=ulab
B) Write a C program to accept a line of characters and print the number of Vowels, Consonants,
blank spaces, digits and special characters.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char line[80];
int i,vow=0,con=0,digit=0,spch=0,spaces=0;
clrscr();
printf("\n Enter a line of text :");
scanf("%[^\n]",line);
Input:-
Enter a line of text : This is 21st century #!
OUTPUT:-
In the given line of text :
No. of vowels is=4
No. of consonants is=11
No. of digits is=2
No. of spaces is=4
No. of special characters is=2
PROGRAM
#include<stdio.h>
#include<conio.h>
void exchange(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\n Enter two numbers :");
scanf("%d%d",&a,&b);
printf("\n Before exchanging ,a=%d and b=%d",a,b);
exchange(&a,&b);
printf("\n After exchanging ,a=%d and b=%d",a,b);
getch();
}
void exchange(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Input:-
Enter two numbers :5 9
OUTPUT:-
Before exchanging ,a=5 and b=9
After exchanging ,a=9 and b=5
B) Write a program to print the elements of an array in reverse order using pointers.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
getch();
}
OUTPUT:
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 3 is : 44
Element 2 is : 33
Element 1 is : 22
Element 0 is : 11
EXPERIMENT-12
FUNTIONS
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
OUTPUT
enter a, b values 12 13
the sum of two numbers in main function is : 25
enter x,y values for sum function: 23 23
the sum of two numbers in sum function is:46
enter a, b values 45 45
the sum of two numbers in main function is : 90
enter x,y values for sum function: 35 35
the sum of two numbers in sum function is:70
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
void sum(int,int);
OUTPUT
enter a, b values: 30 20
the sum of two numbers in main function is : 50
the sum of two numbers in sum function is: 50
enter a, b values: 40 20
the sum of two numbers in main function is : 60
the sum of two numbers in sum function is: 60
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
int sum();
clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
d=sum();
printf("\nthe sum of two numbers from sum function is:%d", d);
getch();
enter a, b values: 90 10
the sum of two numbers in main function is : 100
enter x,y values in sum function: 120 30
the sum of two numbers from sum function is: 150
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
int sum(int,int);
clrscr();
printf("enter a, b values:");
scanf("%d%d", &a, &b);
c= a+b;
printf("\n the sum of two numbers in main function is : %d", c);
d=sum(a,b);
printf("\nthe sum of two numbers from sum function is:%d", d);
getch();
}
int sum(int x, int y)
{
EXPERIMENT-13
FUNTIONS
A) Write a C program to express a four digit number in words. For example 1546 should be
written as one thousand five hundred and forty six
PROGRAM
#include<stdio.h>
#include<conio.h>
void printword(int);
void printtensword(int);
void printteenword(int);
void main()
{
int n,r,i,th,hun,ten,unit;
clrscr();
printf("\n Enter a four digit number:");
scanf("%d",&n);
th=n/1000;
hun=(n%1000)/100;
ten=(n%100)/10;
unit=n%10;
if(n==0)
printf(" Zero ");
if(th>0)
{
printword(th);
void main()
{
int n,f;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
f= rfact(n);
printf("Factorial of %3d is=%d ",n,f);
getch();
}
Input:-
Enter a number:5
OUTPUT:-
Factorial of 5 is=120
C) Write a C program to illustrate the Dynamic Memory allocation function malloc( )
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i;
clrscr();
printf("\n enter size of the an array:");
scanf("%d",&n);
a=(int *)malloc(5*sizeof(int));
if(a==NULL)
{
printf("cant allocate memory address");
exit(0);
}
B) Write a C program to read student roll no, name and marks in six subjects for n number of
students and give class of each student by following the required conditions.
PROGRAM
#include <stdio.h>
struct student
{
char name[50];
int roll;
int marks[6];
} s[10];
int main()
{
int i,j,n,sum=0,avg;
clrscr();
// storing information
printf("enter no.of students");
scanf("%d",&n);
for(i=0; i<n; ++i)
{
s[i].roll = i+1;
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<n; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
for(j=0;j<6;j++)
sum=sum+s[i].marks[j];
avg=sum/6;
if(avg>=70)
printf("\nFirst class");
else
if(avg<=60 && avg>=50)
printf("\nSecond class");
else
printf("\nThird class");
printf("\n");
}
return 0;
}
PROGRAM
typedef struct NODE {
struct NODE *new;
int value;
}Node;
int main(void)
{
Node previous, current;
EXPERIMENT-15
FILES
A) Write a program to create a file and write some text data on the file. Then display the
contents of the file and also print number of characters, number of words, and number
of lines in the file.
PROGRAM
#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;
if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the file\n");
}
getchar();
return(0);
}
a text file named wolf.txt with the following content:
Help Help,
Go away you naughty wolf.
With the above file as input, the program will be show the result as below
OUTPUT
C:\TC3\BIN>type file1.txt
This is a demo file
My college name is PBRVITS, Kavali
C:\TC3\BIN>type file2.txt
My Name is Raghu Chandra
I am studying I btech in PBRVITS, Kavali
My branch is CSE
C:\TC3\BIN>type file3.txt
This is a demo file
My college name is PBRVITS, Kavali
PROGRAM
#include<stdio.h>
void main()
{
char ch;
int loc;
FILE *fp;
fp=fopen(“text.txt”,”r”);
fseek(fp,4,0);
printf(“Location =%d”,ftell(fp));
ch=getc(fp);
printf(“ character is %c”,ch);
}
OUTPUT
text.txt
chadalawada
Location=5
Charater is a
B) Write a program to read student records into a file. Record consists of rollno, name and
marks of a student in six subjects and class. Class field is empty initially. Compute the
class of a student. The calculation of the class is as per CREC rules. Write the first class,
second class, third class and failed students lists separately to another file.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char rollno[10];
char name[15];
int sub[6];
char grade[15];
}s[20];
void main()
if(avg>=70)
{
dc++;
d[dc]=i;
strcpy(s[i].grade,"Distinction");
}
else if(avg>=60)
{
fc++;
f[fc]=i;
Input:-
Enter no. of students :5
Enter students details :
Enter 1 student details :
Roll No :108P1A0312
Name=Rakesh
Enter six subject Marks:65 84 73 81 96 77
OUTPUT:-
C:\TC3\BIN>type results.txt
Details of Distinction students
Roll No Name Grade
108P1A0312 Rakesh Distinction