0% found this document useful (1 vote)
11 views2 pages

Essential C Scripts for Beginners

The document contains several sample C programs demonstrating basic programming concepts. It includes a 'Hello World' program, a program to sum two numbers, a recursive function to calculate factorials, a prime number checker, and an example of file handling. Each program is complete with input prompts and output statements.

Uploaded by

thailandjunior98
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
0% found this document useful (1 vote)
11 views2 pages

Essential C Scripts for Beginners

The document contains several sample C programs demonstrating basic programming concepts. It includes a 'Hello World' program, a program to sum two numbers, a recursive function to calculate factorials, a prime number checker, and an example of file handling. Each program is complete with input prompts and output statements.

Uploaded by

thailandjunior98
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

Sample C Scripts

Hello World Program


#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

Sum of Two Numbers


#include <stdio.h>

int main() {
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}

Factorial using Recursion


#include <stdio.h>

int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}

Check Prime Number


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

if (num <= 1) flag = 1;


for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}

File Handling Example


#include <stdio.h>

int main() {
FILE *fp;
fp = fopen("[Link]", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp, "Hello, file!\n");
fclose(fp);
printf("Data written to file successfully.\n");
return 0;
}

You might also like