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

Program 5

c program slip answer

Uploaded by

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

Program 5

c program slip answer

Uploaded by

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

#include <stdio.

h>

#include <stdlib.h>

int main()

int num1, num2, sum;

FILE *fptr1, *fptr2;

// Accept first number from user and write to a.txt

fptr1 = fopen("a.txt", "w");

if (fptr1 == NULL)

printf("Error opening file a.txt!");

exit(1);

printf("Enter first number: ");

scanf("%d", &num1);

fprintf(fptr1, "%d", num1);

fclose(fptr1);

// Accept second number from user and write to b.txt

fptr2 = fopen("b.txt", "w");

if (fptr2 == NULL)

printf("Error opening file b.txt!");

exit(1);

printf("Enter second number: ");

scanf("%d", &num2);
fprintf(fptr2, "%d", num2);

fclose(fptr2);

// Read numbers from files and calculate sum

fptr1 = fopen("a.txt", "r");

fptr2 = fopen("b.txt", "r");

if (fptr1 == NULL || fptr2 == NULL)

printf("Error opening files!");

exit(1);

fscanf(fptr1, "%d", &num1);

fscanf(fptr2, "%d", &num2);

fclose(fptr1);

fclose(fptr2);

// Calculate the sum

sum = num1 + num2;

// Print the result

printf("The sum of %d and %d is %d\n", num1, num2, sum);

return 0;

You might also like