[go: up one dir, main page]

0% found this document useful (0 votes)
17 views7 pages

Codes

The document contains several C programs demonstrating different functionalities: calculating large factorials using arrays, basic file handling for reading and writing, and combining both to write factorial results to a file. It also includes a program to count vowels in a text file. Each program is structured to handle specific tasks efficiently.

Uploaded by

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

Codes

The document contains several C programs demonstrating different functionalities: calculating large factorials using arrays, basic file handling for reading and writing, and combining both to write factorial results to a file. It also includes a program to count vowels in a text file. Each program is structured to handle specific tasks efficiently.

Uploaded by

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

1️⃣ Large factorial using array

#include <stdio.h>

int main() {
int n = 20;
int result[1000];
int size = 1;
result[0] = 1;

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


int carry = 0;
for (int j = 0; j < size; j++) {
int prod = result[j] * i + carry;
result[j] = prod % 10;
carry = prod / 10;
}
while (carry) {
result[size] = carry % 10;
carry /= 10;
size++;
}
}
printf("%d! = ", n);
for (int i = size - 1; i >= 0; i--) {
printf("%d", result[i]);
}
printf("\n");
return 0;
}

2️⃣ File handling basics

#include <stdio.h>

int main() {
FILE *fp;

// Write to file
fp = fopen("data.txt", "w");
if (!fp) { perror("fopen"); return 1; }
fprintf(fp, "Hello Exam!\n");
fclose(fp);

// Read from file


fp = fopen("data.txt", "r");
if (!fp) { perror("fopen"); return 1; }
char line[100];
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
return 0;
}

3️⃣ Combine factorial + file handling

#include <stdio.h>

int main() {
int n = 20;
int result[1000], size = 1;
result[0] = 1;

// Calculate factorial
for (int i = 2; i <= n; i++) {
int carry = 0;
for (int j = 0; j < size; j++) {
int prod = result[j] * i + carry;
result[j] = prod % 10;
carry = prod / 10;
}
while (carry) {
result[size] = carry % 10;
carry /= 10;
size++;
}
}

// Write factorial to file


FILE *fp = fopen("factorial.txt", "w");
if (!fp) { perror("fopen"); return 1; }
fprintf(fp, "%d! = ", n);
for (int i = size - 1; i >= 0; i--) {
fprintf(fp, "%d", result[i]);
}
fprintf(fp, "\n");
fclose(fp);

// Read back from file


fp = fopen("factorial.txt", "r");
if (!fp) { perror("fopen"); return 1; }
char ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);

return 0;
}

4 Factorial of Large Number using Array + File Handling

#include <stdio.h>

int main() {
int n = 20, res[1000], size = 1;
res[0] = 1;
for (int i = 2; i <= n; i++) {
int carry = 0;
for (int j = 0; j < size; j++) {
int prod = res[j] * i + carry;
res[j] = prod % 10;
carry = prod / 10;
}
while (carry) { res[size++] = carry % 10; carry /= 10; }
}
FILE *fp = fopen("fact.txt", "w");
for (int i = size - 1; i >= 0; i--) fprintf(fp, "%d", res[i]);
fclose(fp);
return 0;
}

5. Count Vowels in a File

#include <stdio.h>
#include <ctype.h>

int main() {
FILE *fp = fopen("input.txt", "r");
if (!fp) return 1;
char ch; int count = 0;
while ((ch = fgetc(fp)) != EOF) {
ch = tolower(ch);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') count++;
}
fclose(fp);
printf("Vowel count: %d\n", count);
return 0;
}

You might also like