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

Practical Solution Sem 1 C

Uploaded by

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

Practical Solution Sem 1 C

Uploaded by

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

Practical Solutions By Pratham

1.1) Write a program to read 5 integer numbers and Calculate and Print Average
of 5 given no’s.

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

void main()
{

int a,b,c,d,e,avg;
clrscr();

printf("Enter The Fist Number : ");


scanf("%d",&a);

printf("Enter The Second Number : ");


scanf("%d",&b);

printf("Enter The Third Number : ");


scanf("%d",&c);

printf("Enter The Fourth Number : ");


scanf("%d",&d);

printf("Enter The Fifth Number : ");


scanf("%d",&e);

avg = (a+b+c+d+e)/5;

printf("Average Of All Number Is : %d",avg);


getch();

1|Page
1.2) Write a program To find simple interest. Hint: SI = (P * R * N)/100

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

void main()
{

float p, r, n, si;
clrscr();

printf("Enter Principal amount: ");


scanf("%f", &p);

printf("Enter Interest Rate : ");


scanf("%f", &r);

printf("Enter Time period : ");


scanf("%f", &n);

si = (p * r * n) / 100;

printf("Simple Interest = %0.2f\n", si);

getch();

2|Page
1.3) Write a program To find area of circle (a=∏r 2 where ∏=3.14)

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

void main()
{

float radius, area;


const float PI = 3.14;
clrscr();

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;

printf("Area of the circle = %0.2f\n", area);

getch();

3|Page
1.4) Write a program to swap two numbers using third variable.

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

void main()
{

int a, b, temp;
clrscr();

printf("Enter the first number: ");


scanf("%d", &a);

printf("Enter the second number: ");


scanf("%d", &b);

clrscr();

printf("Before swapping:\n");
printf("First number = %d\n", a);
printf("Second number = %d\n", b);

temp = a;
a = b;
b = temp;

printf("\nAfter swapping:\n");
printf("First number = %d\n", a);
printf("Second number = %d\n", b);

getch();

4|Page
1.5) To read customer number, customer name, past meter reading, present meter
reading, charge per unit. Calculate bill amount and print all the information

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

void main() {
int cnum;
char cname[50];
float pastr, presentr, charge, bill;
clrscr();

printf("Enter customer number: ");


scanf("%d", &cnum);

printf("Enter customer name: ");


scanf("%s", cname);

printf("Enter past meter reading: ");


scanf("%f", &pastr);

printf("Enter present meter reading: ");


scanf("%f", &presentr);

printf("Enter charge per unit: ");


scanf("%f", &charge);

bill = (presentr - pastr) * charge;


printf("\nCustomer Number: %d\n", cnum);
printf("Customer Name: %s\n", cname);
printf("Past Meter Reading: %0.2f\n", pastr);
printf("Present Meter Reading: %0.2f\n", presentr);
printf("Charge Per Unit: %0.2f\n", charge);
printf("Bill bill: %0.2f\n", bill);

getch();
}

5|Page
1.6) To display following output : *********************************************************
* Roll No: _____________ *
* Name: _____________ *
* Class: _____________ * *******************************************************

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

void main()
{
char sname[50],clas[20];
int rno;
clrscr();

printf("Enter Roll number: ");


scanf("%d", &rno);

printf("Enter Student Name: ");


scanf("%s", &sname);

printf("Enter Class Name : ");


scanf("%s", &clas);

printf(“*************************************************\n”);
printf("\n*Roll number: %d*\n", rno);
printf("*Student Name: %s*\n", sname);
printf("*Class Name : %s*\n", clas);
printf(“*************************************************\n”);

getch();
}

6|Page
2. Program Based On If Statement..

2.1) Write a Program To check whether given number is odd or even.

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

void main()
{
int num;
clrscr();

printf("Enter an Number: ");


scanf("%d", &num);

if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
else
{
printf("%d is odd.\n", num);
}

getch();
}

7|Page
2.2) Write a program to check whether given number is positive, negative or zero.

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

void main()
{
int num;
clrscr();

printf("Enter a number: ");


scanf("%d", &num);

if (num > 0)
{
printf("%d is positive.\n", num);
}

else if (num < 0)


{
printf("%d is negative.\n", num);
}

else {
printf("You entered zero.\n");
}

getch();
}

8|Page
2.3) Write a program To find maximum number from given three numbers.

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

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

printf("Enter three numbers: ");


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

if (num1 >= num2 && num1 >= num3) {


max = num1;
} else if (num2 >= num1 && num2 >= num3) {
max = num2;
} else {
max = num3;
}

printf("The maximum number is: %d\n", max);

getch();
}

9|Page
2.4) Write C Program that read a number and checks weather the given no. is
divisible by X or not. (X is any no. entered from user)

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

void main()
{
int num, x;
clrscr();

printf("Enter a number: ");


scanf("%d", &num);

printf("Enter the divisor (X): ");


scanf("%d", &x);

if (x != 0)
{
if (num % x == 0)
{
printf("%d is divisible by %d.\n", num, x);
}

else
{
printf("%d is not divisible by %d.\n", num, x);
}
}

else
{
printf("Divisor cannot be zero.\n");
}

getch();
}

10 | P a g e
2.5) Write a program to read two operands and one arithmetic operator and
perform the operation according to it using switch statement.

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

void main()
{
char operator;
double num1, num2, result;
clrscr();

printf("Enter first number: ");


scanf("%lf", &num1);

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter second number: ");


scanf("%lf", &num2);

switch (operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;

case '-':
result = num1 - num2;
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;

case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;

case '/':
if (num2 != 0)
{
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
}

else
{
11 | P a g e
printf("Error! Division by zero.\n");
}
break;

default:
printf("Error! Operator is not correct.\n");
break;
}

getch();
}

12 | P a g e
2.6) Input an integer number. Check & print the message Number is one digit, two
digit …..Five digit.

#include <stdio.h>
#include <conio.h>
void main()
{
int num;
clrscr();

printf("Enter an integer number: ");


scanf("%d", &num);

if(num>=0 && num<=9)


{
printf("Number is one digit.\n");
}

else if(num>=10 && num<=99)


{
printf("Number is two digits.\n");
}

else if(num>=100 && num<=999)


{
printf("Number is three digits.\n");
}

else if(num>=1000 && num<=9999)


{
printf("Number is four digits.\n");
}

else if(num>=10000 && num<=99999)


{
printf("Number is five digit. \n");
}

else
{
printf("Enter Correct Number greater than zero");
}

getch(); }
2.7) Input month number and print corresponding month name.

13 | P a g e
#include <stdio.h>
#include <conio.h>

void main()
{
int month;
clrscr();

printf("Enter month number (1-12): ");


scanf("%d", &month);

switch (month) {
case 1:
printf("January\n");
break;

case 2:
printf("February\n");
break;

case 3:
printf("March\n");
break;

case 4:
printf("April\n");
break;

case 5:
printf("May\n");
break;

case 6:
printf("June\n");
break;

case 7:
printf("July\n");
break;

case 8:
printf("August\n");
break;

case 9:
printf("September\n");

14 | P a g e
break;

case 10:
printf("October\n");
break;

case 11:
printf("November\n");
break;

case 12:
printf("December\n");
break;

default:
printf("Invalid month number! Please enter a number between 1 and 12.\n");
break;
}

getch()

15 | P a g e
2.8) An electric power distribution company charges its domestic consumers as
follows:

Consumption Units Rate of Charge


1-100 Rs. 0.75 per unit
101-300 Rs. 75 plus Rs. 1.00 per unit excess
of 100
301-500 Rs. 275 plus Rs. 1.50 per unit
excess of 300
500 and above Rs. 575 plus Rs. 1.75 per unit
excess of 500

Write a program that read customer number & power consumed and print the
amount to be paid by the customer. Note that output should be well
formatted.

#include <stdio.h>
#include <conio.h>
void main()
{
int customerNumber, units;
float amount;
clrscr();

printf("Enter customer number: ");


scanf("%d", &customerNumber);

printf("Enter power consumed (in units): ");


scanf("%d", &units);

if (units <= 100)


{
amount = units * 0.75;
}

else if (units <= 300) {


amount = 75 + (units - 100) * 1.00;
}

else if (units <= 500) {


amount = 275 + (units - 300) * 1.50;
}

else {
amount = 575 + (units - 500) * 1.75;

16 | P a g e
}

printf("\nCustomer Number: %d\n", customerNumber);


printf("Power Consumed: %d units\n", units);
printf("Amount to be paid: Rs. %0.2f\n", amount);

getch();
}

17 | P a g e
2.9) Write a program to find net salary of employee. Criteria to calculate net salary
are as follows:

Employee DA MA PF IT
code
1 to 5 67% 12% 10% 15%
6 to 12 62% 10% 9% 10%
13 to 15 55% 8% 8% 8%

DA, MA, PF and IT are given in percentage of basic salary.


Net salary = Basic salary + DA + MA – PF – IT

#include <stdio.h>
#include <conio.h>
void main()
{
int empCode;
float basicSalary, DA, MA, PF, IT, netSalary;
clrscr();

printf("Enter employee code: ");


scanf("%d", &empCode);

printf("Enter basic salary: ");


scanf("%f", &basicSalary);

if (empCode >= 1 && empCode <= 5)


{
DA = basicSalary * 0.67;
MA = basicSalary * 0.12;
PF = basicSalary * 0.10;
IT = basicSalary * 0.15;
}

else if (empCode >= 6 && empCode <= 12)


{
DA = basicSalary * 0.62;
MA = basicSalary * 0.10;
PF = basicSalary * 0.09;
IT = basicSalary * 0.10;
}

else if (empCode >= 13 && empCode <= 15)


{
DA = basicSalary * 0.55;
MA = basicSalary * 0.08;

18 | P a g e
PF = basicSalary * 0.08;
IT = basicSalary * 0.08;
}
else {
printf("Invalid employee code!\n");
}

netSalary = basicSalary + DA + MA - PF - IT;

printf("\nEmployee Code: %d\n", empCode);


printf("Basic Salary: Rs. %0.2f\n", basicSalary);
printf("DA: Rs. %0.2f\n", DA);
printf("MA: Rs. %0.2f\n", MA);
printf("PF: Rs. %0.2f\n", PF);
printf("IT: Rs. %0.2f\n", IT);
printf("Net Salary: Rs. %0.2f\n", netSalary);

getch();
}

19 | P a g e
2.10) To read student roll number, name and marks of 3 subjects. Calculate total,
percentage class and result according to criteria. (Use flushall)

If student fails in one or two subjects then declare result as “ATKT” and class
as ****
If student fails in more than two subjects then declare result as “FAIL” and
class as ****
If student passes all subjects then declare result as “PASS” and find class
according to following criteria :-

If percentage is >=70 then class is distinction.


If percentage is >=60 then class is First class.
If percentage is >=50 then class is Second class.
If percentage is >=40 then class is Pass class.

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

void main()
{
int rollNumber, marks1, marks2, marks3, total;
float percentage;
char name[50], result[10], clas[20];
clrscr();

printf("Enter roll number: ");


scanf("%d", &rollNumber);

printf("Enter name: ");


getchar();
gets(name);

printf("Enter marks of three subjects: ");


scanf("%d %d %d", &marks1, &marks2, &marks3);

total = marks1 + marks2 + marks3;


percentage = (float)total / 3;

if (marks1 < 40 || marks2 < 40 || marks3 < 40)


{
int failCount = 0;
if (marks1 < 40) failCount++;
if (marks2 < 40) failCount++;

20 | P a g e
if (marks3 < 40) failCount++;

if (failCount > 2) {
strcpy(result, "FAIL");
strcpy(clas, "****");
}

else {
strcpy(result, "ATKT");
strcpy(clas, "****");
}
}

else
{
strcpy(result, "PASS");

if (percentage >= 70) {


strcpy(clas, "Distinction");
}

else if (percentage >= 60) {


strcpy(clas, "First clas");
}

else if (percentage >= 50) {


strcpy(clas, "Second clas");
}

else if (percentage >= 40) {


strcpy(clas, "Pass clas");
}
}

printf("\nRoll Number: %d\n", rollNumber);


printf("Name: %s\n", name);
printf("Total Marks: %d\n", total);
printf("Percentage: %.2f%%\n", percentage);
printf("Result: %s\n", result);
printf("clas: %s\n", clas);

getch();
}

21 | P a g e
3. Programs based on Loops

3.1) Read 10 integer numbers and find Sum of first 10 integer nos .

#include <stdio.h>
#include <conio.h>
void main()
{
int numbers[10];
int sum = 0;
clrscr();

printf("Enter 10 integer numbers:\n");


for (int i = 0; i < 10; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
sum += numbers[i];
}

printf("The sum of the 10 numbers is: %d\n", sum);

getch();
}

22 | P a g e
3.2) To find out N! (Factorial of N).

#include <stdio.h>
#include <conio.h>
void main()
{
int n;
unsigned long long factorial = 1;
clrscr();

printf("Enter a number to find its factorial: ");


scanf("%d", &n);

if (n < 0)
{
printf("Factorial of a negative number doesn't exist.\n");
}

else
{
for (int i = 1; i <= n; i++)
{
factorial *= i;
}
printf("Factorial of %d = %d\n", n, factorial);
}

getch();
}

23 | P a g e
3.3) Read x and y and print value of XY. (Power)

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

void main()
{
int x, y;
long long result = 1;
clrscr();

printf("Enter the base (x): ");


scanf("%d", &x);

printf("Enter the exponent (y): ");


scanf("%d", &y);

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


result *= x;
}

printf("%d raised to the power %d is = %lld\n", x, y, result);

getch();
}

24 | P a g e
3.4) To print N terms of Fibonacci series.
Input: N = 9
Output: Fibonacci series: 1 1 2 3 5 8 13 21

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

int fibonacci();

void main()
{
int n;
clrscr();

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


scanf("%d", &n);

printf("Fibonacci series: ");


for (int i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}

printf("\n");

return 0;
}

int fibonacci(int n)
{
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

25 | P a g e
3.5) Read an integer no and print Reverse of the given number

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

void main() {
int n, reverse = 0, remainder;
clrscr();

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d\n", reverse);

getch();
}

26 | P a g e
3.6) To check whether inputted number is palindrome number or not.

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

void main()
{
int num, reversedNum = 0, remainder, originalNum;
clrscr();

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

while (num != 0)
{
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

if (originalNum == reversedNum)
{
printf("%d is a palindrome.\n", originalNum);
}

else
{
printf("%d is not a palindrome.\n", originalNum);
}

getch();
}

27 | P a g e
3.7) To find sum of odd value and even value digits of a given number.

#include <stdio.h>
#include <conio.h>
void main()
{
int n, digit, sumOdd = 0, sumEven = 0;
clrscr();

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0)
{
digit = n % 10;

if (digit % 2 == 0)
{
sumEven += digit;
}
else
{
sumOdd += digit;
}
n /= 10;
}

printf("Sum of odd digits = %d\n", sumOdd);


printf("Sum of even digits = %d\n", sumEven);

getch();
}

28 | P a g e
3.8) Write a program for 1-2+3-4…….N terms

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

void main()
{
int n, i, sum = 0;
clrscr();

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


scanf("%d", &n);

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


{
if (i % 2 == 0)
{
sum -= i;
}

else
{
sum += i;
}
}

printf("Sum of the series = %d\n", sum);

getch();
}

29 | P a g e
3.9) Write a program for x + x2/2! + x3/3! + x4/4!……..xn/n

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

int factorial();

void main()
{
float x, sum = 0.0;
int n;
clrscr();

printf("Enter the value of x: ");


scanf("%f", &x);

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


scanf("%d", &n);

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


{
sum += pow(x, i) / factorial(i);
}

printf("The sum of the series is: %f\n", sum);

getch();
}

int factorial(int num)


{
int fact = 1;
for (int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}

30 | P a g e
3.10) To print the following pattern for n=4.
1
12
123
1234

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

void main()
{
int n = 4;
clrscr();

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


{
for (int j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}

getch();
}

31 | P a g e
3.11) To print the following pattern for n=4.
1
12
123
1234

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

void main()
{
int n = 4;
clrscr();

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


{
for (int j = i; j < n; j++)
{
printf(" ");
}

for (int j = 1; j <= i; j++)


{
printf("%d ", j);
}
printf("\n");
}

getch();
}

32 | P a g e
3.12) To print the following pattern for n=4.(n in range 1 to 10)
A
A B A
A B C B A
A B C D C B A

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

void main()
{
int n = 4;
clrscr();

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


{
for (int j = i; j < n; j++)
{
printf(" ");
}

for (int j = 1; j <= i; j++)


{
printf("%c ", 'A' + j - 1);
}

for (int j = i - 1; j >= 1; j--)


{
printf("%c ", 'A' + j - 1);
}
printf("\n");
}

getch();
}

33 | P a g e
4. Programs Based on Array

4.1) Input n elements in an array and find sum of all elements of array.(1D)

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

void main()
{
int n, sum = 0;
clrscr();

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


scanf("%d", &n);

int arr[n];

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


for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
sum += arr[i];
}

printf("The sum of the elements is: %d\n", sum);

getch();
}

34 | P a g e
4.2) Find out the maximum and minimum element from one dimensional array

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

void main()
{
int n;
clrscr();

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


scanf("%d", &n);

int arr[n];

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


for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

int max = arr[0];


int min = arr[0];

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


{
if (arr[i] > max)
{
max = arr[i];
}

if (arr[i] < min)


{
min = arr[i];
}
}

printf("The maximum element is: %d\n", max);


printf("The minimum element is: %d\n", min);

getch();
}

35 | P a g e
4.3) Read n elements into one dimensional array and Sort the elements in
ascending order

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

void main()
{
int n;
clrscr();

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


scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

for (int i = 0; i < n - 1; i++)


{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

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


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

getch();
}

36 | P a g e
4.4) Read n elements into one dimensional array and find Frequency of each
element.

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

void main()
{
int arr[100], freq[100];
int size, i, j, count;
clrscr();

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for(i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}

for(i = 0; i < size; i++)


{
count = 1;
for(j = i + 1; j < size; j++)
{
if(arr[i] == arr[j])
{
count++;

freq[j] = 0;
}
}

if(freq[i] != 0)
{
freq[i] = count;
}
}

printf("\nFrequency of all elements of array:\n");


for(i = 0; i < size; i++)
{
if(freq[i] != 0)

37 | P a g e
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

getch();
}

38 | P a g e
4.5) Enter two matrix A and B. create a new matrix C which stores all elements of
matrix A first, then all elements of matrix B.

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

void main()
{
int rowsA, colsA, rowsB, colsB;
int i, j;
clrscr();

printf("Enter the number of rows and columns of matrix A: ");


scanf("%d %d", &rowsA, &colsA);

printf("Enter the number of rows and columns of matrix B: ");


scanf("%d %d", &rowsB, &colsB);

int A[rowsA][colsA], B[rowsB][colsB];


int C[rowsA * colsA + rowsB * colsB];

printf("Enter elements of matrix A:\n");


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

printf("Enter elements of matrix B:\n");


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

int k = 0;
for(i = 0; i < rowsA; i++)
{
for(j = 0; j < colsA; j++)
{
C[k++] = A[i][j];

39 | P a g e
}
}

for(i = 0; i < rowsB; i++)


{
for(j = 0; j < colsB; j++)
{
C[k++] = B[i][j];
}
}

printf("Elements of matrix C:\n");


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

getch();
}

4.6) To read two matrix A and B and perform matrix addition. (A & B both mxn
matrix)

40 | P a g e
#include <stdio.h>
#include <conio.h>

void main()
{
int rows, cols;
int i, j;
clrscr();

printf("Enter the number of rows and columns of the matrices: ");


scanf("%d %d", &rows, &cols);

int A[rows][cols], B[rows][cols], C[rows][cols];

printf("Enter elements of matrix A:\n");


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

printf("Enter elements of matrix B:\n");


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

for(i = 0; i < rows; i++)


{
for(j = 0; j < cols; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}

printf("Resultant matrix C (A + B):\n");


for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d ", C[i][j]);

41 | P a g e
}
printf("\n");
}

getch();
}

42 | P a g e
4.7) Read a matrix and display the Transpose Matrix

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

void main()
{
int rows, cols;
int i, j;
clrscr();

printf("Enter the number of rows and columns of the matrix: ");


scanf("%d %d", &rows, &cols);

int matrix[rows][cols], transpose[cols][rows];

printf("Enter elements of the matrix:\n");


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

for(i = 0; i < rows; i++)


{
for(j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

printf("Transpose of the matrix:\n");


for(i = 0; i < cols; i++)
{
for(j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

getch();
}
4.8) Read a matrix and check whether it is Identity Matrix or not.

43 | P a g e
#include <stdio.h>
#include <conio.h>

void main()
{
int size, i, j, isIdentity = 1;
clrscr();

printf("Enter the size of the matrix (n for an n x n matrix): ");


scanf("%d", &size);

int matrix[size][size];

printf("Enter elements of the matrix:\n");


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

for(i = 0; i < size; i++)


{
for(j = 0; j < size; j++)
{
if(i == j && matrix[i][j] != 1)
{
isIdentity = 0;
break;
}
else if(i != j && matrix[i][j] != 0)
{
isIdentity = 0;
break;
}
}
if(!isIdentity) break;
}

if(isIdentity) {
printf("The matrix is an Identity Matrix.\n");
} else {
printf("The matrix is not an Identity Matrix.\n");

44 | P a g e
}

getch();
}

45 | P a g e
5. Program based on string

5.1) Input string from user and find out Length of string without using strlen
Function

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

void main()
{
char str[100];
int length = 0;
clrscr();

printf("Enter a string: ");


gets(str);

while (str[length] != '\0')


{
length++;
}

printf("Length of the string is: %d\n", length);

getch();
}

46 | P a g e
5.2) Input string from user and find out Length of string using strlen Function

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

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

printf("Enter a string: ");


gets(str);

length = strlen(str);

printf("Length of the string is: %d\n", length);

getch();
}

47 | P a g e
5.3) Input two strings from user. Add second string at the end of first string without
using strcat function.

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

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

printf("Enter the first string: ");


gets(str1);

printf("Enter the second string: ");


gets(str2);

i = 0;
while (str1[i] != '\0')
{
i++;
}

j = 0;
while (str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';

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

getch();
}

48 | P a g e
5.4) Input two strings from user. Add second string at the end of first string using strcat
function

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

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

printf("Enter the first string: ");


gets(str1);

printf("Enter the second string: ");


gets(str2);

strcat(str1, str2);

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

getch();
}

49 | P a g e
5.5) To count and display the total number of Uppercase, Lowercase, digit, blank space
and special character from a given string.

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

void main()
{
char str[100];
int i;
int uppercase = 0, lowercase = 0, digits = 0, spaces = 0, special = 0;
clrscr();

printf("Enter a string: ");


gets(str);

for(i = 0; str[i] != '\0'; i++)


{
if(str[i] >= 'A' && str[i] <= 'Z') {
uppercase++;
}
else if(str[i] >= 'a' && str[i] <= 'z') {
lowercase++;
}
else if(str[i] >= '0' && str[i] <= '9') {
digits++;
}
else if(str[i] == ' ') {
spaces++;
}
else {
special++;
}
}

printf("Uppercase letters: %d\n", uppercase);


printf("Lowercase letters: %d\n", lowercase);
printf("Digits: %d\n", digits);
printf("Blank spaces: %d\n", spaces);
printf("Special characters: %d\n", special);

getch();
}

50 | P a g e
5.6) Convert string into upper case and

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

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

printf("Enter a string: ");


gets(str);

for(i = 0; str[i] != '\0'; i++) {


if(str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
}
printf("Uppercase string: %s\n", str);

getch();
}

Lower Case

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

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

printf("Enter a string: ");


gets(str);

for(i = 0; str[i] != '\0'; i++) {


if(str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32;
}
}
printf("Lowercase string: %s\n", str);

getch();
}

51 | P a g e
5.7) To Reverse a given string with without using strrev

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

void main()
{
char str[100], temp;
int i, j;
clrscr();

printf("Enter a string: ");


gets(str);

int length = strlen(str);

for(i = 0, j = length - 1; i < j; i++, j--) {


temp = str[i];
str[i] = str[j];
str[j] = temp;
}

printf("Reversed string: %s\n", str);

getch();
}

52 | P a g e
5.8) To Reverse a given string with using strrev

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

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

printf("Enter a string: ");


gets(str);

strrev(str);

printf("Reversed string: %s\n", str);

getch();
}

53 | P a g e
5.9) Write a menu based program : Enter a string and perform following operations
1.Encrypt
2.decrypt
3.quit

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

void encrypt(char str[])


{
int i;
for(i = 0; str[i] != '\0'; i++)
{
str[i] = str[i] + 3;
}
}

void decrypt(char str[])


{
int i;
for(i = 0; str[i] != '\0'; i++) {
str[i] = str[i] - 3;
}
}

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

while(1) {
printf("\nMenu:\n");
printf("1. Encrypt\n");
printf("2. Decrypt\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();

switch(choice)
{
case 1:
printf("Enter a string to encrypt: ");
gets(str);
encrypt(str);
printf("Encrypted string: %s\n", str);
break;

54 | P a g e
case 2:
printf("Enter a string to decrypt: ");
gets(str);
decrypt(str);
printf("Decrypted string: %s\n", str);
break;

case 3:
printf("Quitting...\n");
return 0;

default:
printf("Invalid choice! Please try again.\n");
}
}

getch();
}

55 | P a g e

You might also like