sunnyc+++
sunnyc+++
NIT 3, Faridabad
PROGRAMMING IN ‘C’
Practical File
32. Program to accept two array and find out sum of two array.
33. Program to accept 3*3 matrix and find out the diagonal
elements from the matrix.
34. Program to accept m*n matrix find out the sum of diagonal
elements.
35. Program to accept m*n matrix and find out sum of all the
elements of the matrix.
36. Program to accept m*n matrix and find out the transpose of
the matrix.
37. Program to accept m*n matrix and find out sum of each row
and each column.
38. Program to accept 2 m*n matrix and find out the addition of 2
matrix.
39. Program to accept a string and find out the length of the
string.
40. Program to accept a string and copy the string to another
string.
41. Program to accept a string and find out the total no of words
in the string.
42. Program to accept a string in upper case and covert it into
lower case
43. Program to find out the factorial of a number using function.
INPUT:
#include <stdio.h>
#include <conio.h>
void main()
{ clrscr(); int length, breadth,
radius; float area_rectangle,
area_circle;
printf("Enter the length and breadth of the rectangle: ");
scanf("%d %d", &length, &breadth);
printf("Enter the radius of the circle: ");
scanf("%d", &radius);
area_rectangle = length * breadth;
area_circle = 3.14 * radius * radius;
printf("Area ofrectangle: %f\n",
area_rectangle);
printf("Area of circle: %f\n", area_circle);
printf("Enter any key to continue");
getch();
}
OUTPUT:
1
Write
PROGRAM:2
a program to accept temperature into
Celsius and convert it into Fahrenheit.
C*9/5+32=F.
INPUT:
#include <stdio.h>
#include<conio.h>
int main()
{ clrscr();
float celsius, fahrenheit;
printf("Enter temperature in Celsius:
"); scanf("%f", &celsius); fahrenheit =
(celsius * 9 / 5) + 32;
printf("%f Celsius = %f Fahrenheit\
n",celsius,fahrenheit);
printf("Enter any key to continue"); getch();
}
OUTPUT:
2
PROGRAM:3
Write a program to accept distance between
two cities in km and convert distance in
meter, feet and inches.
M=Km*1000,Feet=km*3280.8, inches=
km*39370.0787 .
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
float km, meter, feet, inches;
printf("Enter the distance between two cities in km:
"); scanf("%f", &km); meter = km * 1000; feet = km *
3280.8; inches = km * 39370.0787; printf("Distance in
meter: %f\n", meter); printf("Distance in feet: %f\n",
feet); printf("Distance in inches: %f\n", inches);
printf("Enter any key to continue"); getch();
}
OUTPUT:
3
Write
4
Write a to accept
PROGRAM:4
program basic salary from the user and
calculate the gross salary in which DA is 85% of
basic salary and HRA is 30% of basic salary.
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
float basic_salary, DA, HRA, gross_salary;
printf("Enter basic salary: "); scanf("%f",
&basic_salary); DA = 0.85 * basic salary;
HRA = 0.30 * basic salary; gross salary =
basic salary + DA + HRA; printf("Gross
salary: %.2f\n", gross_salary);
printf("Enter any key to continue");
getch();
}
OUTPUT:
PROGRAM:5
5
Write a to accept
program a number from user and check
whether that number is even or odd.
INPUT:
#include <stdio.h> #include
<conio.h>
int main()
{ clrscr(); int num;
printf("Enter a number:
"); scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is even.\n", num);
} else { printf("%d is odd.\n",
num);
}
getch();
}
OUTPUT:
PROGRAM:6
6
Write a to accept
Program three subjects marks. Find out the
total and %age marks and mark the division
according to the %age. If %age >=50 and %age
<60 than II division, If %age >=60 and %age
<75 than I division, If %age >=75 distinction .
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
int sub1, sub2, sub3, total; float
percentage; printf("Enter marks of three
subjects: "); scanf("%d %d %d", &sub1,
&sub2, &sub3);
total = sub1 + sub2 + sub3; percentage
= (float)total / 300 * 100; printf("Total
Marks: %d\n", total);
printf("Percentage: %f\n",
percentage); if (percentage >= 75)
{
printf("Division: Distinction\n");
}
else if (percentage >= 60)
{
printf("Division: First Division\n");
}
else if (percentage >= 50)
7
Write a to accept
{
printf("Division: Second Division\n");
}
8
else
{
printf("Division: Fail\n");
}
printf("Enter any key to continue");
getch();
}
OUTPUT:
9
Write
PROGRAM:7
a Program to accept sales amount from
salesman and calculate the
commission according to the following rules: If
sales amount lies b/w 2500-
10000 than commission is 10% of sales
amount. If sales amount lies b/w 10000-
25000 than commission is 20% of sales
amount If sales amount >25000 than
commission is 25% of sales amount.
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
float sales_amount, commission;
printf("Enter sales amount: ");
scanf("%f", &sales_amount);
if (sales_amount >= 2500 && sales_amount <= 10000)
{
commission = 0.1 * sales_amount;
}
else if (sales_amount > 10000 && sales_amount <=
25000)
{
commission = 0.2 * sales_amount;
}
else if (sales_amount > 25000)
{
10
commission = 0.25 * sales_amount;
} else
{
commission = 0; // No commission for sales
amount
less than 2500
}
printf("Commission: %f\n", commission);
printf("Enter any key to continue"); getch();
}
OUTPUT:
11
PROGRAM:8
Write a program to accept total no of hours work
by the worker.
Calculate the wages according to the following
wage rate.
Upto 8 hours Rs.
50/- Next 4 hours
Rs. 20/- per hour
Next 4 hours Rs. 25/- per hour
Next 4 hours Rs. 30/- per hour
Next 4 hours Rs. 50/- per hour
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{
clrscr(); int
hours_worked;
float wages;
printf("Enter the total number of hours worked: ");
scanf("%d", &hours_worked); if
(hours_worked <= 8)
{
wages = hours_worked * 50;
}
else if (hours_worked <= 12)
{
wages = 8 * 50 + (hours_worked - 8) * 20;
12
}
else if (hours_worked <= 16)
{
wages = 8 * 50 + 4 * 20 + (hours_worked - 12) * 25;
}
else if (hours_worked <= 20)
{
wages = 8 * 50 + 4 * 20 + 4 * 25 + (hours_worked - 16)
* 30;
} else
{
wages = 8 * 50 + 4 * 20 + 4 * 25 + 4 * 30 + (hours_worked
- 20) * 50;
}
printf("Total wages: %f\n", wages);
printf("Enter any key to continue");
getch();
}
OUTPUT:
13
PROGRAM:9
Write a program to accept a character from the
user and check whether that character is a digit
or alphabets or special character
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
14
printf("%c is an alphabet.\n", ch);
} else if (ch >= '0' && ch <=
'9')
{ printf("%c is a digit.\n", ch);
} else
{
printf("%c is a special character.\n", ch);
}
printf("Enter any key to continue");
getch();
}
15
PROGRAM:10
Write a program to find whether a given
character is a Vowel or consonant. A character is
taken as input. The character may be in Upper
Case or in Lower Case.
INPUT:
#include <stdio.h>
#include
16
OUTPUT:
<conio.h> int
main()
{ clrscr();
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z')
{
ch = ch + 32;
} if (ch == 'a'||ch =='e'||ch =='i'||ch == 'o'||ch ==
'u')
{
printf("%c is a vowel.\n", ch);
} else { printf("%c is a consonant.\
n", ch);
}
printf("Enter any key to continue");
getch();
}
17
PROGRAM:11
Write a program to accept a number from
user and check whether that number is +ve,
-ve or zero.
INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr()
;
int i;
printf("Enter a number:");
scanf("%d",&i);
if(i<=0)
{
if(i==0)
{
printf("You enter a zero");
18
OUTPUT:
}
else
{
printf(" you enter a negative number");
}
}
else
{
printf("you enter a positive a number");
}
printf("\nEnter any key to continue");
getch();
}
19
PROGRAM:12
Write a program to accept basic salary from the
user and calculate the gross salary. If basic salary
is less than 1500 then HRA is 10% of basic salary
and DA is 25% of Basic Salary. If basic salary
>=1500 than HRA is equal to 500/- Rs. And DA is
50% of basic Salary.
INPUT:
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr()
;
float basic_salary, HRA, DA,
gross_salary; printf("Enter basic salary:
"); scanf("%f", &basic_salary); if
(basic_salary < 1500)
{
HRA = 0.1 * basic_salary;
DA = 0.25 * basic_salary;
}
else
{
HRA = 500;
DA = 0.5 * basic_salary;
}
20
OUTPUT:
gross_salary = basic_salary + HRA + DA;
printf("Gross salary: %.2f\n",
gross_salary); printf("Enter a key to
continue..."); getch();
}
21
PROGRAM:13
Write a program to accept a year from the user
and check whether that year is a leap year or
not.
INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr(); int year;
printf("Enter a
year:");
scanf("%d",&year);
if(year%4==0)
{
printf("%d is an leap year",year);
}
else
{
printf("%d is not an leap year",year);
}
printf("\nEnter any key to continue");
getch();
}
OUTPUT:
22
PROGRAM:14
Write a program to accept 3 numbers from the
user and find out the largest number among
them.
INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2,num3;
printf("Enter a different number1: ");
scanf("%d",&num1);
printf("Enter a different number2: ");
scanf("%d",&num2);
printf("Enter a different number3:
"); scanf("%d",&num3);
if(num1>num2 && num1>num3)
{
printf("%d is the largest number",num1);
}
if(num2>num1 && num2>num3)
{
printf("%d is the largest number",num2);
}
if(num3>num1 && num3>num2)
{
printf("%d is the largest number",num3);
}
23
printf("\nEnter any key to continue");
getch();
}
OUTPUT:
24
PR
OG
RA
M:
15
Write a program to generate first 10 terms of
natural numbers.
INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr()
;
int i;
printf("The first ten natural numbers:");
for(i=1;i<=10;i++)
{
printf("\n %d",i);
}
printf("\nEnter any key to continue");
getch();
}
OUTPUT:
25
PROGRAM:16
Write a program to generate n
terms of Fibonacci series.
INPUT:
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr()
;
int f=0,f1=1,f2,limit,count;
printf("Enter the limit to generate fibonacci series:\
n"); scanf("%d",&limit); printf("Fibonacci series is. \
n"); printf("%d\n",f); printf("%d\n",f1); count=2;
while(count<limit)
{
f2=f1+f2;
count++;
printf("%d",f2);
26
f=f1;
f1=f2;
}
printf("\nEnter any key to continue");
getch();
}
27
to
28
OUTPUT:
PRO
GRA
M:1
7
Write a program calculate factorial of a
number.
INPUT:
#include<stdio.h> #include<conio.h>
void main()
{ clrscr(); long int fact=1,n; printf("Enter the
numbers:\n"); scanf("%ld",&n);
if(n<=0)
{
fact=1;
} else
{
for(int i=1;i<=n;i++)
{ fact=fact*i;
}
}
printf("factorial of %ld!=%ld\n",n,fact); printf("\nEnter
any key to continue");
getch();
}
29
to
30
OUTPUT:
PROGRAM:18
Write a programaccept a number and
calculate the sum of digits.
INPUT:
#include<stdio.h> #include<conio.h>
void main()
{ clrscr();
long int n,sod=0,rem,org;
printf("Enter a number(N):");
scanf(" %ld",&n); org=n;
while(n!=0)
{
rem= n % 10; sod=
sod+rem;
n/=10;
}
if (org==0)
{
printf("0");
org/=10;
} else
{
printf("sum of digit =%ld", sod);
}
printf("\nEnter any key to continue"); getch();
}
31
to
32
OUTPUT:
PROGRAM:18
Write a rogram accept a number and
calculate the total no of digits in the
number.
INPUT:
#include
<stdio.h>
#include
<conio.h> int
main() {
int num, count = 0;
printf("Enter a number:
"); scanf("%d", &num);
while (num != 0)
{
num /= 10;
count++;
}
printf("Total no. of digits in the number: %d\n",
count); printf("Enter a key to continue"); getch();
}
OUTPUT:
33
to
34
PROGRAM:20
Write a program to accept a number and find
out reverse of a number..
INPUT:
#include<stdio.h>
void main()
{ clrscr(); long int n,rev=0,rem;
printf("Enter a number(N):");
scanf(" %ld",&n); while(n>0)
{
rem= n % 10; rev=
rev*10+rem; n/=10;
}
printf("The reverse of number is%ld",rev);
printf("\nEnter any key to continue"); getch();
}
Output:
PROGRAM:21
Write a pogram to accept a number and check
whether that number is prime or not.
35
INPUT:
#include<stdio.h> #include<conio.h>
void main()
{ clrscr(); int num,i,flag=0;
printf("enter a number:");
scanf(" %d",&num);
for(i=2;num/2>=i;i++)
{
if(num%i==0)
{flag=1;
break;}
}
if(flag==0)
{
printf("%d is a prime number",num);
} else
{
printf("%d is not a prime number",num);
}
printf("\nEnter any key to continue"); getch();
}
Output:
36
PROGRAM:22
Write a program to accept n numbers from the user
and find out the second largest among them.
INPUT:
#include <stdio.h>
#include <conio.h> int
main()
{ clrscr();
int n, i, j, temp; int
arr[100];
printf("Enter the number of elements: "); scanf("%d",
&n);
printf("Enter %d numbers:\n", n); for (i =
0; i < n; i++) {
scanf("%d", &arr[i]);
37
} for (i = 0; i < n - 1; i++)
{ for (j = 0; j < n - i - 1; j++)
{ if (arr[j] < arr[j + 1])
{
temp = arr[j]; arr[j] =
arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("The second largest number is: %d\n", arr[1]);
printf("Enter any key to continue"); getch();
}
Output:
PROGRAM:23
38
Write a program to accept n numbers from the
user and find out how many are +ve, -ve and
zero.
INPUT:
#include <stdio.h>
#include <conio.h> int
main()
{ clrscr(); int n, i, num, positive = 0, negative = 0,
zero = 0; printf("Enter the number of numbers: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter number %d: ", i + 1);
scanf("%d", &num); if
(num > 0)
{
positive++;
}
else if (num < 0)
{
negative++;
} else
{
zero++;
}
}
printf("Positive numbers: %d\n", positive);
printf("Negative numbers: %d\n", negative);
39
printf("Zero numbers: %d\n", zero); printf("Enter
any key to continue"); getch();
}
Output:
PROGRAM:24
Write a Program to accept n and r from user
and find out n raise to the power r.
INPUT:
#include <stdio.h>
#include <conio.h> int
main()
{ clrscr(); int n, r, result
= 1;
40
printf("Enter the value of n: "); scanf("%d",
&n);
printf("Enter the value of r: ");
scanf("%d", &r);
for (int i = 0; i < r; i++)
{ result *= n;
}
printf("%d raised to the power %d is %d\n", n, r, result);
printf("Enter any key to continue"); getch();
}
Output:
PROGRAM:25
Write a Program to accept a character in lower case and
convert it into upper case.
INPUT:
#include<stdio.h> #include<conio.h>
void main()
{ char c;
int ascii;
printf("Enter any character:");
scanf("%c",&c); ascii=c-32;
41
printf("\n %c in uppercase represented as %c",c,ascii);
printf("\n\nEnter any key to continue"); getch();
}
Output:
PROGRAM:26
Write a program to generate first 10 terms of natural
numbers.
INPUT:
#include <stdio.h> #include <conio.h>
int main()
{ clrscr(); int i, j, k;
for (i = 1; i <= 3; i++)
{ for (j = 1; j <= 3; j++)
{
for (k = 1; k <= 3; k++)
{
printf("%d%d%d\n", i, j, k);
}
}
}
printf("Enter any key to continue..."); getch();
}
42
Output:
PROGRAM:27
Write a program to print days of a week
corresponding to the nos 1 to 7 using switch
case.
INPUT:
#include <stdio.h>
#include <conio.h> int
main()
{ clrscr(); int
day;
43
printf("Enter a number (1-7): ");
scanf("%d", &day); switch (day)
{ case 1:
printf("Monday\n");
break;
case 2: printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4: printf("Thursday\n");
break;
case 5: printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input\n");
}
printf("Enter any key to continue..."); getch();
}
Output:
44
PROGRAM:28
Write a program to accept a number from the
user and check whether that number is a
palindrome or not.
INPUT:
#include<stdio.h> #include<conio.h>
void main()
{ clrscr();
45
long int n,rev=0,rem,org;
printf("Enter a number(N):");
scanf(" %ld",&n); org=n;
while(n!=0)
{
rem= n % 10; rev=
rev*10+rem; n/=10;
}
if (org==rev)
{
printf("This number %d is palindrome",org);
} else
{
printf("This number %d is not palindrome",org);
}
printf("\nEnter any key to continue");
getch();
}
Output:
46
PROGRAM:29
Write a program to accept an array and find out
sum of all the elements of the array.
INPUT:
#include <stdio.h>
#include <conio.h> int
main() { clrscr(); int n,
sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n); int arr[40];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of the elements: %d\n", sum);
printf("Enter any key to continue..."); getch();
}
Output:
PROGRAM:30
47
Write a program to accept an array and find out
largest element from them.
INPUT:
#include <stdio.h> #include
<conio.h>
void main()
{ clrscr();
int i, n, largest;
printf("Enter the number of elements: ");
scanf("%d", &n); int arr[40];
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
largest = arr[0]; for (i=
1; i <n; i++)
{ if (arr[i] > largest)
{ largest = arr[i];
}
}
printf("Largest element: %d\n", largest);
printf("Enter any key to continue..."); getch();
}
Output:
48
49
PROGRAM:31
Write a program an array and print them in
reverse order.
INPUT:
#include <stdio.h> #include
<conio.h> int main() {
clrscr(); int n, i; int
arr[100];
printf("Enter the number of elements: "); scanf("%d", &n);
printf("Enter %d numbers:\n", n); for (i = 0; i <
n; i++)
{
scanf("%d", &arr[i]);
}
printf("Array in reverse order:");
for (i = n - 1; i >= 0; i--)
{
printf("\n%d ", arr[i]);
}
printf("\nEnter any key to continue"); getch();
}
Output:
50
51
PROGRAM:32
program to accept two array and find
sum of two array.
INPUT:
#include <stdio.h>
#include <conio.h>
void main()
{ clrscr();
int n, i;
int arr1[100], arr2[100], sum[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter elements for the first array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter elements for the second array:\n"); for (i
= 0; i < n; i++)
{
scanf("%d", &arr2[i]);
}
for (i = 0; i < n; i++)
{
sum[i] = arr1[i] + arr2[i];
}
printf("Sum of the arrays:\n");
for (i = 0; i < n; i++)
{
52
Write a out
printf("%d\n", sum[i]);
}
printf("Enter any key to continue"); getch();
}
Output:
PROGRAM:33
program to accept 3*3 matrix and find
the diagonal elements from the matrix.
INPUT:
#include <stdio.h>
#include
53
<conio.h> int
main()
{ clrscr(); int
i, j, n = 3;
int matrix[3][3];
printf("Enter elements for the 3x3 matrix:\n");
for (i = 0; i < n; i++)
{ for (j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("Diagonal elements:\n");
for (i = 0; i < n; i++) {
printf("%d ", matrix[i][i]);
}
printf("Enter any key to continue");
getch();
}
Output:
54
Write a out
55
PROGRAM:34
Program to accept m*n matrix find the
sum of diagonal elements.
INPUT:
#include <stdio.h>
#include <conio.h>
int main()
{ clrscr();
int m, n, i, j, sum = 0;
printf("Enter the number of rows and columns:
"); scanf("%d %d", &m, &n); int matrix[100]
[100];
printf("Enter the elements of the matrix:\n");
for (i = 0; i < m; i++)
{ for (j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < m; i++)
{ for (j = 0; j < n; j++)
{ if (i == j)
{
sum += matrix[i][j];
}
}
}
printf("Sum of diagonal elements: %d\n", sum);
printf("Press Enter key to continue..."); getch();
56
Write a out
}
Output:
57
PROGRAM:35
Write a Program to accept m*n matrix and
find out sum of all the elements of the
matrix.
INPUT:
#include <stdio.h> #include
<conio.h>
void main()
{ clrscr(); int m, n, i, j, sum =
0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n); int matrix[100][100];
printf("Enter the elements of the matrix:\n");
for (i = 0; i < m; i++)
{ for (j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
sum += matrix[i][j];
}
}
printf("Sum of all elements: %d\n", sum);
printf("Press Enter key to continue..."); getch();
}
Output:
58
Write a to accept
59
PROGRAM:36
program m*n matrix and find out the
transpose of the
matrix.
INPUT:
#include <stdio.h>
#include <conio.h>
int main()
{ clrscr(); int
m, n, i, j;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n); int matrix[100][100],
transpose[100][100]; printf("Enter the elements
of the matrix:\n");
for (i = 0; i < m; i++) { for
(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < n; i++) { for
(j = 0; j < m; j++) {
transpose[i][j] = matrix[j][i];
}
}
printf("Transpose of the matrix:\n");
for (i = 0; i < n; i++)
{ for (j = 0; j < m; j++)
{
printf("%d ", transpose[i][j]);
60
Write a to accept
}
printf("\n");
}
printf("Press Enter key to continue..."); getch();
}
Output:
PROGRAM:37
program m*n matrix and find out sum
of each row and each column.
INPUT:
61
#include <stdio.h>
#include
<conio.h> int
main()
{ clrscr();
int m, n, i, j, row_sum, col_sum;
printf("Enter the number of rows and columns:
"); scanf("%d %d", &m, &n); int matrix[100]
[100];
printf("Enter the elements of the matrix:\n");
for (i = 0; i < m; i++) { for
(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Sum of each row:\
n"); for (i = 0; i < m; i++)
{ row_sum = 0;
for (j = 0; j < n; j++) {
row_sum += matrix[i][j];
}
printf("Row %d: %d\n", i + 1, row_sum);
}
62
Write a to accept
printf("Column %d: %d\n", j + 1, col_sum);
}
printf("\nPress Enter key to continue..."); getch();
}
Output:
63
Write a Program to accept
PROGRAM:38
2 m*n matrix and find out the addition of 2
matrix.
INPUT:
#include <stdio.h>
#include <conio.h>
void main()
{ clrscr(); int m, n,
i, j;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix1[100][100], matrix2[100][100],
sum[100][100];
printf("Enter the elements of the first matrix:\n");
for (i = 0; i < m; i++) { for (j = 0; j < n; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (i = 0; i < m; i++) { for (j = 0; j < n; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (i = 0; i < m; i++) { for
(j = 0; j < n; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
64
Write a Program to accept
}
printf("Sum of the matrices:\n"); for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
printf("Press Enter key to continue..."); getch();
}
Output:
PROGRAM:39
a string and find out the length of the string.
INPUT:
65
#include <stdio.h>
#include <conio.h>
#include
<string.h> int
main()
{ clrscr();
char str[100];
66
Write a Program to accept
PROGRAM:40
a string and copy the string to another
string.
INPUT:
#include <stdio.h>
#include <conio.h>
#include
<string.h> int
main()
{ clrscr();
char str1[100], str2[100];
printf("Enter a string: "); scanf("%[^\
n]", str1); strcpy(str2, str1);
printf("Copied string: %s\n", str2);
printf("Press Enter key to
continue..."); getch();
} Output:
PROGRAM:41
a string and find out the total no of words in
67
Write a Program to accept
the string
INPUT:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{ clrscr(); char str[100]; int i,
word_count = 0; printf("Enter a
string: "); scanf("%[^\n]", str); for
(i = 0; str[i] != '\0'; i++) { if (str[i]
== ' ' && str[i + 1] != ' ')
{ word_count++;
}
}
word_count++;
printf("Total number of words: %d\n", word_count);
printf("Press Enter key to continue..."); getch();
}
Output:
PROGRAM:42
68
Write a Program to accept
a string in upper case and covert it into
lower
case .
INPUT:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{ clrscr(); char
str[100];
printf("Enter a string in uppercase: "); scanf("%[^\n]",
str);
for (int i = 0; str[i] != '\0'; i++)
{ str[i] = tolower(str[i]);
}
printf("String in lowercase: %s\n", str);
printf("Press Enter key to continue...");
getch();
}
Output:
69
PROGRAM:43
program find out the factorial of a
number using function.
INPUT:
#include <stdio.h> #include
<conio.h>
int factorial(int n)
{ if (n == 0)
{
return 1;
} else { return n *
factorial(n - 1);
}
}
void main()
{ clrscr();
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num); if (num < 0)
{ printf("Factorial is not defined for
negative
numbers.\n");
} else { int result =
factorial(num);
printf("Factorial of %d = %d\n", num, result);
}
70
Write a to
printf("Press Enter key to continue...");
getch();
}
Output:
71
PROGRAM:44
Programswap two numbers using
functions.
INPUT:
#include <stdio.h> #include
<conio.h> void swap(int *a,
int *b) {
int temp = *a; *a =
*b;
*b = temp;
}
void main() {
clrscr();
int x, y;
printf("Enter two integers: "); scanf("%d
%d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y); printf("After swapping: x = %d, y =
%d\n", x, y); printf("Press Enter key to continue...");
getch();
}
Output:
72
Write a to
PROGRAM:45
Write a Program to find out the area of
rectangle using functions
INPUT:
#include <stdio.h>
#include <conio.h>
float area_of_rectangle(float length, float width) {
return length * width;
}
void main()
{ clrscr();
float length, width, area;
printf("Enter the length and width of the rectangle:
");
scanf("%f %f", &length, &width); area
= area_of_rectangle(length, width);
printf("Area of the rectangle = %.2f sq. units\n",
area); printf("Press Enter key to continue...");
getch();
}
Output:
73