100% found this document useful (1 vote)
102 views87 pages

C Lab Manual Record CSE18R153: Submitted By: T.bindamrutha. 9919004275. A9

The document contains solutions to 13 questions on C programming concepts using functions. The questions cover topics like finding the factorial, square root, largest/smallest number, prime checking, sum/average of numbers, power, digit sum, and sorting using pointers and dynamic memory allocation. For each question, the algorithm, source code and sample output are provided. The solutions demonstrate the use of various functions to solve common mathematical and array/number problems in C programming.

Uploaded by

John
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
100% found this document useful (1 vote)
102 views87 pages

C Lab Manual Record CSE18R153: Submitted By: T.bindamrutha. 9919004275. A9

The document contains solutions to 13 questions on C programming concepts using functions. The questions cover topics like finding the factorial, square root, largest/smallest number, prime checking, sum/average of numbers, power, digit sum, and sorting using pointers and dynamic memory allocation. For each question, the algorithm, source code and sample output are provided. The solutions demonstrate the use of various functions to solve common mathematical and array/number problems in C programming.

Uploaded by

John
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/ 87

C Lab Manual

Record
CSE18R153

Submitted by:

T.bindamrutha.

9919004275.

A9

Unrestricted
Q.1 write a c program to find factorial of a number using functions.

Aim: To find factorial of a number using functions

1. Algorithm: input a number n.


2. set variable final as 1.
3. final <= final * n.
4. decrease n.
5. check if n is equal to 0.
6. if n is equal to zero, goto step 8 (break out of loop)
7. else goto step 3.
8. Stop.

Source code:
#include <stdio.h>

int fact(int);

void main()
{
int no,factorial;

printf("Enter a number to calculate it's factorial\n");


scanf("%d",&no);
factorial=fact(no);
printf("Factorial of the num(%d) = %d\n",no,factorial);
}

int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;

Unrestricted
}
return f;
}

Output:enter a number to calculate its factorial


5
Factorial of the num(5)=120.

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

2.use if else statement

3.find the biggest and smallest numbers from the given numbers

4.print the numbers.

5.stop

Source code:

#include <stdio.h>

int max(int num1, int num2);


int min(int num1, int num2);

int main()
{
int num1, num2, maximum, minimum;

printf("Enter any two numbers: ");


scanf("%d%d", &num1, &num2);

Unrestricted
maximum = max(num1, num2);
minimum = min(num1, num2);

printf("\nMaximum = %d\n", maximum);


printf("Minimum = %d", minimum);

return 0;
}

Output:enter any two numbers:10,20


Maximum=20
Minimum=10

Q.3.write a c program to find the square root of a number using functions

Aim: find the square root of a number using functions


Algorithm:start
2.using printf statements
3.sqrt the number
4.print the result
5.stop

Source code:
#include <stdio.h>
#include <math.h>

int main()
{
double num, root;

printf("Enter any number to find square root: ");


scanf("%lf", &num);

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.

Q.4.write a c program to find square of a number using functions.

Aim: to find square of a number using functions


Algorithm:start
2.use the printf statement
3.choose any number.
4.square the number.
5.print the result.
6.stop.

Source code:
#include <stdio.h>

double square(double num)

return (num * num);

int main()

int num;

double n;

Unrestricted
printf("\n\n Function : find square of any number :\n");

printf("------------------------------------------------\n");

printf("Input any number for square : ");

scanf("%d", &num);

n = square(num);

printf("The square of %d is : %.2f\n", num, n);

return 0;

Output:

Input any number for square:12

The square of 12 is :144

Q.5.write a c program to find the cube of a number using function.

Aim: To find the cube of a number using function.

Algorithm:start

2.use the printf statements.

3.enter any number.

4.cube the given number.

5.print the result.

6.stop

Source code:

#include <stdio.h>

Unrestricted
/* Function declaration */
double cube(double num);

int main()
{
int num;
double c;

/* Input number to find cube from user */


printf("Enter any number: ");
scanf("%d", &num);

c = cube(num);

printf("Cube of %d is %.2f", num, c);

return 0;
}

/**
* Function to find cube of any number
*/
double cube(double num)
{
return (num * num * num);
}

Output:

Enter any number:5

Cube of 5 is:125

Q.6.write a c program to find the given number is prime or not using function.

Aim: To find the given number is prime or not using function.

Algorithm:start

Unrestricted
2.enter a number.

3.use if else statements.

4.check whether the is prime or not.

5.print the result.

6.stop

Source code:

#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

// condition for non-prime


if (n % i == 0) {
flag = 1;
break;
}
}

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.

Q.7.write a c program to find the sum of natural numbers using functions.

Aim: To find the sum of natural numbers using functions.

Algorithm:start

2.enter some numbers.

3.and add the numbers.

4.print the result.

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.

Aim: To find the sum and average of n numbers using functions.


Algorithm:start
2.enter some numbers.
3.first add the numbers.
4.after avg the numbers.
5.print the result.
6.stop.

Source code:

#include<stdio.h>

void main()
{
int i,n,Sum=0,numbers;
float Average;

printf("\nPlease Enter How many Number you want?\n");


scanf("%d",&n);

printf("\nPlease Enter the elements one by one\n");


for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}

Average = Sum/n;

printf("\nSum of the %d Numbers = %d",n, Sum);

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

Q.9.write a c program to find the power of a number using function.

Aim: To find the power of a number using function


Algorithm:start
2.enter the base number.
3.enter the number for exponent.
4.calculate the power.
5.print the result.
6.stop.

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
}

void calculate_power(int b,int e)


{
int power=1;
while(e>0)
{
power=power*b;
e--;
}
printf("The power of the no = %d",power);
}

Output:
Enter the base
4
Enter the exponent
3
The power of the number =64

Q.10.write a c program to find the sum of digit using function.

Aim: To find the sum of digit using function.


Algorithm:start.
2.enter a number.
3.after add the digits of a number.
4.print the result.
5.stop.

Source code:
#include <stdio.h>

int sum (int a);

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

int sum (int num)


{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
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 i,j,temp,tempmin, max,min;

max = temp = tempmin = 0;

int arr[10];

min = arr[0];

printf("Enter up to 10 numbers:\n");

for(i=0;i<=10;i++)
{

scanf("%d",&arr[i]);

int *ptr1, *ptr2;

ptr1 = arr;

ptr2 = arr;

for(i=1;i<=10;i++)
{

if(max <= *ptr1)


{

temp = max;

max = *ptr1;

Unrestricted
*ptr1 = temp;

ptr1++;

printf("MAX=%d\n",max);

// finding minimum

for(j=1;j<=10;j++) {

if(min >= *ptr2)


{

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

Q.12.write a c program to sort N numbers using dynamic memory allocation.

Aim:To sort N numbers using dynamic memory allocation.

Algorithm:start

2.enter the numbers which are to be sorted.

3.enter the numbers in random order.

4.by sorting enter the numbers in ascending order.

5.print the result .

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:

Enter the integers to be sorted in ascending order:5

Unrestricted
Enter the 5 numbers to be sorted in ascending order one by one:56

35

567

28

After sorting in ascending order:

28

35

56

567

Q.13.write a c program using memory handling functions

Aim:c program using memory handling functions.

Algorithm:start.

2.using calloc function.

3.adding the numbers.

4.print the result.

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:

Building and calculating the sequence sum of the first 10 terms


Sum = 45

Q.14.write a c program to add two numbers using pointers.

Aim:to add two numbers using pointers.

Algorithm:start

2.enter two numbers.

3.add the two numbers using pointers.

4.print the result.

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;

printf("Sum of the numbers = %d\n", sum);

return 0;
}

Output:

Enter two integers to add

Sum of entered numbers=13.

Q.15.write a c program to read integers in an array and reverse them using


pointers.

Aim: To read integers in an array and reverse them using pointers.

Algorithm:start.

2.read the integers in an array.

3.after the integers using pointers.

4.print the result.

5.stop.

Source code:

#include<stdio.h>

Unrestricted
#include<conio.h>

#define MAX 30

void main() {

int size, i, arr[MAX];

int *ptr;

clrscr();

ptr = &arr[0];

printf("\nEnter the size of array : ");

scanf("%d", &size);

printf("\nEnter %d integers into array: ", size);

for (i = 0; i < size; i++) {

scanf("%d", ptr);

ptr++;

ptr = &arr[size - 1];

printf("\nElements of array in reverse order are :");

Unrestricted
for (i = size - 1; i >= 0; i--) {

printf("\nElement%d is %d : ", i, *ptr);

ptr--;

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 4 is : 44

Element 4 is : 33

Element 4 is : 22

Element 4 is : 11

Q.16.write a c program to swap two numbers using pointers.

Aim: To swap two numbers using pointers.

Algorithm:start.

2.enter two numbers.

Unrestricted
3.swap two numbers using pointers.

4.print the result.

5.stop.

Source code:

#include <stdio.h>

void swap(int *x,int *y)


{
int t;
t = *x;
*x = *y;
*y = t;
}

int main()
{
int num1,num2;

printf("Enter value of num1: ");


scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);

printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

swap(&num1,&num2);

printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

return 0;
}

Unrestricted
Output:’

Enter value of num1=10

Enter value of num2=20

Before swapping: num1: 10,num2: 20

After swapping: num1: 20,num2:10

Q.17.write a c program to count vowels in the string using pointers.

Aim:To count vowels in the string using pointers.

Algorithm:start

2.enter a string.

3.count the vowels in the string.

4.print the result.

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:

Pointer Example Program : Count Vowels In String

Enter Any string in small [below 20 chars] : pointer

Length of String : 7
Vowels Count In the String : 3
Constant Count in the String : 4

Q.18.write a c program to print value and address of a variable using pointer.

Aim: To print value and address of a variable using pointer.

Algorithm:start.

2.know the address and value of a variable.

3.use the pointers.

Unrestricted
4.print the result.

5.stop.

Source code:

# include < stdio.h >


int main( )
{
int a ;
int *p ;
printf(" Enter any integer: ") ;
scanf("%d ",& a) ;
p = &a ;

printf("\n Value of Integer : %d ",a) ;


printf("\n Value of Integer : %d ",*p) ;
printf("\n Value of Integer : %d ",*(&a)) ;
printf("\n Address of Integer : %u ",p) ;
printf("\n Address of Integer : %u ",&a) ;
return ( 0 );
}
Output:
Enter any integer:25
Value of integer:25
Value of integer:25
Value of integer:25
Address of integer:10485316
Address of integer:10485316

Q.19.write a c program to compute sum of array elements using pointers.


Aim:To compute sum of array elements using pointers.
Algorithm:start.
2.enter the size of array.

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;

printf("Enter the size of array A \n");


scanf("%d", &n);

a = (int *) malloc(n * sizeof(int));

printf("Enter Elements of the List \n");


for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}

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


{
sum = sum + *(a + i);
}

printf("Sum of all elements in array = %d\n", sum);


return 0;
}

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

Q.20.write a c program to accesing value from address using pointers.

Aim:To accesing value from address using pointers.

Algorithm:start.

2.give the number.

3.print the numbers using pointers.

4.print the result.

5.stop.

Source code:

#include <stdio.h>

int main(void)
{

int num = 100;

int *ptr;

ptr = &num;

Unrestricted
printf("value of num = %d\n", *ptr);

return 0;
}

Output:

Value of num=100

Q.21.write a c program to read and print one dimensional of integer elements.

Aim:To read and print one dimensional of integer elements.

Algorithm:start

2.enter the elements in an array.

3.use the for loops.

4.read and print the elements.

5.print the result.

6.stop.

Source code:

#include <stdio.h>

void readArray(int arr[], int size)


{
int i =0;

printf("\nEnter elements : \n");

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


{
printf("Enter arr[%d] : ",i);

Unrestricted
scanf("%d",&arr[i]);
}
}

void printArray(int arr[],int size)


{
int i =0;

printf("\nElements are : ");

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


{
printf("\n\tarr[%d] : %d",i,arr[i]);
}
printf("\n");
}

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

Q.22.write a c program to find the average of n numbers using array.

Aim: To find the average of n numbers using array.

Algorithm:start

2.write a array of elements.

3.add the numbers.

4.after adding average the number.

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

while (n > 100 || n < 1) {


printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}

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


printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}

avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

Output:

Enter the numbers of elements: 6


1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

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.

2.enter the array of elements.

3.find the occurrence of given number in an array.

4.print the result.

5.stop.

Source code:

#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i;
int num,count;

printf("Enter total number of elements: ");


scanf("%d",&n);

//read array elements


printf("Enter array elements:\n");
for(i=0;i< n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}

printf("Enter number to find Occurrence: ");


scanf("%d",&num);

//count occurance of num


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

Unrestricted
if(arr[i]==num)
count++;
}
printf("Occurrence of %d is: %d\n",num,count);
return 0;
}

Output:

Enter total number of elements: 5


Enter array elements:
Enter element 1: 10
Enter element 2: 10
Enter element 3: 20
Enter element 4: 30
Enter element 5: 10
Enter number to find Occurrence: 10
Occurrence of 10 is: 3

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.

2.enter one array of elements.

3.enter another array of elements.

4.merge two arrays into third array.

5.print the result.

6.stop.

Source code:

Unrestricted
#include <conio.h>
print(int *a,int n)
{
int i;

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


{

printf("%d ",a[i]);

int main()
{
int a[10000],b[10000],c[20000],i,n1,n2 ;

printf("Enter size of the 1st array : ");


scanf("%d", &n1);
printf("Enter elements in array : ");
for(i=0; i<n1; i++)
{
scanf("%d",&a[i]);
}
printf("Enter size of the 2nd array : ");
scanf("%d",&n2);

printf("Enter elements in array : ");


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

Unrestricted
for(i=0; i<n1+n2; i++)
{
if(i<n1)
c[i]=a[i];
else
c[i]=b[i-n1];

printf(" 1st array :\n");

print(a,n1);

printf("\n2nd array :\n");

print(b,n2);

printf("\n3rd array :\n");

print(c,n1+n2);

return 0;
}

Output:

1 Enter size of the 1st array: 5


2 Enter elements in array: 1
3 2
4 3
5 4
6 5
7 Enter size of the 2nd array: 5
8 Enter elements in array: 1
9 2
10 3

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.

2.enter an array of elements.

3.use the for loops.

4.search for one element and its position.

5.print the element and its position.

6.stop.

Source code:

#include<stdio.h>

void main()

int i,n,a[100],search,flag=0;

printf("Enter the number of elements:\n") ;

Unrestricted
scanf("%d",&n) ;

printf("Enter the elements\n") ;

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

scanf("%d",&a[i]) ;

printf("Enter the element to be seached\n");

scanf("%d",&search);

/*Perform search operation*/

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

if(a[i]==search)

printf("Element %d found at %d position\n",search,i);

flag=1;

break;

if(flag==0)

Unrestricted
{

printf("Element %d not found\n",search);

Output:

Enter the number of elements:

Enter the elements

7 8 12 3

Enter the elements to be searched

Element 8 found at 1 position.

Q.26.write a c program to sort the array in ascending order.

Aim:To sort the array in ascending order.

Algorithm:start.

2.enter the elements in an array.

3.sort the elements.

4.print the result.

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

printf("Enter the numbers \n");


for (i = 0; i < n; ++i)
scanf("%d", &number[i]);

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


{

for (j = i + 1; j < n; ++j)


{

if (number[i] > number[j])


{

a = number[i];
number[i] = number[j];
number[j] = a;

printf("The numbers arranged in ascending order are given below


\n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);

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

Q.27.write a c program to print the odd and even numbers in an array.

Aim: To print the odd and even numbers in an array.

Algorithm:start.

2.enter the array of elements.

3.find the odd and even numbers.

4.print the result .

5.stop.

Source code:

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

int array[100], i, num;


printf("Enter the size of an array \n");

Unrestricted
scanf("%d", &num);
printf("Enter the elements of the array \n");

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


{
scanf("%d", &array[i]);
}

printf("Even numbers in the array are - ");


for (i = 0; i < num; i++)
{
if (array[i] % 2 == 0)
{
printf("%d \t", array[i]);
}
}

printf("\n Odd numbers in the array are -");


for (i = 0; i < num; i++)
{
if (array[i] % 2 != 0)
{
printf("%d \t", array[i]);
}
}

}
Output:

Enter the size of an array


6
Enter the elements of the array
12
19
45
69
98

Unrestricted
23
Even numbers in the array are - 12 98
Odd numbers in the array are - 19 45 69 23

Q.28.write a c program to find the factorial of a number using recursive function.

Aim:To find the factorial of a number using recursive function.

Algorithm:start.

2.use if else statement.

3.find the factorial of the given number.

4.print the result.

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

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Unrestricted
Output:

Enter a positive integer: 6


Factorial of 6 = 720

Q.29. write a c program to generate Fibonacci series of a given number using


function.

Aim:To generate Fibonacci series of a given number using function.

Algorithm:start.

• Declare variables i, a,b , show


• Initialize the variables, a=0, b=1, and show =0
• Enter the number of terms of Fibonacci series to be printed
• Print First two terms of series
• Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
• End

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;

printf("Fibonacci series for %d terms:-\n",n);

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

printf("%d ",c);

a=b;

b=c;

c=a+b;

Output:

Enter a number to generate a fibanocci series for first n terms

Unrestricted
Fibanocci series for 7 terms :

0112358

Q.30.write a c program to compute GCD for given numbers using recursive


function.

Aim:To compute GCD for given numbers using recursive function.

Algorithm:start

2.intialize the variables.

3.declare the variables.

4.find the gcd of the given numbers.

5.print the result.

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

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

Unrestricted
Output:

Enter two positive integers: 366


60
G.C.D of 366 and 60 is 6.

Q.31.write a c program to find the sum of n numbers using recursion.

Aim: To find the sum of n numbers using recursion

Algorithm:start.

2.use if else statement.

3.add the numbers.

4.print the result.

5.stop.

Source code:

#include <stdio.h>

void display_sum(int);

int main()
{
int num;

printf("Enter the Nth number: ");


scanf("%d", &num);
display_sum(num);
return 0;
}

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:

Enter the Nth number: 3


Sum of first N numbers is 6

Q.32.write a c program to solve tower of Hanoi program using recursion.

Aim:To solve tower of Hanoi program using recursion.

Algorithm:start.

2.use if else statements.

3.enter the no.of disks.

4.find the tower of program.

5.print the result.

6.stop.

Unrestricted
Source code:

#include <stdio.h>

void towers(int, char, char, char);

int main()
{
int num;

printf("Enter the number of disks : ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are:\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg,
topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

Output:

Enter the number of disks : 3


The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B

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

Q.33.write a c program to find the LCM of a number using recursion.

Aim: To find the LCM of a number using recursion.

Algorithm:start.

2.enter a number.

3.find the lcm of anumber.

4.use the if else statement.

5.print the result.

6.stop.

Source code:

#include <stdio.h>

int lcm(int, int);

int main()
{
int a, b, result;
int prime[100];

printf("Enter two numbers: ");


scanf("%d%d", &a, &b);
result = lcm(a, b);
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}

Unrestricted
int lcm(int a, int b)
{
static int common = 1;

if (common % a == 0 && common % b == 0)


{
return common;
}
common++;
lcm(a, b);
return common;
}

Output:

Enter two numbers: 456


12
The LCM of 456 and 12 is 456

Q.34.write a c program to find the product of number using recursion.

Aim: To find the product of number using recursion.

Algorithm:start.

2.use if else statement .

3.enter the numbers.

4.find the product.

5.print the result.

6.stop.

Source code:

#include <stdio.h>

Unrestricted
int product(int, int);

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

printf("Enter two numbers to find their product: ");


scanf("%d%d", &a, &b);
result = product(a, b);
printf("Product of %d and %d is %d\n", a, b, result);
return 0;
}

int product(int a, int b)


{
if (a < b)
{
return product(b, a);
}
else if (b != 0)
{
return (a + product(a, b - 1));
}
else
{
return 0;
}
}

Output:

Enter two numbers to find their product: 176 340


Product of 176 and 340 is 59840

Q.35.write a c program to find the power of a number using recursion.

Unrestricted
Aim:To find the power of a number using recursion.

Algorithm:start.

2.enter the base number.

3.enter the exponent number.

4.find the power using recursion.

5.print the result.

6.stop.

Sourcecode:

#include <stdio.h>

long power (int, int);

int main()
{
int pow, num;
long result;

printf("Enter a number: ");


scanf("%d", &num);
printf("Enter it's power: ");
scanf("%d", &pow);
result = power(num, pow);
printf("%d^%d is %ld", num, pow, result);
return 0;
}

long power (int num, int pow)


{
if (pow)
{
return (num * power(num, pow - 1));
}

Unrestricted
return 1;
}

Output:

Enter a number: 456


Enter it's power: 3
456^3 is 94818816

Q.36.write a c program to reverse a string using recursion.

Aim:To reverse a string using recursion.

Algorithm:start.

2.intiliaze the variables,I,j,k.

3.declare the variables.

4.enter the string .

5.reverse the string.

6.print the result.

7.stop.

Source code:

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

// declaring recursive function


char* reverse(char* str);

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

// defining the function


char* reverse(char *str)
{
static int i = 0;
static char rev[100];
if(*str)
{
reverse(str+1);
rev[i++] = *str;
}
return rev;
}

Unrestricted
Output:

Enter the string: studytonight


The original string is: studytonight
The reverse string is: thginotyduts

Q.37.write a c program to find the given number is prime or not using recursion.

Aim: To find the given number is prime or not using recursion.

Algorithm:start.

2.intialize the variables.

3.declare the variables.

4.enter the number.

5.check the number is prime or not .

6.print the result.

7.stop.

Source code:

#include <stdio.h>

int primeno(int, int);

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

int primeno(int num, int i)


{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return primeno(num, i - 1);
}
}
}

Output:

Enter a number: 456


456 is not a prime number

Enter a number: 89
89 is a prime number

Q.38.write a c program to implement the employee database using array


structure.

Unrestricted
Aim:To implement the employee database using array structure.

Algorithm:start.

2.use for loops.

3.enter the details.

4.print the result.

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

printf("Enter %d Employee Details \n \n",n);


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

printf("Employee %d:- \n",i+1);


printf("Name: ");
scanf("%s",employees[i].name);
printf("Id: ");

Unrestricted
scanf("%d",&employees[i].id);

printf("Salary: ");
scanf("%d",&employees[i].salary);

printf("\n");
}

printf("-------------- All Employees Details ---------------\n");

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

printf("Name \t: ");


printf("%s \n",employees[i].name);

printf("Id \t: ");


printf("%d \n",employees[i].id);

printf("Salary \t: ");


printf("%d \n",employees[i].salary);

printf("\n");
}

return 0;

Output:

Unrestricted
Q.39.write a c program to implement the student database using arrays of
structure.

Aim:To implement the student database using arrays of structure.

Algorithm:start.

2.use for loops.

3.enter the details.

4.print the result.

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:

Enter information of students:

For roll number1,


Enter name: Tom
Enter marks: 98

Unrestricted
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.
.

Q.40.write a c program to store information using structures with dynamic


memory allocation.

Aim:To store information using structures with dynamic memory allocation.

Algorithm:start.

2.use for loop.

3.use dynamic memory allocation.

4.print the result.

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

// Memory allocation for noOfRecords structures


ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (i = 0; i < noOfRecords; ++i) {
printf("Enter the name of the subject and marks respectively:\n");
scanf("%s %d", &(ptr + i)->subject, &(ptr + i)->marks);
}

printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i)
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);

return 0;
}

Output

Enter the number of records: 2


Enter the name of the subject and marks respectively:
Programming
22
Enter the name of the subject and marks respectively:
Structure
33

Displaying Information:
Programming 22
Structure 33

Q.41.write a c program to add complex by passing structures to a function.

Unrestricted
Aim:To add complex by passing structures to a function.

Algorithm:start.

2.enter two numbers.

3.add the two complex numbers.

4.print the result.

5.stop.

Source code:

#include <stdio.h>
typedef struct complex {
float real;
float imag;
} complex;

complex add(complex n1, complex n2);

int main() {
complex n1, n2, result;

printf("For 1st complex number \n");


printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);

result = add(n1, n2);

printf("Sum = %.1f + %.1fi", result.real, result.imag);


return 0;
}

complex add(complex n1, complex n2) {

Unrestricted
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}

Output:

For 1st complex number


Enter the real and imaginary parts: 2.1
-2.3

For 2nd complex number


Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i

Q.42.write a c program to add two distances (in inch-feet)system using structures.

Aim: To add two distances (in inch-feet)system using structures.

Algorithm:start.

2.enter two distances.

3.add the two distances.

4.print the result.

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;

// If inch is greater than 12, changing it to feet.


if (sumOfDistances.inch>12.0) {
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
printf("\nSum of distances = %d\'-%.1f\"", sumOfDistances.feet,
sumOfDistances.inch);
return 0;
}

Output:

Enter information for 1st distance


Enter feet: 23
Enter inch: 8.6

Enter information for 2nd distance


Enter feet: 34

Unrestricted
Enter inch: 2.4

Sum of distances = 57'-11.0"

Q.43.write a c program to implement bank database using arrays of structures.

Aim: To implement bank database using arrays of structures.

Algorithm:start.

2.use if else statement.

3.enter the details and process.

4.print the result.

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:

Q.44.write a c program to merge the two files in a third file.

Aim: To merge the two files in a third file.

Algorithm:start.

open file1. txt and file2. txt in read mode.


Open file3. txt in write mode.
Run a loop to one by one copy characters of file1. txt to file3. txt.
Run a loop to one by one copy characters of file2. txt to file3. txt.
Close all files.
Stop.

Unrestricted
Source code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");

FILE *fp3 = fopen("file3.txt", "w");


char c;

if (fp1 == NULL || fp2 == NULL || fp3 == NULL)


{
puts("Could not open files");
exit(0);
}

while ((c = fgetc(fp1)) != EOF)


fputc(c, fp3);

while ((c = fgetc(fp2)) != EOF)


fputc(c, fp3);

printf("Merged file1.txt and file2.txt into file3.txt");

fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}

Output:

Merged file1.txt and file2.txt into file3.txt

Unrestricted
Q.45. write a c program to delete a specific line from a text file.

Aim:To delete a specific line from a text file.

Algorithm:start.

1. FILE *fileptr1, *fileptr2;


2. char filename[40];
3. char ch;
4. int delete_line, temp = 1;
5. printf("Enter file name: ");
6. scanf("%s", filename);

Source code:

#include <stdio.h>

int main()
{
FILE *fileptr1, *fileptr2;
char filename[40];
char ch; int delete_line, temp = 1;

printf("Enter file name: ");


scanf("%s", filename);
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
` while (ch != EOF)
{
printf("%c", ch);
ch = getc(fileptr1);
}
rewind(fileptr1);
printf(" \n Enter line number of the line to be deleted:");
scanf("%d", &delete_line);
fileptr2 = fopen("replica.c", "w");
ch = getc(fileptr1);

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:

Enter line number of the line to be deleted: 10

The contents of file after being modified are as follows:


*
* C PROGRAM TO CONVERSION FROM Decimal to hexadecimal
*/

Unrestricted
#include<stdio.h>
int main()
{
long int decimalnum, remainder, quotient;
int i = 1, j, temp;

printf("Enter any decimal number: ");


scanf("%ld", &decimalnum);

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

printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum);


for (j = i - 1; j > 0; j--)
printf("%c", hexadecimalnum[j]);
return 0;
}

Q.46.write a c program to find the number of lines in a text file.

Aim: to find the number of lines in a text file.

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;

printf("Enter file name: ");


scanf("%s", filechar);
fileptr = fopen(filechar, "r");

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:

Enter file name: pgm2.c


There are 43 lines in pgm2.c in a file

Q.47.write a c program to find the size of file using file handling function.

Aim:To find the size of file using file handling function.

Algorithm:

void main(int argc, char **argv)


FILE *fp;
char ch;
int size = 0;
fp = fopen(argv[1], "r");
if (fp == NULL)
printf("\nFile unable to open ");

Source code:

#include <stdio.h>

void main(int argc, char **argv)


{
FILE *fp;
char ch;
int size = 0;

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

Q.48.write a c program to compare two binary files.

Aim:to compare two binary files.

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

$ a.out a.out a.out


Two files are Equal
/*
* Verify using cmp command
*/
$ cmp a.out a.out

Q.49.write a c program to convert the content of file to uppercase.

Aim:to convert the content of file to uppercase.

Algorithm:

1. int to_upper_file(FILE *);


2. int main(int argc,char *argv[])
3. FILE *fp;
4. int status;
5. if (argc == 1)
6. printf("Insufficient Arguments:");
7. printf("No File name is provided at command line");

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

Q.50.write a c program to reverse the contents of a file and print it.

Aim:To reverse the contents of a file and print it.

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:

The function STRERROR returns a pointer to an ERROR MSG STRING whose


contents are implementation defined.
THE STRING is not MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to
the STRERROR function.
The FILE has been opened..
Number of characters to be copied 203

**File copied successfully in reverse order**

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

**File copied successfully in reverse order**

Unrestricted

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy