Sem 2 Advc
Sem 2 Advc
Write a program to check the given number is palindrome or not using User
Defined Function (UDF)
#include<stdio.h>
#include<conio.h>
void palindrom(int );
void main()
{
int no;
clrscr();
printf("\n\n\t\t\t Enter The Number ::= ");
scanf("%d",&no);
palindrom(no);
getch();
}
void palindrom(int n)
{
int temp,rem,rev=0;
temp=n;
while(n>=1)
{
rem=n%10;
n=n/10;
rev=rev*10+rem;
}
printf("\n\n\t\t<------------------- OUTPUT ----------------->");
if(temp==rev)
{
printf("\n\n\t\t\t\t %d is Palindrome",temp);
}
else
{
printf("\n\n\t\t\t\t %d is Not Palindrome",temp);
}
}
Output:
Enter The Number ::= 252
<------------------- OUTPUT ----------------->
252 is Palindrome
1
2.Write a program to find factorial of given no using UDF
#include<stdio.h>
#include<conio.h>
long factorial(int );
void main()
{
int num;
long f;
clrscr();
printf("Enter the number : - ");
scanf("%d",&num);
f=factorial(num);
printf("Factorial of = %ld” ,f);
getch();
}
long factorial(int no)
{
long fact=1;
while(no > 0)
{
fact = fact * no;
no--;
}
return (fact);
}
Output:
Enter the number : 5
Factorial of 5 = 120
#include<stdio.h>
#include<conio.h>
2
void main()
char str[30];
int i,len;
clrscr();
gets(str);
len=mystrlen(str);
getch();
int i,len=0;
for(i=0;str[i]!='\0';i++)
len++;
return(len);
3
Output
enter the string:
RAHUL KHATRI
if(p==1)
printf("\n%d is prime number",num);
else
printf("\n%d is not prime number",num);
getch();
}
break;
}
4
if(t==0)
return(0);
else
return(1);
Output:
Enter the number :17
17 is prime number
5.Write a program to find factorial of given no using recursion.
#include<stdio.h>
#include<conio.h>
long factorial(int );
void main()
{
int num;
long f;
clrscr();
printf("Enter the number : - ");
scanf("%d",&num);
f=factorial(num);
printf("Factorial of = %ld",f);
getch();
}
long factorial(int no)
{
long fact;
if(no==1)
return (1);
else
fact=no*factorial(no-1);
return (fact);
}
Output:
Enter the number : 6
Factorial of 6 = 720
5
6.write a program to display first 25 terms of Fibonacci series using
recursion.
#include<stdio.h>
#include<conio.h>
void fibonacci(long, long, int);
void main()
{
long pre=0,cur=1;
int i=3;
clrscr();
printf("\n::Fibonacci Series::\n");
printf("\n%ld\t%ld",pre,cur);
fibonacci(pre,cur,i);
getch();
}
void fibonacci(long pre, long cur, int i)
{
long next;
if (i <= 25)
{
next = cur + pre;
printf("\t%ld",next);
pre = cur;
cur = next;
i++;
fibonacci(pre,cur,i);
}
}
Output:
::Fibonacci Series::
0 1 1 2 3 5 8 13 21 34
55 89 144 233 377 610 987 1597 2584 4181
6765 10946 17711 28657 46368
#include<stdio.h>
#include<conio.h>
int findgcd(int x,int y);
6
void main()
{
int n1,n2,gcd;
clrscr();
printf("\nEnter two numbers: ");
scanf("%d %d",&n1,&n2);
if( n1 <= 0 && n2 <= 0)
{
printf("\nEnter Positive Number only ..");
}
else
{
gcd=findgcd(n1,n2);
printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
}
getch();
}
int findgcd(int x,int y)
{
while(x!=y)
{
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}
Output:
Enter two numbers: 42 28
GCD of 42 and 28 is: 14
8.Write a program that uses a UDF to sort an array of integer.
#include<stdio.h>
#include<conio.h>
void sort(int no,int a[10]);
void dispaly(int no,int a[10]);
int i,j,temp;
void main()
{
int num[10],n;
7
clrscr();
printf("how many number u want 2 enter :- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the number%d :- ",i);
scanf("%d",&num[i]);
}
printf("\n\n::BEFORE SORT ARRAY::\n");
dispaly(n,num);
sort(n,num);
printf("\n\n ::SORTING OF ARRAY:: \n");
dispaly(n,num);
getch();
}
void sort(int no,int a[10])
{
int temp;
for(i=0;i<no;i++)
{
for(j=i;j<no;j++)
{
if(a[i] > a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void dispaly(int no,int a[10])
{
for(i=0;i<no;i++)
printf("\t %d",a[i]);
}
Output:
how many number u want 2 enter :- 5
Enter the number1 :- 23
Enter the number2 :- 12
Enter the number3 :- 56
8
Enter the number4 :- 34
Enter the number5 :- 67
::BEFORE SORT ARRAY::
23 12 56 34 67
::SORTING OF ARRAY::
12 23 34 56 67
clrscr();
printf("\nEnter no1=");
scanf("%d",&x);
printf("\nEnter no2=");
scanf("%d",&y);
add(x,y);
getch();
}
void add(int a, int b)
{
int ans;
ans=a+b;
printf("\nAddition");
display(ans);
}
void display(int d)
{
printf("=%d",d);
}
Output:
Enter no1=10
Enter no2=20
Addition=30
9
10.Define a structure type struct personal that would contain person name
Date of joining and salary using this structure to read this information
And display on screen
#include <stdio.h>
#include <conio.h>
struct personal
{
char name[50];
char doj[20];
int salary;
}p[5];
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("\n\n\t Enter Person Name : ");
scanf("%s",p[i].name);
printf("\n\n\t Person Date of Joining : ");
scanf("%s",p[i].doj);
printf("\n\n\t Person Salary : ");
scanf("%d",&p[i].salary);
}
for(i=0;i<5;i++)
{
printf("\n\t Person Name : %s",p[i].name);
printf("\n\t Date of Joining : %s",p[i].doj);
printf("\n\t Person Salary : %d",p[i].salary);
}
getch();
}
Output:
Enter Person Name : Sanjay
Person Date of Joining : 1/6/2022
Person Salary : 10000
Enter Person Name : Bharat
Person Date of Joining : 14/5/2021
Person Salary : 9000
Enter Person Name : Bhavik
Person Date of Joining : 4/6/2020
Person Salary : 12000
10
Enter Person Name : Kirit
Person Date of Joining : 5/8/2022
Person Salary : 21000
Enter Person Name : Pravin
Person Date of Joining : 1/12/2021
Person Salary : 10000
Person Name : Sanjay
Date of Joining : 1/6/2022
Person Salary : 10000
Person Name : Bharat
Date of Joining : 14/5/2021
Person Salary : 9000
Person Name : Bhavik
Date of Joining : 4/6/2020
Person Salary : 12000
Person Name : Kirit
Date of Joining : 5/8/2022
Person Salary : 21000
Person Name : Pravin
Date of Joining : 1/12/2021
Person Salary : 10000
#include <stdio.h>
#include<conio.h>
struct student
{
char name[50];
char city[100];
int roll;
int percentage;
s[100];
};
void main()
{
int i;
struct student s[100];
11
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter Roll _No. : ");
scanf("%d",&s[i].roll);
Output
Enter Roll_No:1
Enter Name:Mahesh
Enter City:Deesa
Enter percenatage:80
Enter Roll_No:2
Enter Name:Ramesh
Enter City:Deesa
Enter percenatage:70
Enter Roll_No:3
Enter Name:Naresh
12
Enter City:Deesa
Enter percenatage:60
Enter Roll_No:4
Enter Name:Haresh
Enter City:Deesa
Enter percenatage:50
Enter Roll_No:5
Enter Name:Kamlesh
Enter City:Deesa
Enter percenatage:40
Displaying student_records
Roll_No1
Name:mahesh
City:deesa
Percentage:80
Roll_No2
Name:ramesh
City:deesa
Percentage:70
Roll_No3
Name:naresh
City:deesa
Percentage:60
Roll_No4
Name:haresh
City:deesa
Percentage:50
Roll_No5
Name:kamlesh
City:deesa
Percentage:40
13
12.Write a program using structure within structure.
#include <stdio.h>
#include <conio.h>
struct stud_Inf
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}result;
void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&result.rno);
printf("\n\t Enter Semester : ");
scanf("%s",result.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t<-:Student Information:->");
printf("\n\n\t Roll Number : %d",result.rno);
printf("\n\n\t Standard : %s",result.std);
printf("\n\n\t Subject Code : %s",result.marks.subj_nm);
printf("\n\n\t Marks : %d",result.marks.subj_mark);
getch();
}
Output:
Enter Roll Number : 37
Enter Semester : FYBCA_SEM-1
Enter Subject Code : BCA_101
Enter Marks : 89
<-:Student Information:->
Roll Number : 37
Standard : FYBCA_SEM-BCA_101
Subject Code : BCA_101
14
Marks : 89
#include <stdio.h>
#include<conio.h>
struct student {
char name[50];
int per,rno;
};
void main() {
struct student s1;
clrscr();
printf("Enter name: ");
scanf("%s",&s1.name);
printf("Enter the roll number: ");
scanf("%d",&s1.rno);
printf("Enter percentage: ");
scanf("%d", &s1.per);
display(s1.name,s1.rno,s1.per);
getch();
}
}
16
getch();
}
Output:
For person 1
Enter the name : jay
Enter the code : a1
Enter the age : 23
Enter the weight : 56
Enter the height : 46
For person 2
Enter the name : Parul
Enter the code : a2
Enter the age : 24
Enter the weight : 45
Enter the height : 43
For person 3
Enter the name : Tejal
Enter the code : a3
Enter the age : 31
Enter the weight : 55
Enter the height : 45
For person 4
Enter the name : omkar
Enter the code : a5
Enter the age : 45
Enter the weight : 67
Enter the height : 54
For person 5
#include<stdio.h>
#include<conio.h>
void main()
{
18
int no1,no2;
int *ptr1,*ptr2;
int sum,sub,mult;
float div;
clrscr();
printf("Enter number1:\n");
scanf("%d",&no1);
printf("Enter number2:\n");
scanf("%d",&no2);
sum=(*ptr1) + (*ptr2);
sub=(*ptr1) - (*ptr2);
mult=(*ptr1) * (*ptr2);
div=(*ptr1) / (*ptr2);
printf("sum= %d\n",sum);
printf("subtraction= %d\n",sub);
printf("Multiplication= %d\n",mult);
19
printf("Division= %f\n",div);
getch();
Output
Enter number1:
36
Enter number2:
12
Sum=48
Subtraction=24
Multiplication=432
Division=3.000000
{
int i,n,sum=0;
int a[10],*p;
clrscr();
printf("Enter Elements of First List\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
p=&a[0];
for(i=0;i<10;i++)
{
sum = sum + *(p+i);
}
printf("Sum of all elements in array = %d\n", sum);
getch();
}
Output:
20
Enter Elements of First List
10
20
55
84
12
5
14
8
9
42
Sum of all elements in array = 259
{
printf("Enter no%d=",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(*(arr+j)>*(arr+j+1))
{
p=arr+j;
temp1=*p++;
temp2=*p;
*p--=temp1;
*p=temp2;
}
}
21
}
printf("\n\nAfter Sorting::");
for(i=0;i<10;i++)
printf(" %d",arr[i]);
getch();
}
Output:
Enter no1=23
Enter no2=22
Enter no3=45
Enter no4=21
Enter no5=54
Enter no6=11
Enter no7=68
Enter no8=44
Enter no9=43
Enter no10=99
After Sorting:: 11 21 22 23 43 44 45 54 68 99
18.Write a program to swap the two values using pointers and UDF.
#include<stdio.h>
#include<conio.h>
void main()
{
};
void main()
{
struct invent product[2],*ptr;
clrscr();
printf("\n\t\t::Input::");
for(ptr=product;ptr<product+2;ptr++)
{
printf("\n\n\tEnter Product name,no and price:");
scanf("%s%d%d",ptr->name,&ptr->no,&ptr->price);
}
printf("\n\t\t::Output::");
for(ptr=product;ptr<product+2;ptr++)
{
printf("\n\n\tProduct name=%s",ptr->name);
printf("\n\n\tProduct no=%d",ptr->no);
printf("\n\n\tProduct price=%d",ptr->price);
}
getch();
23
}
Output:
::Input::
Enter Product name,no and price:Keyboard 1 1000
Enter Product name,no and price:Mouse 2 300
::Output::
Product name=Keyboard:
Product no=1:
Product price=1000:
Product name=Mouse:
Product no=2:
Product price=300:
#include <stdio.h>
#include<conio.h>
void main()
{
//Compare Two strigs using Pointers
char string1[50],string2[50],*str1,*str2;
int i,length1,length2;
clrscr();
printf("Enter The First String: ");
scanf("%s",string1);
printf("Enter The Second String: ");
scanf("%s",string2);
str1 = string1;
str2 = string2;
length1=strlen(str1);
while(str1!='\0')
{
str1++;
}
printf("The length1 of string =%d",length1);
length2=strlen(str2);
24
{
if ( *str1 == '\0' || *str2 == '\0' )
break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
getch();
}
OUTPUT
Enter The First String :rahul
Enter The Second String:raj
The length1 of string =5
The length2 of string =3
Both String Are Not Equal
25
}
printf("Array in original order\n");
for(i=0; i<n; i++)
{
printf("%d\n",ptr[i]);
}
printf("Array in reverse order\n");
for(i=n-1; i>=0; i--)
{
printf("%d\n",ptr[i]);
}
getch();
}
Output:
Enter the no of elements:5
Enter 1 element : 11
Enter 2 element : 22
Enter 3 element : 33
Enter 4 element : 44
Enter 5 element : 55
Array in original order
11
22
33
44
55
Array in reverse order
55
44
33
22
11
22.Write a program using UDF and pointers to add two matrices and to
return the resultant matrix to the calling function.
#include <stdio.h>
#include <conio.h>
int m1[3][3];
int i,j,m2[3][3],m3[3][3];
26
void add(int m1[3][3],int m2[3][3],int m3[3][3])
{
int *p1=m1,*p2=m2,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m3[i][j]=(*p1)+(*p2);
p1++;
p2++;
}
}
}
void main()
{
clrscr();
printf("\nEnter value of first %d by %d matrix", 3,3);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter value of [%d][%d] element : ", i+1, j+1);
scanf("%d", &m1[i][j]);
}
}
printf("\nEnter value of second %d by %d matrix", 3,3);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter value of [%d][%d] element : ", i+1, j+1);
scanf("%d", &m2[i][j]);
}
}
add(m1,m2,m3);
printf("\n\nAddition of two matrix:->\n ");
27
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",m3[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter value of first 3 by 3 matrix
Enter value of [1][1] element : 1
Enter value of [1][2] element : 2
Enter value of [1][3] element : 3
Enter value of [2][1] element : 4
Enter value of [2][2] element : 5
Enter value of [2][3] element : 6
Enter value of [3][1] element : 7
Enter value of [3][2] element : 8
Enter value of [3][3] element : 9
Enter value of second 3 by 3 matrix
Enter value of [1][1] element : 1
Enter value of [1][2] element : 2
Enter value of [1][3] element : 3
Enter value of [2][1] element : 4
Enter value of [2][2] element : 5
Enter value of [2][3] element : 6
Enter value of [3][1] element : 7
Enter value of [3][2] element : 8
Enter value of [3][3] element : 9
Addition of two matrix:->
246
8 10 12
14 16 18
24.Create
one text file store some information into it and print the
Same information on terminal.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char ch;
clrscr();
f1=fopen("input.txt","r");
while((ch=getc(f1))!=EOF)
printf("%c",ch);
fclose(f1);
getch();
}
Output:
Welcome to College
25.A file named data contains series of integer no. Write a c program to
read that no and then write all odd no into file named odd no.and
write all even no into file named even no.Display all the contents
of these file on screen.
#include<stdio.h>
29
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf("Enter the numbers :- ");
printf("\n Enter -1 for stop for add number into file:- \n\n");
f1=fopen("data.txt","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number == -1) break;
putw(number,f1);
}
fclose(f1);
f1=fopen("data.txt","r");
f2=fopen("odd.txt","w");
f3=fopen("even.txt","w");
while((number = getw(f1)) != EOF)
{
if(number % 2 == 0)
{
putw(number,f3);
}
else
{
putw(number,f2);
}
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("odd.txt","r");
f3=fopen("even.txt","r");
printf("\n\n Content of odd file \n\n");
while((number = getw(f2)) != EOF)
printf("%d ",number);
printf("\n\n Content of even file \n\n");
while((number = getw(f3)) != EOF)
printf("%d ",number);
30
fclose(f2);
fclose(f3);
getch();
}
Output:
Enter the numbers :-
Enter -1 for stop for add number into file:-
123456
-1
Content of odd file
135
Content of even file
246
31
This is file handling program
It take Multiple line
clrscr();
f1 = fopen("abc.txt","r");
while((ch =getc(f1)) != EOF)
{
cha++;
if(ch == ' ')
{
cha--;
}
if(ch == '\n')
{
line++;
cha--;
}
}
fclose(f1);
printf("\n\n =================================\n\n");
printf("\n characters :- %d",cha);
printf("\n lines :- %d",line);
printf("\n\n =================================\n\n");
getch();
}
Output
=================================
characters :- 11
lines :- 2
=================================
abc.txt
32
Hi
How are you
28. Two files DATA1 and DATA2 contains lists of integers.Write a program
to produce a third file DATA which holds a single sorted,merged list of
these two lists.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,j,k,i,temp,sort[40];
clrscr();
printf("Enter the numbers:-");
printf("Enter -1 for stop for add number into file:-\n\n");
f1=fopen("data1.txt","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,f1);
}
fclose(f1);
printf("Enter the numbers:-");
printf("Enter -1 for stop for add number into file:-\n\n");
f2=fopen("data2.txt","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,f2);
}
fclose(f2);
f1=fopen("data1.txt","r");
33
f3=fopen("data3.txt","w");
while((number=getw(f1))!=EOF)
{
putw(number,f3);
}
fclose(f1);
f2=fopen("data2.txt","r");
while((number=getw(f2))!=EOF)
{
putw(number,f3);
}
fclose(f2);
fclose(f3);
f3=fopen("data3.txt","r");
i=0;
while((number=getw(f3))!=EOF)
{
sort[i]=number;
i++;
}
fclose(f3);
f3=fopen("data3.txt","w");
for(j=0;j<i;j++)
{
for(k=j;k<i;k++)
{
if(sort[j]>sort[k])
{
temp=sort[j];
sort[j]=sort[k];
sort[k]=temp;
}
}
putw(sort[j],f3);
}
fclose(f3);
f3=fopen("data3.txt","r");
34
while((number=getw(f3))!=EOF)
{
printf("\n%d",number);
}
fclose(f3);
getch();
}
Output
Enter the numbers :-
Enter -1 for stop for add number into file:-
12
23
45
68
-1
Enter the numbers :-
Enter -1 for stop for add number into file:-
36
46
78
63
-1
12
23
36
45
46
63
68
78
Result
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - -
35
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
int sub[3];
int total,per,gread;
};
struct student s[20];
void main()
{
FILE *fp;
int n,i,j;
char a[5][10],res[13];
clrscr();
printf("\n\nHow Many Record You Want To Insert : ");
scanf("%d",&n);
fp=fopen("kk.txt","a");
fprintf(fp,"-----------------------------------\n");
fprintf(fp,"NO.\tName\tSub1\tSub2\tSub3\tTotal\tPer%\tClass\n");
fprintf(fp,"-----------------------------------\n");
for(i=0;i<n;i++)
{
printf("\n\n\n\tEnter The Student Roll No. : ");
scanf("%d",&s[i].rollno);
printf("\n\n\tEnter The Student Name : ");
scanf("%s",s[i].name);
printf("\n\n\tEnter The Student Marks");
for(j=0;j<3;j++)
{
printf("\n\n\t\tSubject %d : ",1+j);
scanf("%d",&s[i].sub[j]);
}
s[i].total=s[i].sub[0]+s[i].sub[1]+s[i].sub[2];
s[i].per=s[i].total/3;
if(s[i].sub[0] >= 35 && s[i].sub[1]>=35 && s[i].sub[2] >=35 )
{
if( s[i].per >= 70)
{
strcpy(res,"Dist.");
36
}
else if(s[i].per>=60)
{
strcpy(res,"First");
}
else if(s[i].per>=50)
{
strcpy(res,"Second");
}
else
{
strcpy(res,"Pass");
}
}
else
{
strcpy(res,"Fail");
}
fprintf(fp,"%d\t%s\t%d\t%d\t%d\t%d\t%d\t%s\n",s[i].rollno,s[i].name,s[i].sub[0
],
s[i].sub[1],s[i].sub[2],s[i].total,s[i].per,res);
}
fclose(fp);
}
Output:
How Many Record You Want To Insert : 3
Enter The Student Roll No. : 1
Enter The Student Name : Parth
Enter The Student Marks
Subject 1 : 34
Subject 2 : 45
Subject 3 : 54
-------------------------------------------------------------
NO. Name Sub1 Sub2 Sub3 Total Per% Class
-------------------------------------------------------------
1 Parth 34 45 54 133 44 Fail
2 Parul 34 67 46 147 49 Fail
3 Minaxi 23 45 65 133 44 Fail
30.Write a c program to input employee no, employee name and basic and
to Store output into empdata file in following format.
A/C Department
- - - - - - - - - - - - -- -- - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - -
Emp-No Name Basic DA HRA MA PF GROSS NET-PAY
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - -- - -
1 XYZ 5000 2500 500 100 500 8100 7500
2
3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
#include<stdio.h>
#include<conio.h>
struct emp
{
int no,basic;
char name[20];
float da,hra,ma,pf,gross,net;
};
Struct emp s[20];
38
void main()
{
FILE *fp;
int n,i;
clrscr();
printf("\n\nHow Many Record You Want To Insert : ");
scanf("%d",&n);
fp=fopen("ks.txt","a");
fprintf(fp,"------------------------------------------------------\n");
fprintf(fp,"NO.\tName\tBasic\tDA\tHRA\tMA\tPF\tGROSS\tNET-PAY\n");
fprintf(fp,"------------------------------------------------------\n");
for(i=0;i<n;i++)
{
clrscr();
printf("\n\n\n\tEnter The Employee No : ");
scanf("%d",&s[i].no);
printf("\n\n\tEnter The Employee Name : ");
scanf("%s",s[i].name);
printf("\n\n\tEnter The Basic Salary : ");
scanf("%d",&s[i].basic);
s[i].da=s[i].basic*0.50;
s[i].hra=s[i].basic*0.10;
s[i].ma=100;
s[i].pf=s[i].basic*0.10;
s[i].gross=s[i].basic+s[i].da+s[i].hra+s[i].ma;
s[i].net=s[i].gross-s[i].pf;
fprintf(fp,"%d\t%s\t%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.2f\t\n",s[i].no,s[i].na
me
,s[i].basic,s[i].da,s[i].hra,s[i].ma,s[i].pf,s[i].gross,s[i].net);
}
fprintf(fp,"------------------------------------------------------\n");
fclose(fp);
}
Output:
How Many Record You Want To Insert : 2
Enter The Employee No : 1
Enter The Employee Name : Parul
Enter The Basic Salary : 12500
Enter The Employee No : 2
Enter The Employee Name : Minaxi
Enter The Basic Salary : 15000
39
ks.txt
----------------------------------------------------------------------------------------------
NO. Name Basic DA HRA MA PF GROSS NET-PAY
----------------------------------------------------------------------------------------------
1 Parul 12500 6250.0 1250.0 100.0 1250.0 20100.0 18850.00
2 Minaxi 15000 7500.0 1500.0 100.0 1500.0 24100.0 22600.00
----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp
{
int no,basic;
char name[20];
float da,hra,ma,pf,gross,net;
};
struct emp s[20];
void main()
{
FILE *fp,*fp1;
int n,i;
clrscr();
fp=fopen("empin.txt","r");
fp1=fopen("empout.txt","w");
fprintf(fp1,"------------------------------------------------------------------------------
\n");
fprintf(fp1,"NO.\tName\tBasic\tDA\tHRA\tMA\tPF\tGROSS\tNET-PAY\n");
fprintf(fp1,"------------------------------------------------------------------------------
\n");
while(fscanf(fp,"%d %s %d",&s[i].no,s[i].name,&s[i].basic) != EOF)
{
printf("%d %s %d",s[i].no,s[i].name,s[i].basic);
s[i].da=s[i].basic*0.50;
s[i].hra=s[i].basic*0.10;
s[i].ma=100;
s[i].pf=s[i].basic*0.10;
40
s[i].gross=s[i].basic+s[i].da+s[i].hra+s[i].ma;
s[i].net=s[i].gross-s[i].pf;
fprintf(fp1,"%d\t%s\t%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.2f\t\n",s[i].no,s[i].na
me,s[
i].basic,s[i].da,s[i].hra,s[i].ma,s[i].pf,s[i].gross,s[i].net);
}
fprintf(fp1,"------------------------------------------------------------------------------
\n");
fclose(fp1);
fclose(fp);
}
Output
empin.txt
1 Bhargav 12500
2 Harshad 10000
empout.txt
-----------------------------------------------------------------------------------------------------------
NO. Name Basic DA HRA MA PF GROSS NET-PAY
-----------------------------------------------------------------------------------------------------------
1 Bhargav 12500 6250.0 1250.0 100.0 1250.0 20100.0 18850.00
2 Harshad 10000 5000.0 1000.0 100.0 1000.0 16100.0 15100.00
-----------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
long n;
char c;
fp=fopen("RANDOM.txt","w");
while((c=getchar())!=EOF)
putc(c,fp);
41
printf("No Charcters Entered =%ld\n",ftell(fp));
fclose(fp);
fp=fopen("RANDOM.txt","r");
n=0L;
while(feof(fp)==0)
{
fseek(fp,n,0);
printf("Position of %c is %ld\n",getc(fp),ftell(fp));
n=n+5L;
}
putchar('\n');
fclose(fp);
getch();
}
Output
rahul^Z
No character Entered = 5
Positon of r is 0
Position of is 5
33.Write a C program to work as a dos type command using command line
argument.
#include<stdio.h>
#include<conio.h>
44