Professional Documents
Culture Documents
2.1 To
calculate the area and perimeter of rectangle and area and
circumference of a circle. The length and breadth of the rectangle and radius of
the circle are input through the keyboard.
2.2 Temperature of a city degree Fahrenheit is entered through the keyboard. Write
a C program to convert this temperature to degree Celsius.
Flowchart/Algorithm:
C Program
Source Code:
Exp:2.1
#include<stdio.h>
int main()
{
//declare variable
int l, b, AeraR=0, Peri=0;
float r, AreaC=0.0, circm=0.0;
//input
printf("\n Enter the value of length, breadth of the rectangle \n");
scanf("%d %d",&l, &b);
printf("\n Enter the value of radius of the circle \n");
scanf("%f",&r);
//process
AeraR=l*b;
Peri=2*(l+b);
AreaC=3.14*r*r;
circm=3.14*r;
//output
printf("\n\n Area of rectangle with length =%d breadth =%d is %d",l,b,AeraR);
printf("\t and perimeter of rectangle is %d \n\n",Peri);
printf("\n\n Area of circle with radius=%f is %f",r,AreaC);
printf("\t and circumference of circle is %f",circm);
return 0;
}
Exp:2.2
/* Fahrenheit to Celsius
*/
#include<stdio.h>
int main()
{
float c, f;
printf("enter temperature in Fahrenheit \n");
scanf("%f",&f);
c=(f-32)*5/9;
Exp:2.3
/* Simple interest
*/
#include<stdio.h>
int main()
{
int n;
float p, r, si;
printf("enter numbers of years \n\n");
scanf("%d",&n);
printf("enter Principal Amount \n\n");
scanf("%f",&p);
printf("enter Rate of Interest \n\n");
scanf("%f",&r);
si= (n* p* r)/100;
printf("Simple Interest = %f",si);
return 0;
}
Exp:2.4
/* To volume of sphere
*/
#include<stdio.h>
#define PI 3.14
int main()
{
float r,v;
printf("enter radius of sphere \n\n");
scanf("%f",&r);
v=(4 * PI * r * r * r)/3;
printf("Volume of sphere= %f",v);
return 0;
}
Output:
Exp:2.1
O/P:
Enter the value of length, breadth of the rectangle
12 4
Area of rectangle with length =12 breadth =4 is 48 and perimeter of rectangle is 32
Exp:2.2
O/P:
enter temperature in Fahrenheit
77
Exp:2.3
O/P:
enter numbers of years
5
enter Principal Amount
100000
enter Rate of Interest
3.25
Simple Interest = 16250.000000
Exp:2.4
O/P:
enter radius of sphere
5
Volume of sphere= 523.333313
Flowchart/Algorithm:
C Program
Source Code:
3.1
/* To interchange
*/
#include<stdio.h>
int main()
{
int a,b,t=0;
printf("to interchange the value of any to numbers \n");
printf("Enter the value of a \n");
scanf("%d",&a);
printf("Enter the value of b \n");
scanf("%d",&b);
printf("\n before interchange the value a= %d and the value of b=%d ",a,b);
t=a;
a=b;
b=t;
printf("\n\n after interchange the value of a =%d and the value of b =%d",a,b);
return 0;
}
3.2
#include<stdio.h>
#include<math.h>
int main()
{
float a,sr;
printf("to find the square root of any number\n");
printf("Enter the value of a\n");
scanf("%f",&a);
sr=sqrt(a);
printf("\nthe square root of %f is = %f",a,sr);
return 0;
}
3.3
#include<stdio.h>
#include<math.h>
int main()
{
float a,x,r1,r2,v;
printf("find the value of the below expression for the given value of X\n");
printf("Enter the value of X\n");
scanf("%f",&a);
x=sqrt(a);
r1=pow(x+1,6);
r2=pow(x+1,6);
v=r1+r2;
printf("\nthe value of x is %f and the answers is = %f",a,v);
return 0;
}
3.4
#include<stdio.h>
int main()
{
float a,b;
printf("Print the maximum number given any two numbers\n");
printf("Enter the first number\n");
scanf("%f",&a);
printf("Enter the second number\n");
scanf("%f",&b);
(a>b)?printf ("%f is a maximum number\n",a):printf ("%f is a maximum number\n",b);
return 0;
}
Output:
3.1
O/P:
3.2
O/P:
to find the square root of any number
Enter the value of a
4
find the value of the below expression for the given value of X
3.4
O/P:
Print the maximum number given any two numbers
Enter the first number
25
Enter the second number
40
40.000000 is a maximum number
4.1 Write a C program that will print whether a given number is positive, negative or
zero using a conditional operator.
4.2 Write a C program that prints appropriate messages on the screen based
on user input character.
If Message
pressed...
A Excellent
B Good
C Fair
D Poor
Flowchart/Algorithm:
C Program
Source Code:
4.1
#include<stdio.h>
int main()
{
float a,b;
printf("print whether a given number is positive, negative or zero\n");
printf("Enter the first number\n");
scanf("%f",&a);
(a>0)?printf ("%f is a positive number\n",a):(a<0)?printf ("%f is a negative number\n",a):printf ("%f
is a zero\n",a);
return 0;
}
4.2
#include<stdio.h>
int main()
{
char a;
printf("Print the appropriate messages on the screen based on user input character\n");
printf("Enter the grade between \'A\' to \'D\'\n");
scanf("%c",&a);
if(a=='A')
{
printf("Excellent");
}
else if(a=='B')
{
printf("Good");
}
else if(a=='C')
{
printf("Fair");
}
else if(a=='D')
{
printf("Poor");
}
else
{
printf("Enter the appropriate grade");
}
return 0;
}
4.3
#include<stdio.h>
int main()
{
char marital_status,gender;
int age;
printf("To check if driver is married or unmarried \n");
printf("Enter the marital status \'m\'-> Married and \'u\'-> Unmarried\n");
scanf("%c",&marital_status);
printf("\nEnter the gender \'M\'-> Male and \'F\'-> Female\n");
scanf(" %c",&gender);
printf("\nEnter the age\n");
scanf("%d",&age);
if(marital_status=='m')
{
printf("\nDriver is Married");
printf("\nthe driver is insured");
}
else if(marital_status=='u' && gender =='M' && age >=30)
{
printf("\ndriver is Unmarried , Male and above 30yrs of age");
printf("\nthe driver is insured");
}
else if(marital_status=='u' && gender =='F' && age >=25)
{
printf("\n driver is Unmarried , Female and above 25yrs of age");
printf("\nthe driver is insured");
}
else
{
printf("\nthe driver is not insured");
}
return 0;
}
Output:
4.1
O/P:
print whether a given number is positive, negative or zero
Enter the first number
4
4.000000 is a positive number
4.2
O/P:
Print the appropriate messages on the screen based on user input character
Excellent
4.3
O/P:
5.1 Write a C program that will print whether a given number is even or odd.
5.2 Write a C program to check divisibility of a number entered by the user.
5.4 Write a C program that prints the smallest one given any 3 different numbers.
Marks Message
40-60 Pass
80-100 Distinction
Any Other Invalid Marks Input
Flowchart/Algorithm:
C Program
Source Code:
5.1
#include<stdio.h>
int main()
{
int a;
printf("to check if a given number is even or odd\n");
printf("Enter a number\n");
scanf("%d",&a);
if(a%2==0)
{
printf("%d is a even number",a);
}
else
{
printf("%d is a odd number",a);
}
return 0;
}
_________________________________________________________________________
___
5.2
#include<stdio.h>
int main()
{
int a,b;
printf("to check divisibility of a number entered by the user\n");
printf("Enter a Dividend \n");
scanf("%d",&a);
printf("Enter a Divisor number\n");
scanf("%d",&b);
if(a%b==0)
{
printf("%d is divisible by %d",a,b);
}
else
{
printf("%d is not divisibility by %d",a,b);
}
return 0;
}
_________________________________________________________________________
___
5.3
#include<stdio.h>
int main()
{
char a;
printf("to check if a character is vowel or a consonant\n");
printf("Enter a character \n");
scanf("%c",&a);
if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U')
{
printf("vowel");
}
else
{
printf("consonant");
}
return 0;
}
_________________________________________________________________________
_
5.4
#include<stdio.h>
int main()
{
int a,b,c;
printf("to check the number is smallest one given any 3 different number\n");
printf("Enter a first number \n");
scanf("%d",&a);
printf("Enter a second number \n");
scanf("%d",&b);
printf("Enter a third number \n");
scanf("%d",&c);
if(a<b)
{
if(a<c)
{
printf("%d is smallest",a);
}
}
else
{
if(b<c)
{
printf("%d is smallest",b);
}
else
{
printf("%d is smallest",c);
}
}
return 0;
}
______________________________________________________________________
5.5
#include<stdio.h>
int main()
{
int a;
printf("To check the grade\n");
printf("Enter marks out of 100\n");
scanf("%d",&a);
if(a<=40)
{
printf("Fail");
}
else if(a<=60&&a>40)
{
printf("Pass");
}
else if(a<=80&&a>60)
{
printf("First Class");
}
else if(a<=100&&a>80)
{
printf("Distinction");
}
else
{
printf("Enter the appropriate marks");
}
return 0;
}
_________________________________________________________________________
5.6
#include <math.h>
#include <stdio.h>
int main() {
float a, b, c, d, r1, r2;
printf(" to calculate the root of a Quadratic Equation\n");
printf("Enter coefficients a: ");
scanf("%f",&a);
printf("Enter coefficients b:");
scanf("%f",&b);
printf("Enter coefficients c: ");
scanf("%f",&c);
if (d > 0) {
r1 = (-b - sqrt(d)) / (2 * a);
r2 = (-b + sqrt(d)) / (2 * a);
printf("root1 = %.2f and root2 = %.2f", r1, r2);
}
else if (d == 0) {
r1 = -b / (2 * a);
printf("root1 = root2 = %.2f;", r1);
}
else {
printf("then no real roots exist");
}
return 0;
}
_________________________________________________________________________
__
5.7
#include<stdio.h>
int main()
{
int x,y;
printf("in which quadrant the coordinate point lies\n");
printf("Enter the x quadrant:");
scanf("%d",&x);
printf("Enter the y quadrant:");
scanf("%d",&y);
return 0;
}
Output:
Exp:5.1
O/P:
to check if a given number is even or odd
Enter a number
4
4 is a even number
Exp:5.2
O/P:
to check divisibility of a number entered by the user
Enter a Dividend
10
Enter a Divisor number
5
10 is divisible by 5
Exp:5.3
O/P:
to check if a character is vowel or a consonant
Enter a character
a
vowel
Exp:5.4
O/P:
to check the number is smallest one given any 3 diffiernt number
Enter a first number
4
Enter a second number
2
Enter a third number
3
2 is smallest
Exp:5.5
O/P:
To check the grade
Enter marks out of 100
99
Distinction
Exp:5.6:
O/P:
to calculate the root of a Quadratic Equation
Enter coefficients a: 1
Enter coefficients b:-7
Enter coefficients c: -60
root1 = -5.00 and root2 = 12.00
Exp:5.7
O/P:
in which quadrant the coordinate point lies
Enter the x quadrant:2
Enter the y quadrant:-4
quadrant second
DECISION MAKING AND LOOPING
Expt No.6 Date: 15/11/2021
ROLLNO:B21616
6.1 Write a C program to print the sum and reverse of any given 4-digit number
using a while loop.
6.3 Write a C program to print the following series for N>1 using a for loop:
C Program
Source Code:
6.1
#include<stdio.h>
int main()
{
int R,rev=0,n,sum=0;
printf("enter the number to print sum\n");
scanf("%d",&n);
while(n!=0)
{
int R=n%10;
sum=sum+R;
rev=rev*10+R;
n=n/10;
}
printf("\n The sum of digits is %d",sum);
printf("\nNumber in reverse is %d",rev);
return 0;
}
6.2
#include<stdio.h>
int main()
{
int i=1,n;
printf("enter the n number\n");
scanf("%d",&n);
do
{
printf("%d ",i);
i++;
}
while(i<=n);
6.3
#include<stdio.h>
int main()
{
int N;
printf("Enter the N number\n ");
scanf("%d",&N);
for ( int i =1; i<=N ;i++ )
{
if(i==N)
{
printf("%d/%d! ",i,i);
}
else
{
printf("%d/%d! + ",i,i);
}
}
return 0;
}
6.4
A)
#include<stdio.h>
int main()
{
}
printf("\n");
}
return 0;
}
B)
#include<stdio.h>
int main()
{
int k=5;
printf("nesting for loop\n");
}
printf("\n");
}
return 0;
}
C)
#include<stdio.h>
int main()
{
printf("nesting for loop\n");
for ( int i =1; i<=5 ;i++)
{
for ( int j =1; j<=i ;j++ )
{
printf(" ");
}
for ( int j =5; j>=i ;j-- )
{
printf("$");
}
printf("\n");
}
return 0;
}
int main()
{
printf("nesting for loop\n");
printf("\n");
}
return 0;
}
Output:
Exp6.1:
o/p:
enter the number to print sum
1234
Exp6.3:
O/P:
Enter the N number
5
1/1! + 2/2! + 3/3! + 4/4! + 5/5!
Exp:6.4:
a)
nesting for loop
1
12
123
1234
12345
b)O/P:
nesting for loop
5
44
333
2222
11111
c)
O/P:
nesting for loop
$$$$$
$$$$
$$$
$$
$
ROLLNO:B21616
1. Factorial of a number.
2. Prime or not.
3. Odd or even.
4. Exit.
Flowchart/Algorithm:
C Program
Source Code:
7.1
#include<stdio.h>
int main(){
int ch,n,i,m=0,flag=0;
char c;
do
{
printf("press 1 to Factorial of a number \n");
printf("press 2 to Prime or not\n");
printf("press 3 to Odd or even \n");
printf("Enter the operation\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter a number\n");
scanf("%d",&n);
int fact=1,i=1;
for ( ;i<=n;i++)
{
fact= fact*i;
}
printf("%d! = %d",n,fact);
break;
case 2:printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("%d is not prime",n);
flag=1;
break;
}
}
if(flag==0)
{
printf("Number is prime");
}
break;
case 3:printf("\n Enter a number\n");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is a even number",n);
}
else
{
printf("%d is a odd number",n);
}
break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)");
scanf(" %c",&c);
}while(c=='y');
return 0;
}
7.2
#include<stdio.h>
int main(){
int ch,d,a,b,q=0,g=0,r=0;
char c;
do
{
printf("press 1 to Quotient and Remainder of division \n");
printf("press 2 to Reverse of a given number\n");
printf("press 3 to Find the greatest of 3 \n");
printf("press 4 to Illustration of increment and decrement operators \n\n");
printf("Enter the operation\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter two number\n");
scanf("%d %d",&a,&b);
q=a/b;
r=a%b;
printf("\nquotient of %d and %d is %d",a,b,q);
printf("\nRemainder of %d and %d is %d",a,b,r);
break;
case 2:printf("\n Enter a number\n");
scanf("%d",&a);
while(a>0)
{
int s=a;
r=a%10;
g=g*10+r;
a=a/10;
}
printf("Reverse is %d\n",a,g);
break;
case 3:printf("\n Enter 3 number\n");
scanf("%d %d %d",&a,&b,&d);
(a>b && a>d)?printf("%d is greater than %d and %d ",a,b,d):
(b>d && b>a)?printf("%d is greater than %d and %d ",b,a,d):
printf("%d is greater than %d and %d ",d,a,b);
break;
case 4:printf("\n Enter a number\n");
scanf("%d",&a);
r=a;
q=a;
printf("\nincrement of %d is %d",a,++r);
printf("\ndecrement of %d is %d",a,--q);
break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)\n");
scanf(" %c",&c);
}while(c=='y');
return 0;
}
7.3
#include<stdio.h>
int main(){
int ch,r,s;
char c;
do
{
printf("press 1 to Area of circle \n");
printf("press 2 to Area of square\n");
printf("press 3 to Area of sphere \n\n");
printf("Enter the operation\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter a radius\n");
scanf("%d",&r);
float c= 3.14*r*r;
printf("Area of circle is %.2f",c);
break;
case 2:printf("Enter side:");
scanf("%d",&s);
int sq = s*s;
printf("Area of square is %d",sq);
break;
case 3:printf("\n Enter a radius\n");
scanf("%d",&r);
float sp= 4*3.14*r*r;
printf("Area of sphere is %.2f",sp);
break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)\n");
scanf(" %c",&c);
}while(c=='y');
Output:
Exp.7.1:
O/P:
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
1
Enter a number
4
4! = 24
Do you want to continue? (Press y/n)y
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
2
Enter the number to check prime:5
Number is prime
Do you want to continue? (Press y/n)y
press 1 to Factorial of a number
press 2 to Prime or not
press 3 to Odd or even
Enter the operation
3
Enter a number
4
4 is a even number
Do you want to continue? (Press y/n)n
Exp7.2:
O/P:
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators
quotient of 6 and 2 is 3
Remainder of 6 and 2 is 0
Do you want to continue? (Press y/n)
y
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators
Enter a number
1234
Reverse is 4321
Enter 3 number
237
7 is greater than 2 and 3
Do you want to continue? (Press y/n)
y
press 1 to Quotient and Remainder of division
press 2 to Reverse of a given number
press 3 to Find the greatest of 3
press 4 to Illustration of increment and decrement operators
increment of 7 is 8
decrement of 7 is 6
Do you want to continue? (Press y/n)
n
Exp7.3
O/P:
press 1 to Area of circle
press 2 to Area of square
press 3 to Area of sphere
Enter a radius
4
Area of circle is 50.24
Do you want to continue? (Press y/n)
y
press 1 to Area of circle
press 2 to Area of square
press 3 to Area of sphere
Enter a radius
4
Area of sphere is 200.96
Do you want to continue? (Press y/n)
n
Functions in C
Expt No.8 Date:
7/12/2021
ROLLNO:B21616
C Program
Source Code:
8.1
#include<stdio.h>
void squareOfNumber();
void main()
{
int number;
printf("Please Enter any integer Value : ");
scanf("%d", &number);
squareOfNumber(number);
return 0;
}
void squareOfNumber(int num)
{
int sque;
sque = num*num;
printf("square of a given number %d is = %d", num, sque);
}
8.2
#include<stdio.h>
void vote();
int main()
{
int age;
printf(" enter age\n");
scanf("%d",&age);
vote(age);
return 0;
}
void vote(int a)
{
if(a>=18)
{
printf("\nEligible to vote");
}
else
{
printf("not eligible to vote");
}
}
8.3
#include<stdio.h>
void rect();
int main()
{
int a,b;
8.4
#include<stdio.h>
void gcd();
int main()
{
int a,b;
gcd( a,b);
return 0;
}
void gcd(int j,int k)
{
int Q, R=1;
while(R!=0)
{
Q=j/k;
R=j-Q*k;
j=k;
k=R;
}
printf("GCD is %d",j);
8.5
#include<stdio.h>
void Fido();
int main()
{
int n1=0,n2=1,n3;
Fibo(n1,n2,n3);
return 0;
}
void Fibo(int a,int b,int c)
{
int i=1;
printf("%d,%d,",a,b);
while(i<c){
int n4=a +b;
a=b;
b=n4;
printf("%d,",n4);
i++;}
Output:
Exp8.1:
Exp8.2:
enter age
19
Eligible to vote
Exp8.3
Enter the length of the rectangle
23
Enter the breadth of the rectangle
Exp8.4:
Enter the two number
1980 1617
GCD is 33
Exp8.5:
Enter the number
12
0,1,1,2,3,5,8,13,21,34,55,89,144,
Index 0 1 2 3 4 5
A[6] 7 9 12 6 2 1
Flowchart/Algorithm:
\
C Program
Source Code:
9.1
#include<stdio.h>
int main()
{
printf(" Program to access and print an array of 5 elements.\n");
int arr[5];
printf("Enter the 5 elements of the array\n");
for(int i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("entered elements are\n");
for(int i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
9.2
#include<stdio.h>
int main()
{
printf(" 10 numbers are entered from a keyboard into an array. \nThe number to be
searched is entered through the keyboard by the user. \nWrite a C program to find if the
number to be searched is present in the array and if it is present, \ndisplay the number of
times(frequency) it appears in the array.\n");
int arr[10];
int count=0,searchs;
printf("\n\nEnter the 10 elements of the array\n");
for(int i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
printf("enter the number to be search : ");
scanf("%d",&searchs);
for(int i=0;i<10;i++)
{
if(searchs == arr[i])
{
count++;
}
}
if(count==0)
printf("Not present= %d ",searchs);
else
printf(" %d present in the array %d times",searchs,count);
}
9.3
#include<stdio.h>
int main()
{
printf("Write a C program to count the positive and negative integers in a given array.\n\
n");
int n;
int countpos=0,countneg=0;
printf("enter the size if the elements :");
scanf("%d",&n);
int arr[n];
printf("\n\nEnter the elements of the array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++)
{if(arr[i]>0)
{
countpos++;
}
else if(arr[i]<0)
{
countneg++;
}
}
printf("the total positive are is %d and negative is%d",countpos,countneg);
}
Exp9.4:
// Online C compiler to run C program online
#include <stdio.h>
int main() {
// Write C code here
//printf("Hello world");
int a[6];
int b[6],c[6];
printf("enter the 6 elements=\n ");
for(int i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
printf("\n\n");
printf("a[6]= ");
for(int i=0;i<6;i++)
{
printf(",%d ",a[i]);
}
printf("\n\n");
printf("b[6]= ");
for(int i=0;i<6;i++)
{
b[i]=a[i]*a[i];
printf(", %d ",b[i]);
}
printf("\n\n");
printf("b[6]= ");
for(int i=0;i<6;i++)
{
c[i]=b[i]*a[i];
printf(", %d",c[i]);
}
return 0;
}
Output:
Exp9.1:
Program to access and print an array of 5 elements.
Enter the 5 elements of the array
12345
entered elements are
1 2 3 4 5
Exp9.2:
Exp9.3:
Write a C program to count the positive and negative integers in a given array.
Exp9.4:
enter the 6 elements=
7 9 12 6 2 1
a[6]= ,7 ,9 ,12 ,6 ,2 ,1
b[6]= , 49 , 81 , 144 , 36 , 4 , 1
10.2 Write a C program to print the largest number from any 5 row by 3
column matrix. Also print its position.
10.1
#include<stdio.h>
int main()
{
int SumEven=0,SumOdd=0;
int arr[3][3];
printf("enter the elements of the array\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf(" the elements are\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j]%2==0)
{
SumEven+=arr[i][j];
}
else
{
SumOdd+=arr[i][j];
}
}
}
printf("The Sum of Odd Elements = %d and sum of even elements=%d
",SumOdd,SumEven);
return 0;
}
10.2
#include<stdio.h>
int main()
{
int largest=0,row,col;
int arr[5][3];
printf("enter the elements of the array\n");
for(int i=0;i<5;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf(" the elements are\n");
for(int i=0;i<5;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
for(int i=0;i<5;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j]>largest)
{
largest=arr[i][j];
}
}
}
printf("largest element is %d ",largest);
return 0;
}
10.3
#include<stdio.h>
void main()
{
char ch;
char choice;
int c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
int i, j, a[10][10], Sum = 0;
int m, n, matrix[10][10], transpose[10][10];
int rows, columns, Multiplication[10][10], Number;
do{
printf("\n A. Addition And subtraction of matrices");
printf("\n B. To compute the sum of diagonals of an array.");
printf("\n C.To find the transpose of a given matrix. ");
printf("\n D. To find the scalar multiplication. \n");
printf("Select your choice\n");
scanf("%s",&ch);
switch(ch)
{
case 'A':
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
/*
printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
/*
printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
/*
finding the SUM of the two matrices
and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
break;
case 'B':
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );
break;
case 'C':
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter 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++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
break;
case 'D':
printf("\n Please Enter Number of rows and columns\n");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &Multiplication[rows][columns]);
}
}
printf("\n Please Enter the Multiplication Value : ");
scanf("%d", &Number);
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
Multiplication[rows][columns] = Number * Multiplication[rows][columns];
}
}
printf("\n The Result of a Scalar Matrix Multiplication is : \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("%d \t ", Multiplication[rows][columns]);
}
printf("\n");
}
break;
default:printf("Wrong choice!");
}
Output:
10.1:
enter the elements of the array
243
654
953
the elements are
2 4 3
6 5 4
9 5 3
The Sum of Odd Elements = 25 and sum of even elements=16
10.2:
enter the elements of the array
123
6 12 3
4 8 16
10 5 9
11 13 0
the elements are
1 2 3
6 12 3
4 8 16
10 5 9
11 13 0
largest element is 16
10.3:
O/P:
22
67
80
84
21
The first matrix is:
6 7
8 0
8 4
2 1
14 11
10 1
-2 3
6 -1
Strings in C
Expt No. 11 Date:
23/12/2021
11.3 To concatenate any two strings without the use of predefined function
Flowchart/Algorithm:
C Program
Source Code:
Exp11.1:
#include <stdio.h>
#include <string.h>
int main()
{
char string[20];
int count = 0;
printf("enter you name \n");
gets(string);
for(int i = 0; i < strlen(string); i++) {
if(string[i] != ' ')
count++;
}
printf("Total number of characters in a string: %d", count);
return 0;
}
Exp11.2:
#include <stdio.h>
#include <string.h>
int main()
{
Exp11.3:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
char str3[40];
printf("enter your first name \n");
gets(str1);
printf("enter your sceond name \n");
gets(str2);
int i = 0, j = 0;
printf("\nFirst string: %s", str1);
printf("\nSecond string: %s", str2);
// Insert the first string in the new string
while (str1[i] != '\0') {
str3[j] = str1[i];
i++;
j++;
}
// Insert the second string in the new string
i = 0;
while (str2[i] != '\0') {
str3[j] = str2[i];
i++;
j++;
}
str3[j] = '\0'; //Terminate the string str3
// Print the concatenated string
printf("\nConcatenated string: %s", str3);
return 0;
}
Exp11.4:
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Exp11.5
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,n,c=0;
printf("Enter the string : ");
gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
Output:
Exp11.1:
o/p:
enter you name
gildon ferreira
Total number of characters in a string: 14
Exp11.2:
o/p:
enter you name
gildon ferreira
Enter character to be searched: r
character 'r' occurs 3 times
Exp11.3:
o/p:
enter your first name
gildon
enter your sceond name
ferreira
First string: gildon
Second string: ferreira
Concatenated string: gildon ferreira
Exp11.4:
o/p:
Enter string s1: gildon ferreira
String s2: gildon ferreira
exp:11.5:
o/p:
Enter the string : wow
string is palindrome
Arrays in Functions
Expt No.12 Date:
04/01/2022
12.2 Write two functions that will reverse and sort the elements respectively of
any array.
Flowchart/Algorithm:
C Program
Source Code:
Exp12.1:
#include<stdio.h>
double searchElement();
void main()
{
int num[5];
int search;
printf("Please Enter any 5 integer Value : ");
for(int i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
printf("Please Enter the search element : ");
scanf("%d", &search);
searchElement(num,search);
}
double searchElement(int arr[],int s) {
Exp12.2:
#include<stdio.h>
void rev();
void sortasc();
void main()
{
int A[6];
printf("Please Enter any 6 integer Value : ");
for(int i=0;i<=5;i++)
{
scanf("%d",&A[i]);
}
rev(A);
sortasc(A);
}
void rev(int arr[]) {
printf("The elements in reverse order are\n");
for (int i = 5; i >=0; i--) {
printf("%d ",arr[i]);
}
}
Output:
12.1:
o/p:
Please Enter any 5 integer Value : 10 9 4 2 10
Please Enter the search element : 10
10 is present in the array
12.2:
o/p:
Please Enter any 6 integer Value : 5 8 45 6 -10 99
The elements in reverse order are
99 -10 6 45 8 5
sorted in ascending order
-10 5 6 8 45 99