0% found this document useful (0 votes)
3 views44 pages

Sem 2 Advc

The document contains a series of C programming exercises that demonstrate various concepts such as user-defined functions (UDF), recursion, structures, and sorting algorithms. Each exercise includes code snippets for tasks like checking for palindromes, calculating factorials, finding string lengths, determining prime numbers, and managing student records. The output of each program is also provided to illustrate the expected results.

Uploaded by

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

Sem 2 Advc

The document contains a series of C programming exercises that demonstrate various concepts such as user-defined functions (UDF), recursion, structures, and sorting algorithms. Each exercise includes code snippets for tasks like checking for palindromes, calculating factorials, finding string lengths, determining prime numbers, and managing student records. The output of each program is also provided to illustrate the expected results.

Uploaded by

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

1.

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

3. Write a program to find length of string using UDF

#include<stdio.h>

#include<conio.h>

int mystrlen(char str[30]);

2
void main()

char str[30];

int i,len;

clrscr();

printf("enter the string:\n");

gets(str);

len=mystrlen(str);

printf("Length of given string is %d",len);

getch();

int mystrlen(char str[30])

int i,len=0;

for(i=0;str[i]!='\0';i++)

len++;

return(len);

3
Output
enter the string:

RAHUL KHATRI

Length of given string is 12

4.Write a function prime that returns 1 if its argument is a prime and


return zero Otherwise.
#include<stdio.h>
#include<conio.h>
int prime(int no);
void main()
{
int num,p;
clrscr();
printf("enter the number");
scanf("%d",&num);
p=prime(num);

if(p==1)
printf("\n%d is prime number",num);
else
printf("\n%d is not prime number",num);
getch();
}

int prime(int no)


{
int i,t;
for(i=2;i<no;i++)
{
if(no%i==0)
{
t=0;

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

7.Write a program using a recursive function to find the GCD(Greatest


Common Divisor) of two positive integer.

#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

9.Write a program which explains the use of nesting of functions.


#include <stdio.h>
#include <conio.h>
void add();
void display(int);
void main()
{
int x,y;

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

11.Design a structure student_records to contain Roll_no,Name,City and


Percentage obtained.Develop a program to read data for 5 students and
Display them.

#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);

printf("Enter Name : ");


scanf("%s",s[i].name);

printf("Enter city: ");


scanf("%s",s[i].city);

printf("Enter percentage : ");


scanf("%d",&s[i].percentage);
}
printf("\nDisplaying student_records\n");
for(i=1;i<=5;i++)
{
printf("Roll_No: %d\n",s[i].roll);
printf("Name: %s\n",s[i].name);
printf("city: %s\n",s[i].city);
printf("percentage: %d\n",s[i].percentage);
}
getch();
}

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

13. Write a program using structure within Function.

#include <stdio.h>
#include<conio.h>
struct student {
char name[50];
int per,rno;
};

void display(char name[],int a, int b);

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();
}

void display(char name[],int a, int b )


{
printf("\nDisplaying information\n");
printf("\n Enter the name %s",name);
printf("\nRoll number: %d", a);
printf("\nPercentage: %d", b);
}
Output
Enter the name: mahesh
Enter the roll number : 22
Enter the percentage :70
Displaying information
Enter the name:Mahesh
Roll No:22
15
Percenatage:70

14.Write a program declare following structure member:name,code,age


Weight and height Read all members of the structure for 10 persons
With all related data whose weight>50 and height > 40 and print
the same with suitable format and title .
#include<stdio.h>
#include<conio.h>
struct member
{
char nm[20],cd[20];
int height,age,weight;
}s[10];
void main()
{
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("for person %d\n",i+1);
printf("enter the name");
scanf("%s",s[i].nm);
printf("enter the code");
scanf("%s",s[i].cd);
printf("enter the age");
scanf("%d",&s[i].age);
printf("enter the weight");
scanf("%d",&s[i].weight);
printf("enter the height");
scanf("%d",&s[i].height);
}
printf("\n\nperson having weigtht greater than 50 and height greater than 40\n"
);
printf("\n Name Age Height Weight\n");
printf("-----------------------------\n");
for(i=0;i<10;i++)
{
if(s[i].weight>50 && s[i].height>40)
printf("%s%s%d%d%d\n",s[i].nm,s[i].cd,s[i].age,s[i].height,s[i].weight);

}
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

Enter the name : kinjal


Enter the code : a6
Enter the age : 34
Enter the weight : 78
Enter the height : 67
For person 6
Enter the name : mital
Enter the code : a8
Enter the age : 17
Enter the weight : 45
Enter the height : 34
For person 7
17
Enter the name : jigar
Enter the code : d4
Enter the age : 43
Enter the weight : 67
Enter the height : 55
For person 8
Enter the name : yogini
Enter the code : d9
Enter the age : 31
Enter the weight : 53
Enter the height : 43
For person 9
Enter the name : nikunj
Enter the code : 89
Enter the age : 45
Enter the weight : 59
Enter the height : 34
For person 10
Enter the name : payal
Enter the code : 34
Enter the age : 25
Enter the weight : 56
Enter the height : 45
Persons having weight greater than 50 and height greater than 40
Name Code Age Height Weight
--------------------------------
jay a1 23 46 56
Tejal a3 31 45 55
omkar a5 45 54 67
kinjal a6 34 67 78
jigar d4 43 55 67
yogini d9 31 43 53
payal 34 25 45 56

15. Write a program to use of pointer in arithmetic operation.

#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);

ptr1=&no1;//ptr1 stores address of no1

ptr2=&no2;//ptr2 stores address of 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

16.Write a program to accept 10 numbers and display its sum using


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

{
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

17.Write a program to accept 10 numbers and sort them with use of


Pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp1,temp2;
int arr[10];
int *p;
clrscr();
for(i=0;i<10;i++)

{
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 swap(int *x,int *y);


int a,b;
clrscr();
printf("\n Enter the value of a:- ");
scanf("%d",&a);
printf("\n Enter the value of b:- ");
scanf("%d",&b);
printf("\n\na = %d ",a);
printf("\nb = %d ",b);
swap(&a,&b);
printf("\n\n After swaping \n");
printf("\na = %d ",a);
printf("\nb = %d ",b);
getch();
}
void swap(int *x ,int *y)
{
22
int t;
t=*x;
*x=*y;
*y=t;
}
Output:
Enter the value of a:- 10
Enter the value of b:- 20
a = 10
b = 20
After swaping
a = 20
b = 10

19.Write a program with structure and pointer.


#include<stdio.h>
#include<conio.h>
struct invent
{
char *name[20];
int no;
int price;

};
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:

20.Write a program using pointers to find the length of a string and


compare two Strings.

#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);

printf("The length2 of string =%d",length2);


while(*str1 == *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

21.Write a program using pointers to read an array of integers and print


Its in reverse order.
#include<conio.h>
#include<stdio.h>
void main()
{
int *ptr,i,n;
clrscr();
printf("Enter the no of elements:");
scanf("%d",&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf("Not enough memory");
exit(1);
}

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


{
printf("Enter %d element : ",i+1);
scanf("%d",&ptr[i]);

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

23.Write programs which explain the use of memory allocation functions.


#include<stdio.h>
#include<conio.h>
28
void main()
{
int *p,no;
clrscr();
p = (int *)malloc(sizeof(int));
printf("\n\nEnter any no:");
scanf("%d",&no);
*p = no;
printf("The value=%d\n", *p);
free(p);
getch();
}
Output:
Enter any no:12
The value=12

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

26.Write a c program to read data from keyboard, write it to a file called


input and display data of input file on screen.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char ch;
clrscr();
printf("Data Input \n\n");
f1=fopen("input.txt","w");
while((ch=getchar())!=EOF)
putc(ch,f1);
fclose(f1);
printf("\n\n Data Output \n\n");
f1=fopen("input.txt","r");
while((ch=getc(f1))!=EOF)
printf("%c",ch);
fclose(f1);
getch();
}
Output:
Data Input
This is file handling program
It take Multiple line
^Z
Data Output

31
This is file handling program
It take Multiple line

27.Write a program that counts the number of characters and number of


lines in a file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char ch;
int cha=0,line=1;

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

29.Write a c program to read mark data which contains rollno,name


Sub1,sub2,sub3 file and generate the annual examination results are
tabulated as follows:

Result

Rollno Name Sub1 Sub2 Sub3 Total per% Class

--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - -

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

Enter The Student Roll No. : 2


Enter The Student Name : Parul
Enter The Student Marks
Subject 1 : 34
Subject 2 : 67
Subject 3 : 46
Enter The Student Roll No. : 3
Enter The Student Name : Minaxi
Enter The Student Marks
37
Subject 1 : 23
Subject 2 : 45
Subject 3 : 65
kk.txt

-------------------------------------------------------------
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.

DA=50% of basic HRA=105 of Basic


MA=100PF =10% of Basic
GROSS=BASIC+DA+HRA+MA NET-PAY=GROSS-PF

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
----------------------------------------------------------------------------------------------

31.Write a c program to read empin data file which contains empno


Empname and basic.To create empout data file as per practical no
30 format.

#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
-----------------------------------------------------------------------------------------------------------

32.Write a program using fseek and ftell functions.

#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>

void main(int argc, char *argv[])


{
FILE *fp;
char ch;
clrscr();
42
if( argc != 2)
{
printf("\n Required parameter mssing ..");
printf("\n Help : file1 < file-name> \n");
exit(1);
}
fp =fopen(argv[1],"r");
if(fp==NULL)
{
printf("\n File not found - %s \n",argv[1]);
exit(1);
}
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
Output
abc.txt file contain is read
hi

34.Write a C program to work as a dos copy command using command line


argument.
#include<stdio.h>
#include<conio.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if( argc != 3)
{
printf("\n Required parameter mssing ..");
printf("\n Help : file1 < file-name> \n");
exit(1);
}
fs =fopen(argv[1],"r");
if(fs==NULL)
{
printf("\n File not found - %s \n",argv[1]);
exit(1);
}
43
ft =fopen(argv[2],"w");
while((ch=getc(fs))!=EOF)
{
putc(ch,ft);
}
printf("\n 1 file(s) copied ");
fclose(fs);
fclose(ft);
getch();
}
Output
abc.txt xyz.txt
1 file(s) copied

35.Write a program which explains the use of macro.


#include<stdio.h>
#include<conio.h>
#define CUBE(a) (a*a*a)
void main()
{
int no,cube=219,i,ans;
clrscr();
printf("Enter no:");
scanf("%d",&no);
ans=CUBE(no);
printf("CUBE=%d\n\n",ans);
for(i=1;i<=ans;i++)
printf("%c ",cube);
getch();
}
Output:
Enter no:3
CUBE=27
􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀
􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀 􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀􀀀

44

You might also like