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

Programs with Their Outputs

Uploaded by

Anish Gelal
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 (0 votes)
55 views

Programs with Their Outputs

Uploaded by

Anish Gelal
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
You are on page 1/ 76

Programs with Their Outputs

1. Print "Hello World"

Program

#include<stdio.h>
#include<conio.h>

void main() {
clrscr();
printf("Hello World");
getch();
}

Output

Hello World

2. Find the sum of two numbers

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

Enter two numbers: 10 20


Sum = 30

3. Find the simple interest

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

Enter principal, rate, and time: 1000 5 2


Simple Interest = 100.00
4. Find the area of a circle

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

Enter the radius of the circle: 5


Area of Circle = 78.54

5. Convert Celsius to Fahrenheit

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

Enter temperature in Celsius: 0


Temperature in Fahrenheit = 32.00

6. Find the total and percentage of five subjects

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

Enter marks of five subjects: 80 90 85 75 95


Total = 425.00
Percentage = 85.00%
7. Print the ASCII value of a character

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

8. Calculate the perimeter of a rectangle

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

Enter length and breadth of the rectangle: 5 10


Perimeter of Rectangle = 30.00

9. Display input in reverse order

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

Enter a number: 12345


Reversed Number = 54321
Here are the C programs for the tasks listed, along with their
outputs:

1. Check whether a number is divisible by 5 and 8 or not

Program

#include<stdio.h>
#include<conio.h>

void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);

if (num % 5 == 0 && num % 8 == 0) {


printf("The number is divisible by both 5 and 8.");
} else {
printf("The number is not divisible by both 5 and 8.");
}

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.

2. Check if a character is a vowel or a consonant

Program

#include<stdio.h>
#include<conio.h>

void main() {
char ch;
clrscr();
printf("Enter a character: ");
scanf("%c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'


||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
printf("%c is a vowel.", ch);
} else {
printf("%c is a consonant.", ch);
}

getch();
}

Example Input/Output

Enter a character: A
A is a vowel.
Enter a character: B
B is a consonant.

3. Check if a number is even or odd

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);

if (a >= b && a >= c) {


printf("The largest number is %d", a);
} else if (b >= a && b >= c) {
printf("The largest number is %d", b);
} else {
printf("The largest number is %d", c);
}

getch();
}

Example Input/Output

Enter three numbers: 10 20 15


The largest number is 20
Enter three numbers: 5 3 2
The largest number is 5
5. Find the smallest among three numbers using nested if-
else

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

Enter three numbers: 5 2 7


The smallest number is 2
Enter three numbers: 12 15 10
The smallest number is 10
6. Calculate the power of a number using the pow() function

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);

result = pow(base, exponent);


printf("Result = %.2lf", result);

getch();
}

Example Input/Output

Enter base and exponent: 2 3


Result = 8.00
Enter base and exponent: 5 2
Result = 25.00
7. Calculate the remainder of two numbers using the modulus
operator

Program

#include<stdio.h>
#include<conio.h>

void main() {
int num1, num2, remainder;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

remainder = num1 % num2;


printf("The remainder of %d divided by %d is %d", num1, num2,
remainder);

getch();
}

Example Input/Output

Enter two numbers: 10 3


The remainder of 10 divided by 3 is 1
Enter two numbers: 17 5
The remainder of 17 divided by 5 is 2

8. Demonstrate the use of increment (++) and decrement (--)


operators

Program

#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);

printf("Original number: %d\n", num);


printf("After increment: %d\n", ++num); // Pre-increment
printf("After decrement: %d\n", --num); // Pre-decrement

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

Enter a number (1-7): 3


Wednesday
Enter a number (1-7): 8
Invalid input! Enter a number between 1 and 7.
3. Calculate using a switch case

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

Enter an operator (+, -, *, /): +


Enter two numbers: 10 20
Result = 30.00
Enter an operator (+, -, *, /): /
Enter two numbers: 10 0
Division by zero is not allowed.

4. Calculate the grade of a student based on marks using if-else

Program

#include<stdio.h>
#include<conio.h>

void main() {
int marks;
clrscr();
printf("Enter the marks (0-100): ");
scanf("%d", &marks);

if (marks >= 90) {


printf("Grade: A");
} else if (marks >= 80) {
printf("Grade: B");
} else if (marks >= 70) {
printf("Grade: C");
} else if (marks >= 60) {
printf("Grade: D");
} else {
printf("Grade: F");
}

getch();
}

Example Input/Output

Enter the marks (0-100): 85


Grade: B
Enter the marks (0-100): 45
Grade: F

5. Check the eligibility to vote based on age using if-else

Program

#include<stdio.h>
#include<conio.h>

void main() {
int age;
clrscr();
printf("Enter your age: ");
scanf("%d", &age);

if (age >= 18) {


printf("You are eligible to vote.");
} else {
printf("You are not eligible to vote.");
}

getch();
}
Example Input/Output

Enter your age: 20


You are eligible to vote.
Enter your age: 15
You are not eligible to vote.

6. Find the type of triangle using if-else

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

Enter the three sides of the triangle: 5 5 5


The triangle is equilateral.
Enter the three sides of the triangle: 5 5 8
The triangle is isosceles.

7. Perform a basic calculator using a switch case

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

Select operation (+, -, *, /): *


Enter two numbers: 10 5
Result = 50.00
Select operation (+, -, *, /): /
Enter two numbers: 8 0
Cannot divide by zero.

You can compile and test these programs in your C compiler.

1. Display "Hello World" 10 times using a do-while loop

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)

2. Find the sum of N numbers using a for loop

Program

#include<stdio.h>
#include<conio.h>

void main() {
int n, i, sum = 0;
clrscr();
printf("Enter the value of N: ");
scanf("%d", &n);

for (i = 1; i <= n; i++) {


sum += i;
}
printf("Sum of first %d numbers = %d", n, sum);
getch();
}

Example Input/Output

Enter the value of N: 5


Sum of first 5 numbers = 15
3. Reverse a number using a while loop

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

Enter a number: 1234


Reversed Number = 4321

4. Display the multiplication table of a given number

Program

#include<stdio.h>
#include<conio.h>
void main() {
int num, i;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);

for (i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}
getch();
}

Example Input/Output

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

5. Find the factorial of a given number

Program

#include<stdio.h>
#include<conio.h>

void main() {
int num, i, factorial = 1;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);

for (i = 1; i <= num; i++) {


factorial *= i;
}
printf("Factorial of %d = %d", num, factorial);
getch();
}

Example Input/Output

Enter a number: 5
Factorial of 5 = 120

6. Generate the Fibonacci series

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);

printf("Fibonacci Series: %d %d ", first, second);


for (i = 3; i <= n; i++) {
next = first + second;
printf("%d ", next);
first = second;
second = next;
}
getch();
}
Example Input/Output

Enter the number of terms: 7


Fibonacci Series: 0 1 1 2 3 5 8

7. Check whether an input number is prime

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.

8. Check if a number is an Armstrong 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

Enter a number: 153


153 is an Armstrong number.

9. Check if a number is a palindrome

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

Enter a number: 121


121 is a palindrome.

10. Display patterns through nested loops

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>

int findSum(int a, int b) {


return a + b;
}

void main() {
int num1, num2, sum;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

sum = findSum(num1, num2);


printf("Sum = %d", sum);
getch();
}
Example Input/Output

Enter two numbers: 5 10


Sum = 15

2. Find the sum using passing arguments but not returning values

Program

#include<stdio.h>
#include<conio.h>

void findSum(int a, int b) {


int sum = a + b;
printf("Sum = %d", sum);
}

void main() {
int num1, num2;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

findSum(num1, num2);
getch();
}

Example Input/Output

Enter two numbers: 7 8


Sum = 15
3. Find the sum using passing no arguments and returning values

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

Enter two numbers: 4 9


Sum = 13

4. Find the sum using no arguments and no return values

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

Enter two numbers: 6 14


Sum = 20

5. Find the factorial using a recursive function

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

6. Generate a Fibonacci sequence using a recursive function

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

Enter the number of terms: 7


Fibonacci Sequence: 0 1 1 2 3 5 8

1. Store and display values using an array

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]);
}

printf("The numbers are:\n");


for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
getch();
}

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

2. Find the smallest element in an array

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

3. Search for a particular element in an array

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]);
}

printf("Enter the number to search: ");


scanf("%d", &search);

for (i = 0; i < 10; i++) {


if (arr[i] == search) {
found = 1;
break;
}
}

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.

4. Display 10 numbers using an array

Program

(Same as 1. Store and display values using an array)

5. Sort an array in ascending order

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;
}
}
}

printf("Sorted array in ascending order:\n");


for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
getch();
}

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

6. Sort an array in descending order

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;
}
}
}

printf("Sorted array in descending order:\n");


for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
getch();
}

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

7. Store and display a two-dimensional array

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]);
}
}

printf("The 3x3 matrix is:\n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
getch();
}

Example Input/Output

Enter elements of a 3x3 matrix:


1 2 3
4 5 6
7 8 9
The 3x3 matrix is:
1 2 3
4 5 6
7 8 9

8. Find the sum of two matrices

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("Enter elements of second 3x3 matrix:\n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat2[i][j]);
}
}

for (i = 0; i < 3; i++) {


for (j = 0; j < 3; j++) {
sum[i][j] = mat1[i][j] + mat2[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

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
Sum of matrices:
10 10 10
10 10 10
10 10 10

9. Perform matrix multiplication

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]);
}
}

printf("Enter elements of second 3x3 matrix:\n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &mat2[i][j]);
}
}

for (i = 0; i < 3; i++) {


for (j =

0; j < 3; j++) { result[i][j] = 0; for (k = 0; k < 3; k++) { result[i][j] += mat1[i][k] * mat2[k][j]; } } }


printf("Product of matrices:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
getch();

#### Example Input/Output

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

1. Demonstrate pointer addition

Program

#include<stdio.h>
#include<conio.h>

void main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr;
clrscr();

ptr = arr; // Points to the first element of the array

printf("Original Array: ");


for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i)); // Pointer addition to access
elements
}

getch();
}

Example Input/Output

Original Array: 10 20 30 40 50

2. Swap two numbers using pointers

Program

#include<stdio.h>
#include<conio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void main() {
int num1, num2;
clrscr();

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);


swap(&num1, &num2); // Passing addresses of num1 and num2
printf("After swapping: num1 = %d, num2 = %d", num1, num2);

getch();
}

Example Input/Output

Enter two numbers: 10 20


Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10

3. Find the length of a string using pointers

Program

#include<stdio.h>
#include<conio.h>

int stringLength(char *str) {


int length = 0;
while (*str != '\0') { // Traverse till null character
length++;
str++;
}
return length;
}

void main() {
char str[50];
clrscr();

printf("Enter a string: ");


gets(str);

printf("Length of the string is: %d", stringLength(str));

getch();
}

Example Input/Output

Enter a string: Hello


Length of the string is: 5

4. Reverse a string using pointers

Program

#include<stdio.h>
#include<conio.h>

void reverseString(char *str) {


char *start = str;
char *end = str;
char temp;

// Move the end pointer to the last character of the string


while (*end != '\0') {
end++;
}
end--; // Move to the last character

// Swap the characters from start and end pointers


while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
void main() {
char str[50];
clrscr();

printf("Enter a string: ");


gets(str);

reverseString(str);
printf("Reversed string is: %s", str);

getch();
}

Example Input/Output

Enter a string: Hello


Reversed string is: olleH

5. Demonstrate the malloc function

Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main() {
int *ptr, n, i;
clrscr();

printf("Enter the number of elements: ");


scanf("%d", &n);

// Dynamically allocate memory using malloc


ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed");
return;
}

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}

printf("The entered elements are: ");


for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}

free(ptr); // Free the allocated memory


getch();
}

Example Input/Output

Enter the number of elements: 5


Enter 5 elements: 1 2 3 4 5
The entered elements are: 1 2 3 4 5

6. Demonstrate the calloc function

Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main() {
int *ptr, n, i;
clrscr();

printf("Enter the number of elements: ");


scanf("%d", &n);

// Dynamically allocate memory using calloc


ptr = (int *)calloc(n, sizeof(int));

if (ptr == NULL) {
printf("Memory allocation failed");
return;
}

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}

printf("The entered elements are: ");


for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}

free(ptr); // Free the allocated memory


getch();
}

Example Input/Output

Enter the number of elements: 4


Enter 4 elements: 10 20 30 40
The entered elements are: 10 20 30 40
Explanation:

1. Pointer Addition: Demonstrates how pointer arithmetic can be used to access


elements of an array.
2. Swap Using Pointers: Uses pointers to pass the addresses of variables to swap
their values.
3. String Length Using Pointers: Uses pointer traversal to count the length of a string.
4. String Reverse Using Pointers: Reverses the string by using two pointers: one at
the beginning and one at the end.
5. Malloc Function: Demonstrates memory allocation at runtime using malloc().
6. Calloc Function: Demonstrates memory allocation using calloc() which
initializes the allocated memory to zero.

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:

1. Display Your Name

Program

#include<stdio.h>
#include<conio.h>

void main() {
clrscr();
printf("Your Name: John Doe\n");
getch();
}
Example Output

Your Name: John Doe

2. Use of strlen function

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str[100];
clrscr();

printf("Enter a string: ");


gets(str);

printf("Length of the string is: %lu", strlen(str));

getch();
}

Example Input/Output

Enter a string: Hello, World!


Length of the string is: 13
3. Use of strrev function

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str[100];
clrscr();

printf("Enter a string: ");


gets(str);

printf("Reversed string is: %s", strrev(str));

getch();
}

Example Input/Output

Enter a string: Hello


Reversed string is: olleH

4. Copy a string from source to destination using strcpy function

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char source[100], destination[100];
clrscr();

printf("Enter a string: ");


gets(source);

strcpy(destination, source); // Copy source string to destination

printf("Source String: %s\n", source);


printf("Destination String: %s\n", destination);

getch();
}

Example Input/Output

Enter a string: Hello, World!


Source String: Hello, World!
Destination String: Hello, World!

5. Concatenate strings using strcat function

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str1[100], str2[100];
clrscr();

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

strcat(str1, str2); // Concatenate str2 to str1

printf("Concatenated String: %s\n", str1);

getch();
}

Example Input/Output

Enter first string: Hello


Enter second string: World
Concatenated String: HelloWorld

6. Compare strings using strcmp function

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str1[100], str2[100];
clrscr();

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

int result = strcmp(str1, str2); // Compare the two strings


if(result == 0) {
printf("The strings are identical.\n");
} else if(result > 0) {
printf("The first string is greater than the second.\n");
} else {
printf("The second string is greater than the first.\n");
}

getch();
}

Example Input/Output

Enter first string: Apple


Enter second string: Banana
The first string is greater than the second.

7. Check if a string is a palindrome or not

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str[100], rev[100];
int i, len;
clrscr();

printf("Enter a string: ");


gets(str);

// Find the length of the string


len = strlen(str);
// Reverse the string
for(i = 0; i < len; i++) {
rev[i] = str[len - 1 - i];
}
rev[i] = '\0'; // Null terminate the reversed string

// Check if the string is equal to its reverse


if(strcmp(str, rev) == 0) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}

getch();
}

Example Input/Output

Enter a string: madam


The string is a palindrome.

8. Sort names in ascending order

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]);
}

// Sorting names in ascending order using bubble sort


for(i = 0; i < 4; i++) {
for(j = i + 1; j < 5; j++) {
if(strcmp(names[i], names[j]) > 0) {
// Swap names[i] and names[j]
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}

printf("\nSorted Names in Ascending Order:\n");


for(i = 0; i < 5; i++) {
printf("%s\n", names[i]);
}

getch();
}

Example Input/Output

Enter 5 names:
John
Alice
Bob
David
Charlie

Sorted Names in Ascending Order:


Alice
Bob
Charlie
David
John

Explanation:

1. Display Your Name: A simple program to print a name.


2. strlen function: Computes the length of the entered string.
3. strrev function: Reverses the entered string using the strrev function.
4. strcpy function: Copies the contents of one string to another.
5. strcat function: Concatenates two strings into one.
6. strcmp function: Compares two strings and returns a value based on their
lexicographical order.
7. Palindrome Check: Compares the string with its reverse to check if it's a
palindrome.
8. Sorting Names: Sorts an array of names in ascending lexicographical order using
strcmp and bubble sort.

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:

1. Define a structure to store the record of an employee

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("Enter employee ID: ");


scanf("%d", &emp.id);

printf("Enter employee name: ");


getchar(); // to consume the newline left by previous scanf
gets(emp.name);

printf("Enter sex (M/F): ");


scanf("%c", &emp.sex);

printf("Enter salary: ");


scanf("%f", &emp.salary);

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

Enter employee ID: 101


Enter employee name: John Doe
Enter sex (M/F): M
Enter salary: 50000
Employee Record:
ID: 101
Name: John Doe
Sex: M
Salary: 50000.00

2. Read the data of a book using a structure

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("Enter Book ID: ");


scanf("%d", &book.book_id);

printf("Enter Book Name: ");


getchar(); // to consume the newline left by previous scanf
gets(book.book_name);

printf("Enter Book Price: ");


scanf("%f", &book.price);
printf("Enter Number of Pages: ");
scanf("%d", &book.pages);

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

Enter Book ID: 1


Enter Book Name: C Programming
Enter Book Price: 299.99
Enter Number of Pages: 400

Book Record:
ID: 1
Name: C Programming
Price: 299.99
Pages: 400

3. Create a structure for a student and display the data

Program

#include<stdio.h>
#include<conio.h>

struct Student {
int roll_number;
char name[50];
float marks;
};

void main() {
struct Student stu;

clrscr();

printf("Enter Roll Number: ");


scanf("%d", &stu.roll_number);

printf("Enter Name: ");


getchar(); // to consume the newline left by previous scanf
gets(stu.name);

printf("Enter Marks: ");


scanf("%f", &stu.marks);

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

Enter Roll Number: 101


Enter Name: Alice
Enter Marks: 85.5

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;
};

float calculateDistance(struct Point p1, struct Point p2) {


return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}

void main() {
struct Point point1, point2;

clrscr();

printf("Enter coordinates for point 1 (x y): ");


scanf("%d %d", &point1.x, &point1.y);

printf("Enter coordinates for point 2 (x y): ");


scanf("%d %d", &point2.x, &point2.y);

float distance = calculateDistance(point1, point2);

printf("\nDistance between points: %.2f\n", distance);

getch();
}
Example Input/Output

Enter coordinates for point 1 (x y): 1 2


Enter coordinates for point 2 (x y): 4 6

Distance between points: 5.00

5. Using an array of structures

Program

#include<stdio.h>
#include<conio.h>

struct Employee {
int id;
char name[50];
};

void main() {
struct Employee employees[3];
clrscr();

for(int i = 0; i < 3; i++) {


printf("\nEnter Employee %d ID: ", i+1);
scanf("%d", &employees[i].id);
printf("Enter Employee %d Name: ", i+1);
getchar(); // to consume the newline left by previous scanf
gets(employees[i].name);
}

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

Enter Employee 1 ID: 101


Enter Employee 1 Name: John

Enter Employee 2 ID: 102


Enter Employee 2 Name: Alice

Enter Employee 3 ID: 103


Enter Employee 3 Name: Bob

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);

// Storing a float (overwrites the integer value)


data.f = 220.5;
printf("Data stored as float: %.2f\n", data.f);

// Storing a string (overwrites the float value)


strcpy(data.str, "Hello, Union!");
printf("Data stored as string: %s\n", data.str);

getch();
}

Example Input/Output

Data stored as integer: 10


Data stored as float: 220.50
Data stored as string: Hello, Union!

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();

// Open file for writing (creates or overwrites if exists)


file = fopen("record.dat", "w");

if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}

// Input name and age


printf("Enter name: ");
gets(p.name);
printf("Enter age: ");
scanf("%d", &p.age);

// Write the data to the file


fprintf(file, "Name: %s\nAge: %d\n", p.name, p.age);

printf("\nRecord saved to file successfully.\n");

// Close the file


fclose(file);

getch();
}
Example Output (content in record.dat)

Name: John Doe


Age: 30

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();

// Open the file for reading


file = fopen("record.dat", "r");

if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}

// Read and display the content of the file


printf("Contents of the file:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // Print each character to console
}

// Close the file


fclose(file);
getch();
}

Example Output

Contents of the file:


Name: John Doe
Age: 30

3. WAP to count the number of characters, words, and lines in a file.

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();

// Open file for reading


file = fopen("record.dat", "r");

if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}

// Read the file character by character


while ((ch = fgetc(file)) != EOF) {
charCount++;

if (ch == '\n') {
lineCount++;
}

if (isspace(ch)) {
inWord = 0;
} else if (inWord == 0) {
wordCount++;
inWord = 1;
}
}

// Output the results


printf("Number of characters: %d\n", charCount);
printf("Number of words: %d\n", wordCount);
printf("Number of lines: %d\n", lineCount);

// Close the file


fclose(file);

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();

// Open file for writing


file = fopen("squares.dat", "w");

if (file == NULL) {
printf("Error opening file.\n");
getch();
return;
}

// Write the squares of numbers from 1 to 10 into the file


for (i = 1; i <= 10; i++) {
square = i * i;
fprintf(file, "Square of %d: %d\n", i, square);
}

printf("Squares of numbers from 1 to 10 saved to file


successfully.\n");

// Close the file


fclose(file);

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.

You might also like