0% found this document useful (0 votes)
4 views

Lab 2

Uploaded by

22052659
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 2

Uploaded by

22052659
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab – 2

Name-Rahul Churiwal
Roll No. -22052659
Sec-CSE_40
2.1 Aim of the program: Write a program in C to convert the first ‘n’ decimal
numbers of a disc file to binary using recursion. Store the binary value in a separate
disc file.
Note# Read the value of ‘n’, source file name and destination file name from
command line arguments. Display the decimal numbers and their equivalent binary
numbers from the output file.
Give the contents of the input disc file “inDec.dat” as 30 75 2564 ...
Contents of the output disc file “outBin.dat” as
The binary equivalent of 30 is 0000000000011110
The binary equivalent of 75 is 0000000001001011
The binary equivalent of 2564 is 0000101000000100
Terminal Input:
$gcc lab2q1.c -o lab2q1
$./lab2q1 150 inDec.dat outBin.dat
Output: Content of the first ‘n’ decimal and their equivalent binary numbers
#include <stdio.h>

int dectobinary(int n)
{
if (n == 0)
return 0;
else
return (n % 2 + 10 * dectobinary(n / 2));
}
int main()
{
FILE *file, *file1;
int num;
file = fopen("a.txt", "r");
file1 = fopen("b.txt", "w");
while (fscanf(file, "%d", &num) != EOF)
{
int n = dectobinary(num);
fprintf(file1, "%d \n", n);
}
fclose(file);
}

Output:

2.2 Aim of the program: Write a program in C to find GCD of two numbers using
recursion.
Read all pair of numbers from a file and store the result in a separate file.
Note# Source file name and destination file name taken from command line
arguments. The source file must contain at least 20 pairs of numbers.
Give the contents of the input disc file “inGcd.dat” as 8 12 20 45 30 80
Contents of the output disc file “outGcd.dat” as
The GCD of 8 and 12 is 4
The GCD of 20 and 45 is 5
The GCD of 30 and 80 is 10
Terminal Input:
$gcc lab2q2.c -o lab2q2
$./lab2q2 inGcd.dat outGcd.dat
Output: Display the gcd stored in the output file outGcd.dat
#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b)


{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main(int argc, char *argv[])
{
FILE *source, *destination;
int number;
int count = 0;
int upperlimit = atoi(argv[1]);
int a[100];
source = fopen(argv[2], "r");
destination = fopen(argv[3], "w");
while (fscanf(source, "%d", &number) != EOF && count <
upperlimit * 2)
{
a[count] = number;
count++;
}
for (int i = 0; i < count; i = i + 2)
{
printf("%d\n", gcd(a[i], a[i + 1]));
fprintf(destination, "%d \n", gcd(a[i], a[i + 1]));
}
fclose(source);
fclose(destination);
}

Output:

You might also like