Programs with Their Outputs
Programs with Their Outputs
Program
#include<stdio.h>
#include<conio.h>
void main() {
clrscr();
printf("Hello World");
getch();
}
Output
Hello World
Program
#include<stdio.h>
#include<conio.h>
void main() {
int a, b, sum;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
float principal, rate, time, si;
clrscr();
printf("Enter principal, rate, and time: ");
scanf("%f %f %f", &principal, &rate, &time);
si = (principal * rate * time) / 100;
printf("Simple Interest = %.2f", si);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
float radius, area;
clrscr();
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of Circle = %.2f", area);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
float celsius, fahrenheit;
clrscr();
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit = %.2f", fahrenheit);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
float sub1, sub2, sub3, sub4, sub5, total, percentage;
clrscr();
printf("Enter marks of five subjects: ");
scanf("%f %f %f %f %f", &sub1, &sub2, &sub3, &sub4, &sub5);
total = sub1 + sub2 + sub3 + sub4 + sub5;
percentage = (total / 500) * 100;
printf("Total = %.2f\n", total);
printf("Percentage = %.2f%%", percentage);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
char ch;
clrscr();
printf("Enter a character: ");
scanf("%c", &ch);
printf("ASCII value of %c = %d", ch, ch);
getch();
}
Example Input/Output
Enter a character: A
ASCII value of A = 65
Program
#include<stdio.h>
#include<conio.h>
void main() {
float length, breadth, perimeter;
clrscr();
printf("Enter length and breadth of the rectangle: ");
scanf("%f %f", &length, &breadth);
perimeter = 2 * (length + breadth);
printf("Perimeter of Rectangle = %.2f", perimeter);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, reverse = 0, remainder;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed Number = %d", reverse);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
getch();
}
Example Input/Output
Enter a number: 40
The number is divisible by both 5 and 8.
Enter a number: 20
The number is not divisible by both 5 and 8.
Program
#include<stdio.h>
#include<conio.h>
void main() {
char ch;
clrscr();
printf("Enter a character: ");
scanf("%c", &ch);
getch();
}
Example Input/Output
Enter a character: A
A is a vowel.
Enter a character: B
B is a consonant.
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.");
} else {
printf("The number is odd.");
}
getch();
}
Example Input/Output
Enter a number: 6
The number is even.
Enter a number: 7
The number is odd.
4. Find the largest number among three numbers
Program
#include<stdio.h>
#include<conio.h>
void main() {
int a, b, c;
clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int a, b, c;
clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a <= b) {
if (a <= c) {
printf("The smallest number is %d", a);
} else {
printf("The smallest number is %d", c);
}
} else {
if (b <= c) {
printf("The smallest number is %d", b);
} else {
printf("The smallest number is %d", c);
}
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
double base, exponent, result;
clrscr();
printf("Enter base and exponent: ");
scanf("%lf %lf", &base, &exponent);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num1, num2, remainder;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
getch();
}
Example Input/Output
Enter a number: 5
Original number: 5
After increment: 6
After decrement: 5
Enter a number: 10
Original number: 10
After increment: 11
After decrement: 10
These are the C programs for the requested tasks, along with example
outputs. You can compile and execute them in any C environment.
Here are the C programs for the listed tasks along with their outputs:
1. Check whether a number is negative or positive
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.");
} else if (num < 0) {
printf("The number is negative.");
} else {
printf("The number is zero.");
}
getch();
}
Example Input/Output
Enter a number: -5
The number is negative.
Enter a number: 10
The number is positive.
2. Display the day of the week using a switch case
Program
#include<stdio.h>
#include<conio.h>
void main() {
int day;
clrscr();
printf("Enter a number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid input! Enter a number between 1 and
7.");
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
char operator;
float num1, num2, result;
clrscr();
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 != 0)
result = num1 / num2;
else {
printf("Division by zero is not allowed.");
getch();
return;
}
break;
default:
printf("Invalid operator!");
getch();
return;
}
printf("Result = %.2f", result);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int marks;
clrscr();
printf("Enter the marks (0-100): ");
scanf("%d", &marks);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int age;
clrscr();
printf("Enter your age: ");
scanf("%d", &age);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int a, b, c;
clrscr();
printf("Enter the three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a == b && b == c) {
printf("The triangle is equilateral.");
} else if (a == b || b == c || a == c) {
printf("The triangle is isosceles.");
} else {
printf("The triangle is scalene.");
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
char operator;
float num1, num2, result;
clrscr();
printf("Select operation (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(operator) {
case '+': result = num1 + num2; printf("Result = %.2f",
result); break;
case '-': result = num1 - num2; printf("Result = %.2f",
result); break;
case '*': result = num1 * num2; printf("Result = %.2f",
result); break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result = %.2f", result);
} else {
printf("Cannot divide by zero.");
}
break;
default: printf("Invalid operator!");
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int i = 1;
clrscr();
do {
printf("Hello World\n");
i++;
} while (i <= 10);
getch();
}
Output
Hello World
Hello World
...
(10 times)
Program
#include<stdio.h>
#include<conio.h>
void main() {
int n, i, sum = 0;
clrscr();
printf("Enter the value of N: ");
scanf("%d", &n);
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, reverse = 0, remainder;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed Number = %d", reverse);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, i;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
Example Input/Output
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, i, factorial = 1;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
Example Input/Output
Enter a number: 5
Factorial of 5 = 120
Program
#include<stdio.h>
#include<conio.h>
void main() {
int n, first = 0, second = 1, next, i;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &n);
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, i, isPrime = 1;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.", num);
} else {
printf("%d is not a prime number.", num);
}
getch();
}
Example Input/Output
Enter a number: 7
7 is a prime number.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
int num, originalNum, remainder, result = 0, n = 0;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
// Count digits
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num) {
printf("%d is an Armstrong number.", num);
} else {
printf("%d is not an Armstrong number.", num);
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num, originalNum, reverse = 0, remainder;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if (reverse == originalNum) {
printf("%d is a palindrome.", originalNum);
} else {
printf("%d is not a palindrome.", originalNum);
}
getch();
}
Example Input/Output
Example 1:
#include<stdio.h>
#include<conio.h>
void main() {
int i, j;
clrscr();
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
printf("%d", i);
}
printf("\n");
}
getch();
}
Output
11111
22222
33333
44444
55555
Example 2:
#include<stdio.h>
#include<conio.h>
void main() {
int i, j;
clrscr();
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
getch();
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
These programs use various loops and nested structures effectively to solve the given
problems.
1. Find the sum of two numbers using passing arguments and returning
values
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num1, num2, sum;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
2. Find the sum using passing arguments but not returning values
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num1, num2;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
findSum(num1, num2);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
int findSum() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
return a + b;
}
void main() {
int sum;
clrscr();
sum = findSum();
printf("Sum = %d", sum);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void findSum() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
}
void main() {
clrscr();
findSum();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
void main() {
int num, result;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
result = factorial(num);
printf("Factorial of %d = %d", num, result);
getch();
}
Example Input/Output
Enter a number: 5
Factorial of 5 = 120
Program
#include<stdio.h>
#include<conio.h>
int fibonacci(int n) {
if (n == 1)
return 0;
else if (n == 2)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
void main() {
int terms, i;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &terms);
printf("Fibonacci Sequence: ");
for (i = 1; i <= terms; i++) {
printf("%d ", fibonacci(i));
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10], i;
clrscr();
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
Example Input/Output
Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
The numbers are:
1 2 3 4 5 6 7 8 9 10
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10], i, min;
clrscr();
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
min = arr[0];
for (i = 1; i < 10; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
printf("The smallest element is: %d", min);
getch();
}
Example Input/Output
Enter 10 numbers:
4 7 1 9 3 6 2 8 10 5
The smallest element is: 1
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10], i, search, found = 0;
clrscr();
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
if (found) {
printf("Element %d found at position %d.", search, i + 1);
} else {
printf("Element %d not found.", search);
}
getch();
}
Example Input/Output
Enter 10 numbers:
5 8 3 7 2 6 9 1 4 10
Enter the number to search: 7
Element 7 found at position 4.
Program
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10], i, j, temp;
clrscr();
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
Example Input/Output
Enter 10 numbers:
9 4 7 1 6 3 2 10 5 8
Sorted array in ascending order:
1 2 3 4 5 6 7 8 9 10
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10], i, j, temp;
clrscr();
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < 10; i++) {
for (j = i + 1; j < 10; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
Example Input/Output
Enter 10 numbers:
3 9 1 7 6 4 2 8 5 10
Sorted array in descending order:
10 9 8 7 6 5 4 3 2 1
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[3][3], i, j;
clrscr();
printf("Enter elements of a 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &arr[i][j]);
}
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int mat1[3][3], mat2[3][3], sum[3][3], i, j;
clrscr();
printf("Enter elements of first 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Sum of matrices:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
int mat1[3][3], mat2[3][3], result[3][3], i, j, k;
clrscr();
printf("Enter elements of first 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat1[i][j]);
}
}
Enter elements of first 3x3 matrix: 1 2 3 4 5 6 7 8 9 Enter elements of second 3x3 matrix: 9 8
7 6 5 4 3 2 1 Product of matrices: 30 24 18 84 69 54 138 114 90
Program
#include<stdio.h>
#include<conio.h>
void main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr;
clrscr();
getch();
}
Example Input/Output
Original Array: 10 20 30 40 50
Program
#include<stdio.h>
#include<conio.h>
void main() {
int num1, num2;
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
void main() {
char str[50];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
reverseString(str);
printf("Reversed string is: %s", str);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
int *ptr, n, i;
clrscr();
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
int *ptr, n, i;
clrscr();
if (ptr == NULL) {
printf("Memory allocation failed");
return;
}
Example Input/Output
These programs provide basic examples of pointer manipulation and dynamic memory
allocation in C.
Here are the C programs for the requested tasks using string functions:
Program
#include<stdio.h>
#include<conio.h>
void main() {
clrscr();
printf("Your Name: John Doe\n");
getch();
}
Example Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str[100];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str[100];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char source[100], destination[100];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str1[100], str2[100];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str1[100], str2[100];
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str[100], rev[100];
int i, len;
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char names[5][100];
char temp[100];
int i, j;
clrscr();
printf("Enter 5 names:\n");
for(i = 0; i < 5; i++) {
gets(names[i]);
}
getch();
}
Example Input/Output
Enter 5 names:
John
Alice
Bob
David
Charlie
Explanation:
These programs utilize standard string manipulation functions like strlen, strrev,
strcpy, strcat, strcmp to demonstrate common tasks involving strings in C.
Here are the C programs demonstrating the use of structures and unions as per your
request:
Program
#include<stdio.h>
#include<conio.h>
struct Employee {
int id;
char name[50];
char sex;
float salary;
};
void main() {
struct Employee emp;
clrscr();
printf("\nEmployee Record:\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
printf("Sex: %c\n", emp.sex);
printf("Salary: %.2f\n", emp.salary);
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
struct Book {
int book_id;
char book_name[50];
float price;
int pages;
};
void main() {
struct Book book;
clrscr();
printf("\nBook Record:\n");
printf("ID: %d\n", book.book_id);
printf("Name: %s\n", book.book_name);
printf("Price: %.2f\n", book.price);
printf("Pages: %d\n", book.pages);
getch();
}
Example Input/Output
Book Record:
ID: 1
Name: C Programming
Price: 299.99
Pages: 400
Program
#include<stdio.h>
#include<conio.h>
struct Student {
int roll_number;
char name[50];
float marks;
};
void main() {
struct Student stu;
clrscr();
printf("\nStudent Record:\n");
printf("Roll Number: %d\n", stu.roll_number);
printf("Name: %s\n", stu.name);
printf("Marks: %.2f\n", stu.marks);
getch();
}
Example Input/Output
Student Record:
Roll Number: 101
Name: Alice
Marks: 85.50
4. Create a structure for a point with x and y coordinates and calculate the
distance
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct Point {
int x;
int y;
};
void main() {
struct Point point1, point2;
clrscr();
getch();
}
Example Input/Output
Program
#include<stdio.h>
#include<conio.h>
struct Employee {
int id;
char name[50];
};
void main() {
struct Employee employees[3];
clrscr();
printf("\nEmployee Records:\n");
for(int i = 0; i < 3; i++) {
printf("\nEmployee %d:\n", i+1);
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
}
getch();
}
Example Input/Output
Employee Records:
Employee 1:
ID: 101
Name: John
Employee 2:
ID: 102
Name: Alice
Employee 3:
ID: 103
Name: Bob
6. Demonstrate the use of a union
Program
#include<stdio.h>
#include<conio.h>
union Data {
int i;
float f;
char str[20];
};
void main() {
union Data data;
clrscr();
// Storing an integer
data.i = 10;
printf("Data stored as integer: %d\n", data.i);
getch();
}
Example Input/Output
Explanation:
1. Employee Record Structure: Defines an employee structure with fields for ID,
name, sex, and salary and displays the data entered by the user.
2. Book Structure: Defines a book structure with fields for book ID, name, price, and
pages, and displays them.
3. Student Structure: Defines a student structure with fields for roll number, name,
and marks, and displays the data.
4. Point Structure: Defines a point structure with x and y coordinates and calculates
the distance between two points using the Euclidean formula.
5. Array of Structures: Demonstrates the use of an array of structures to store and
display records of multiple employees.
6. Union: Demonstrates the use of a union where different data types share the same
memory location, and overwriting one member will affect the others.
These programs illustrate how to use structures for data organization and how unions
allow storing different data types in the same memory space.
Here are the C programs to handle basic file operations such as writing to a file, reading
from a file, counting characters/words/lines, and writing squares of numbers into a file:
1. WAP to ask the name and age of a person and store it in a file named
record.dat.
Program
#include <stdio.h>
#include <conio.h>
struct Person {
char name[50];
int age;
};
void main() {
FILE *file;
struct Person p;
clrscr();
if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}
getch();
}
Example Output (content in record.dat)
2. WAP to read from a file and display its contents on the console.
Program
#include <stdio.h>
#include <conio.h>
void main() {
FILE *file;
char ch;
clrscr();
if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}
Example Output
Program
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main() {
FILE *file;
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
int inWord = 0;
clrscr();
if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}
if (ch == '\n') {
lineCount++;
}
if (isspace(ch)) {
inWord = 0;
} else if (inWord == 0) {
wordCount++;
inWord = 1;
}
}
getch();
}
Example Output
Number of characters: 21
Number of words: 4
Number of lines: 2
4. WAP to write the squares of numbers from 1 to 10 into a file.
Program
#include <stdio.h>
#include <conio.h>
void main() {
FILE *file;
int i, square;
clrscr();
if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}
getch();
}
Example Output (content in squares.dat)
Square of 1: 1
Square of 2: 4
Square of 3: 9
Square of 4: 16
Square of 5: 25
Square of 6: 36
Square of 7: 49
Square of 8: 64
Square of 9: 81
Square of 10: 100
Explanation:
1. Store name and age in a file: This program stores a person's name and age in a file
(record.dat) using a structure. The user is prompted to enter the name and age,
and the data is written to the file.
2. Read from a file and display its contents: This program opens a file and reads its
contents character by character, displaying them on the console.
3. Count characters, words, and lines in a file: This program reads a file and counts
the number of characters, words, and lines using simple logic and checks for
spaces and newlines to identify words and lines.
4. Write squares of numbers to a file: This program writes the squares of numbers
from 1 to 10 into a file (squares.dat), displaying the result in the file.
These file handling programs demonstrate basic file operations like reading, writing, and
counting, which are common tasks in C programming.