C Lab Manual Record CSE18R153: Submitted By: T.bindamrutha. 9919004275. A9
C Lab Manual Record CSE18R153: Submitted By: T.bindamrutha. 9919004275. A9
Record
CSE18R153
Submitted by:
T.bindamrutha.
9919004275.
A9
Unrestricted
Q.1 write a c program to find factorial of a number using functions.
Source code:
#include <stdio.h>
int fact(int);
void main()
{
int no,factorial;
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
Unrestricted
}
return f;
}
Q.2Write a c program to find the smallest and largest number using user defined
functions.
Aim: to find the smallest and largest number using user defined functions.
Algorithm:start
3.find the biggest and smallest numbers from the given numbers
5.stop
Source code:
#include <stdio.h>
int main()
{
int num1, num2, maximum, minimum;
Unrestricted
maximum = max(num1, num2);
minimum = min(num1, num2);
return 0;
}
Source code:
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
root = sqrt(num);
Unrestricted
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}
Output:
Enter any number to find square root:9
Square of 9=3.
Source code:
#include <stdio.h>
int main()
int num;
double n;
Unrestricted
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");
scanf("%d", &num);
n = square(num);
return 0;
Output:
Algorithm:start
6.stop
Source code:
#include <stdio.h>
Unrestricted
/* Function declaration */
double cube(double num);
int main()
{
int num;
double c;
c = cube(num);
return 0;
}
/**
* Function to find cube of any number
*/
double cube(double num)
{
return (num * num * num);
}
Output:
Cube of 5 is:125
Q.6.write a c program to find the given number is prime or not using function.
Algorithm:start
Unrestricted
2.enter a number.
6.stop
Source code:
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Unrestricted
Output:
Enter a positive number:29
29 is a prime number.
Algorithm:start
5.stop.
Source code:
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
Unrestricted
Output:
Enter a positive integer: 20
Sum = 210
Q.8.write a c program to find the sum and average of n numbers using functions.
Source code:
#include<stdio.h>
void main()
{
int i,n,Sum=0,numbers;
float Average;
Average = Sum/n;
Unrestricted
printf("\nAverage of the %d Numbers = %.2f",n, Average);
return 0;
}
Output:
Please enter how many numbers you want?
3
Please enter the elements one by one
10
20
30
Sum of 3 numbers=60
Average of 3 numbers=30
Source code:
#include<stdio.h>
void calculate_power(int,int);
void main()
{
int b,e;
printf("Enter the base\n");
scanf("%d",&b);
printf("Enter the exponent\n");
scanf("%d",&e);
calculate_power(b,e);
Unrestricted
}
Output:
Enter the base
4
Enter the exponent
3
The power of the number =64
Source code:
#include <stdio.h>
int main()
{
int num, result;
Unrestricted
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
Output:
Enter a number:3465
Sum of digits in 3465 is 18.
Q.11.write a c program to find the maximum and minmum among list of numbers
using pointers.
Aim: To find the maximum and minmum among list of numbers using pointers.
Algorithm:start.
2.enter a list of numbers.
3.find the maximum and minimum numbers.
4.print the result.
5.stop.
Source code:
#include <stdio.h>
#include <conio.h>
Unrestricted
int main()
int arr[10];
min = arr[0];
printf("Enter up to 10 numbers:\n");
for(i=0;i<=10;i++)
{
scanf("%d",&arr[i]);
ptr1 = arr;
ptr2 = arr;
for(i=1;i<=10;i++)
{
temp = max;
max = *ptr1;
Unrestricted
*ptr1 = temp;
ptr1++;
printf("MAX=%d\n",max);
// finding minimum
for(j=1;j<=10;j++) {
tempmin = *ptr2;
*ptr2 = min;
min = tempmin;
ptr2++;
printf("MIN=%d\n",min);
system("PAUSE");
return 0;
Unrestricted
0utput:
Enter up to 10 numbers:
23
5
66
87
34
65
32
21
25
85
45
MAX=87
MIN=5
Algorithm:start
6.stop.
Source code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Unrestricted
void main()
{
int *a,n,i,j,t;
clrscr();
printf("\nEnter the number of integers to be sorted in ascending order: ");
scanf("%d",&n);
a=(int *)malloc(n *sizeof(int));
printf("\nEnter the %d numbers to be sorted in ascending order one by one: ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", (a+i));
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(*(a+i)<*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("\nAfter sorting in ascending order: ");
for(i=0;i<n;i++)
printf("\n%d",*(a+i));
getch();
}
Output:
Unrestricted
Enter the 5 numbers to be sorted in ascending order one by one:56
35
567
28
28
35
56
567
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
int main() {
int i, * ptr, sum = 0;
ptr = calloc(10, sizeof(int));
Unrestricted
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Building and calculating the sequence sum of the first 10 terms \ n ");
for (i = 0; i < 10; ++i) { * (ptr + i) = i;
sum += * (ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Output:
Algorithm:start
5.stop.
Source code:
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
Unrestricted
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
Output:
Algorithm:start.
5.stop.
Source code:
#include<stdio.h>
Unrestricted
#include<conio.h>
#define MAX 30
void main() {
int *ptr;
clrscr();
ptr = &arr[0];
scanf("%d", &size);
scanf("%d", ptr);
ptr++;
Unrestricted
for (i = size - 1; i >= 0; i--) {
ptr--;
getch();
Output:
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11
Algorithm:start.
Unrestricted
3.swap two numbers using pointers.
5.stop.
Source code:
#include <stdio.h>
int main()
{
int num1,num2;
swap(&num1,&num2);
return 0;
}
Unrestricted
Output:’
Algorithm:start
2.enter a string.
5.stop.
Source code:
#include<stdio.h>
int main() {
char str[20], *pt;
int i = 0, c = 0;
printf("Pointer Example Program : Count Vowels In String \n");
printf("\nEnter Any string in small [below 20 chars] : ");
gets(str);
pt = str;
Unrestricted
while (*pt != '\0') {
if (*pt == 'a' || *pt == 'e' || *pt == 'i' || *pt == 'o' || *pt == 'u')
c++;
i++;
pt++;
}
printf("\nLength of String : %d", i);
printf("\nVowels Count In the String : %d", c);
printf("\nConstant Count in the String : %d", (i - c));
return 0;
}
Output:
Length of String : 7
Vowels Count In the String : 3
Constant Count in the String : 4
Algorithm:start.
Unrestricted
4.print the result.
5.stop.
Source code:
Unrestricted
3.enter the some numbers in array.
4.add the elements in an array.
5.print the result.
6.stop.
Source code:
#include <stdio.h>
#include <malloc.h>
void main()
{
int i, n, sum = 0;
int *a;
Output:
Unrestricted
Enter the size of array A
5
Enter Elements of the List
4
9
10
56
100
Sum of all elements in array = 179
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
int main(void)
{
int *ptr;
ptr = #
Unrestricted
printf("value of num = %d\n", *ptr);
return 0;
}
Output:
Value of num=100
Algorithm:start
6.stop.
Source code:
#include <stdio.h>
Unrestricted
scanf("%d",&arr[i]);
}
}
int main()
{
int arr[10];
readArray(arr,10);
printArray(arr,10);
return 0;
}
Output:
Enter elements :
Enter arr[0] : 1
Enter arr[1] : 2
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 6
Enter arr[6] : 7
Unrestricted
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10
Elements are :
arr[0] : 1
arr[1] : 2
arr[2] : 3
arr[3] : 4
arr[4] : 5
arr[5] : 6
arr[6] : 7
arr[7] : 8
arr[8] : 9
arr[9] : 10
Algorithm:start
5.print result.
6.stop.
Source code:
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
Unrestricted
printf("Enter the numbers of elements: ");
scanf("%d", &n);
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
Output:
Q.23.write a c program to find the counting the occurrence of the given number
in an array.
Unrestricted
Aim:To find the counting the occurrence of the given number in an array.
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i;
int num,count;
Unrestricted
if(arr[i]==num)
count++;
}
printf("Occurrence of %d is: %d\n",num,count);
return 0;
}
Output:
Q.24.write a c program to merge the elements of two different arrays into a third
array.
Aim: To merge the elements of two different arrays into a third array.
Algorithm:start.
6.stop.
Source code:
Unrestricted
#include <conio.h>
print(int *a,int n)
{
int i;
printf("%d ",a[i]);
int main()
{
int a[10000],b[10000],c[20000],i,n1,n2 ;
Unrestricted
for(i=0; i<n1+n2; i++)
{
if(i<n1)
c[i]=a[i];
else
c[i]=b[i-n1];
print(a,n1);
print(b,n2);
print(c,n1+n2);
return 0;
}
Output:
Unrestricted
11 4
12 5
13 1st array :
14 1 2 3 4 5
15 2nd array :
16 1 2 3 4 5
17 3rd array :
18 1 2 3 4 5 1 2 3 4 5
Q.25.write a c program to find the particular element and its position in the given
array.
Aim:To find the particular element and its position in the given array.
Algorithm:start.
6.stop.
Source code:
#include<stdio.h>
void main()
int i,n,a[100],search,flag=0;
Unrestricted
scanf("%d",&n) ;
for(i=0;i<n;i++)
scanf("%d",&a[i]) ;
scanf("%d",&search);
for(i=0;i<n;i++)
if(a[i]==search)
flag=1;
break;
if(flag==0)
Unrestricted
{
Output:
7 8 12 3
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
void main()
{
Unrestricted
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
a = number[i];
number[i] = number[j];
number[j] = a;
Output:
Unrestricted
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
void main()
{
Unrestricted
scanf("%d", &num);
printf("Enter the elements of the array \n");
}
Output:
Unrestricted
23
Even numbers in the array are - 12 98
Odd numbers in the array are - 19 45 69 23
Algorithm:start.
5.stop.
Source code:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
Unrestricted
Output:
Algorithm:start.
Source code:
#include<stdio.h>
void fibo(int);
void main()
int n;
Unrestricted
printf("\nEnter a number to generate fibonacci series for first n terms\n",n);
scanf("%d",&n);
fibo(n);
void fibo(int n)
int i,c=0;
int a=0;
int b=1;
for(i=0;i<n;i++)
printf("%d ",c);
a=b;
b=c;
c=a+b;
Output:
Unrestricted
Fibanocci series for 7 terms :
0112358
Algorithm:start
6.stop.
Source code:
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
Unrestricted
Output:
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
void display_sum(int);
int main()
{
int num;
Unrestricted
void display_sum(int num)
{
static int sum = 0;
if (num == 0)
{
printf("Sum of first N numbers is %d\n", sum);
return;
}
else
{
sum += num;
display_sum(--num);
}
}
Output:
Algorithm:start.
6.stop.
Unrestricted
Source code:
#include <stdio.h>
int main()
{
int num;
Output:
Unrestricted
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Algorithm:start.
2.enter a number.
6.stop.
Source code:
#include <stdio.h>
int main()
{
int a, b, result;
int prime[100];
Unrestricted
int lcm(int a, int b)
{
static int common = 1;
Output:
Algorithm:start.
6.stop.
Source code:
#include <stdio.h>
Unrestricted
int product(int, int);
int main()
{
int a, b, result;
Output:
Unrestricted
Aim:To find the power of a number using recursion.
Algorithm:start.
6.stop.
Sourcecode:
#include <stdio.h>
int main()
{
int pow, num;
long result;
Unrestricted
return 1;
}
Output:
Algorithm:start.
7.stop.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
Unrestricted
{
int i, j, k;
char str[100];
char *rev;
printf("Enter the string:\t");
scanf("%s", str);
printf("The original string is: %s\n", str);
rev = reverse(str);
printf("The reversed string is: %s\n", rev);
getch();
}
Unrestricted
Output:
Q.37.write a c program to find the given number is prime or not using recursion.
Algorithm:start.
7.stop.
Source code:
#include <stdio.h>
int main()
{
int num, check;
printf("Enter a number: ");
scanf("%d", &num);
check = primeno(num, num / 2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
Unrestricted
{
printf("%d is not a prime number\n", num);
}
return 0;
}
Output:
Enter a number: 89
89 is a prime number
Unrestricted
Aim:To implement the employee database using array structure.
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
int salary;
} Employee;
int main()
{
int i, n=2;
Employee employees[n];
Unrestricted
scanf("%d",&employees[i].id);
printf("Salary: ");
scanf("%d",&employees[i].salary);
printf("\n");
}
printf("\n");
}
return 0;
Output:
Unrestricted
Q.39.write a c program to implement the student database using arrays of
structure.
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
Unrestricted
} s[10];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output:
Unrestricted
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
.
.
.
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
#include <stdlib.h>
struct course {
int marks;
char subject[30];
Unrestricted
};
int main() {
struct course *ptr;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i)
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
return 0;
}
Output
Displaying Information:
Programming 22
Structure 33
Unrestricted
Aim:To add complex by passing structures to a function.
Algorithm:start.
5.stop.
Source code:
#include <stdio.h>
typedef struct complex {
float real;
float imag;
} complex;
int main() {
complex n1, n2, result;
Unrestricted
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
Output:
Algorithm:start.
5.stop.
Source code:
#include<stdio.h>
struct Distance {
int feet;
Unrestricted
float inch;
} d1, d2, sumOfDistances;
int main() {
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet=d1.feet+d2.feet;
sumOfDistances.inch=d1.inch+d2.inch;
Output:
Unrestricted
Enter inch: 2.4
Algorithm:start.
5.stop.
Source code
# include < stdio.h >
# include < conio.h >
void creation( ) ;
void deposit( ) ;
void withdraw( ) ;
void lowbal( ) ;
int a = 0 , i = 1001 ;
struct bank
{
int no ;
char name[20] ;
float bal ;
float dep ;
} s[100];
Unrestricted
int main( )
{
int ch ;
do
{
printf(" \n**************************** ") ;
printf(" \nBANKING ") ;
printf(" \n**************************** ") ;
printf(" \n1. Create New Account ") ;
printf(" \n2. Cash Deposit ") ;
printf(" \n3. Cash Withdraw ") ;
printf(" \n4. Low Balance Enquiry ") ;
printf(" \n5. Exit ") ;
printf(" \nEnter your choice : ") ;
scanf("%d ",& ch) ;
switch ( ch)
{
case 1 : creation( ) ;
break ;
case 2 : deposit( ) ;
break ;
case 3 : withdraw( ) ;
break ;
case 4 : lowbal( ) ;
break ;
case 5 : ;
break ;
defalut : printf(" Choice a Valid option !! ") ;
break ;
getch( ) ;
}
} while( ch != 5 ) ;
}
void creation( )
{
printf(" \n**************************** ") ;
printf(" \nNEW ACCOUNT CREATION ") ;
Unrestricted
printf(" \n**************************** ") ;
printf(" \nYour Account Number is :%d ",i) ;
s[a].no = i ;
printf(" \nEnter your Name : ") ;
scanf("%s ",& s[a].name) ;
printf(" \nYour Deposit is Minimum Rs.500") ;
s[a].dep=500 ;a++ ;
i++ ;
getch( ) ;
}
void deposit( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH DEPOSIT ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nDeposited Amount : ") ;
scanf("%f ",& &aa) ;
s[m].dep+=aa ;
printf("\nThe Balance in Account is : %f ",s[m].dep) ;
getch( ) ;
}
else
{
Unrestricted
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
}
void withdraw( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH WITHDRAW ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nWithdraw Amount : ") ;
scanf("%f ",& aa) ;
if ( s[m].dep < aa+500)
{
printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM BALANCE ") ;
getch( ) ;
}
else
{
s[m].dep-=aa ;
printf("\nThe Balance Amount in Account is : %f ",s[m].dep) ;
}
Unrestricted
}
else
{
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
getch( ) ;
}
void lowbal( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nFOLLOWING ACCOUNT HOLDER'S BALANCE IS LESS THAN 1000 ") ;
printf(" \n**************************** ") ;
for ( b = 0 ; b < a ; b++)
{
if ( s[b].dep < 1000))
{
printf("\n\n Account Number : %d ",s[b].no) ;
printf("\n Name : %s ",s[b].name) ;
}
}
getch( ) ;
}
Unrestricted
Output of Program:
Algorithm:start.
Unrestricted
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
Output:
Unrestricted
Q.45. write a c program to delete a specific line from a text file.
Algorithm:start.
Source code:
#include <stdio.h>
int main()
{
FILE *fileptr1, *fileptr2;
char filename[40];
char ch; int delete_line, temp = 1;
Unrestricted
while (ch != EOF)
{
ch = getc(fileptr1);
if (ch == '\n')
temp++;
if (temp != delete_line)
{
putc(ch, fileptr2);
}
}
fclose(fileptr1);
fclose(fileptr2);
remove(filename);
rename("replica.c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
while (ch != EOF)
{
printf("%c", ch);
ch = getc(fileptr1);
}
fclose(fileptr1);
return 0;
}
Output:
Unrestricted
#include<stdio.h>
int main()
{
long int decimalnum, remainder, quotient;
int i = 1, j, temp;
quotient = decimalnum;
while (quotient != 0)
{
temp = quotient % 16;
//To convert integer into character
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
hexadecimalnum[i++] = temp;
quotient = quotient / 16;
}
1. Algorithm:start.
2. FILE *fileptr;
Unrestricted
3. int count_lines = 0;
4. char filechar[40], chr;
5. printf("Enter file name: ");
6. scanf("%s", filechar);
7. fileptr = fopen(filechar, "r");
Source code:
#include <stdio.h>
int main()
{
FILE *fileptr;
int count_lines = 0;
char filechar[40], chr;
chr = getc(fileptr);
while (chr != EOF)
{
if (chr == 'n')
{
count_lines = count_lines + 1;
}
chr = getc(fileptr);
}
fclose(fileptr); //close file.
printf("There are %d lines in %s in a file\n", count_lines, filechar);
return 0;
}
Unrestricted
Output:
Q.47.write a c program to find the size of file using file handling function.
Algorithm:
Source code:
#include <stdio.h>
fp = fopen(argv[1], "r");
if (fp == NULL)
printf("\nFile unable to open ");
else
printf("\nFile opened ");
fseek(fp, 0, 2);
Unrestricted
size = ftell(fp);
printf("The size of given file is : %d\n", size);
fclose(fp);
}
Output:
File opened The size of given file is : 3791744
Algorithm:
* Position where they Differ.
void compare_two_binary_files(FILE *,FILE *);
int main(int argc, char *argv[])
FILE *fp1, *fp2;
if (argc < 3)
printf("\nInsufficient Arguments: \n");
Source code:
1. #include <stdio.h>
2.
3. void compare_two_binary_files(FILE *,FILE *);
4.
5. int main(int argc, char *argv[])
6. {
7. FILE *fp1, *fp2;
8.
9. if (argc < 3)
10. {
11. printf("\nInsufficient Arguments: \n");
12. printf("\nHelp:./executable <filename1> <filename2>\n");
Unrestricted
13. return;
14. }
15. else
16. {
17. fp1 = fopen(argv[1], "r");
18. if (fp1 == NULL)
19. {
20. printf("\nError in opening file %s", argv[1]);
21. return;
22. }
23.
24. fp2 = fopen(argv[2], "r");
25.
26. if (fp2 == NULL)
27. {
28. printf("\nError in opening file %s", argv[2]);
29. return;
30. }
31.
32. if ((fp1 != NULL) && (fp2 != NULL))
33. {
34. compare_two_binary_files(fp1, fp2);
35. }
36. }
37.}
38.
39./*
40. * compare two binary files character by character
41. */
42.void compare_two_binary_files(FILE *fp1, FILE *fp2)
43.{
44. char ch1, ch2;
45. int flag = 0;
46.
47. while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
48. {
49. /*
Unrestricted
50. * character by character comparision
51. * if equal then continue by comparing till the end of files
52. */
53. if (ch1 == ch2)
54. {
55. flag = 1;
56. continue;
57. }
58. /*
59. * If not equal then returns the byte position
60. */
61. else
62. {
63. fseek(fp1, -1, SEEK_CUR);
64. flag = 0;
}
65. }
66.
67. if (flag == 0)
68. {
69. printf("Two files are not equal : byte poistion at which two files
differ is %d\n", ftell(fp1)+1);
70. }
71. else
72. {
73. printf("Two files are Equal\n ", ftell(fp1)+1);
74. }
75.}
Output:
$ gcc file15.c
$ a.out /bin/chgrp /bin/chown
Two files are not equal : byte poistion at which two files differ is 25
/*
* Verify using cmp command
Unrestricted
*/
$ cmp /bin/chgrp /bin/chown
/bin/chgrp /bin/chown differ: byte 25, line 1
Algorithm:
Source code:
1. #include <stdio.h>
2.
3. int to_upper_file(FILE *);
4.
5. int main(int argc,char *argv[])
6. {
Unrestricted
7. FILE *fp;
8. int status;
9.
10. if (argc == 1)
11. {
12. printf("Insufficient Arguments:");
13. printf("No File name is provided at command line");
14. return;
15. }
16. if (argc > 1)
17. {
18. fp = fopen(argv[1],"r+");
19. status = to_upper_file(fp);
20.
21. /*
22. *If the status returned is 0 then the coversion of file content was
completed successfully
23. */
24.
25. if (status == 0)
26. {
27. printf("\n The content of \"%s\" file was successfully converted to
upper case\n",argv[1]);
28. return;
29. }
30. /*
31. * If the status returnes is -1 then the conversion of file content was
not done
32. */
33. if (status == -1)
34. {
35. printf("\n Failed to convert");
36. return;
37. }
38. }
39.}
40.
Unrestricted
41./*
42. * convert the file content to uppercase
43. */
44.int to_upper_file(FILE *fp)
45.{
46. char ch;
47.
48. if (fp == NULL)
49. {
50. perror("Unable to open file");
51. return -1;
52. }
53. else
54. {
55. /*
56. * Read the file content and convert to uppercase
57. */
58. while (ch != EOF)
59. {
60. ch = fgetc(fp);
61. if ((ch >= 'a') && (ch <= 'z'))
62. {
63. ch = ch - 32;
64. fseek(fp,-1,SEEK_CUR);
65. fputc(ch,fp);
66. }
67. }
68. return 0;
69. }
70.
}
Output:
This is Manish
I had worked in Wipro and Cisco
The content of "mydata" file was successfully converted to upper case
THIS IS MANISH
Unrestricted
I HAD WORKED IN WIPRO AND CISCO
Algorithm:
1. #include <errno.h>
2. long count_characters(FILE *);
3. void main(int argc, char * argv[])
4. int i;
5. long cnt;
6. char ch, ch1;
7. FILE *fp1, *fp2;
Source code:
1. #include <stdio.h>
2. #include <errno.h>
3.
4. long count_characters(FILE *);
5.
6. void main(int argc, char * argv[])
7. {
8. int i;
9. long cnt;
10. char ch, ch1;
11. FILE *fp1, *fp2;
12.
13. if (fp1 = fopen(argv[1], "r"))
14. {
15. printf("The FILE has been opened...\n");
16. fp2 = fopen(argv[2], "w");
Unrestricted
17. cnt = count_characters(fp1); fseek(fp1, -1L, 2);
18. printf("Number of characters to be copied %d\n", ftell(fp1));
19.
20. while (cnt)
21. {
22. ch = fgetc(fp1);
23. fputc(ch, fp2);
24. fseek(fp1, -2L, 1);
25. cnt--;
26. }
27. printf("\n**File copied successfully in reverse order**\n");
28. }
29. else
30. {
31. perror("Error occured\n");
32. }
33. fclose(fp1);
34. fclose(fp2);
35.}
36.long count_characters(FILE *f)
37.{
38. fseek(f, -1L, 2);
39. long last_pos = ftell(f); last_pos++;
40. return last_pos;
41.}
Output:
Unrestricted
.noitcnuf RORRERTS eht ot llaC TNEUQESBUS a yb nettirwrevo ebyam dna
ELBAIFIDOM ton si GNIRTS EHT
.denifed noitatnemelpmi era stnetnoc esohw GNIRTS GSM RORRE na ot retniop a
snruter RORRERTS noitcnuf ehT
The FILE has been opened..
Number of characters to be copied 203
Unrestricted