0% found this document useful (0 votes)
28 views14 pages

#Include Stdio.h (N, Sum (, N) (N) (I I N I) ( (,i) (, (I) ) Sum (I) ) (,sum) )

Uploaded by

mails.amandaala
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)
28 views14 pages

#Include Stdio.h (N, Sum (, N) (N) (I I N I) ( (,i) (, (I) ) Sum (I) ) (,sum) )

Uploaded by

mails.amandaala
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/ 14

Lab:5

1.
1. #include<stdio.h>

int main()
{
int n,sum;
printf("Enter the size of array. ");
scanf("%d",&n);
int a[n];

for(int i=0;i<n;i++)
{
printf("Enter at %d index = ",i);
scanf("%d",&a[i]);
sum+=a[i];
}

printf("Sum of entered arrays is %d",sum);


return 0;
}

2.
#include<stdio.h>

int main()
{
int n,x,count=0;
printf("Enter the size of array. ");
scanf("%d",&n);
int a[n];

for(int i=0;i<n;i++)
{
printf("Enter at %d index = ",i);
scanf("%d",&a[i]);
}

printf("Enter the element to find in this array = ");


scanf("%d",&x);

for(int i=0;i<n;i++)
{
if(x==a[i])
count++;
}

printf("No. %d appeared %d times in this array",x,count);


return 0;
}

3.
#include<stdio.h>

int main()
{
int n,x,y;
printf("Enter the size of array. ");
scanf("%d",&n);
int a[n];

for(int i=0;i<n;i++)
{
printf("Enter at %d index = ",i);
scanf("%d",&a[i]);
}

printf("Array Old ");


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

printf("\nEnter the index to entered new element = ");


scanf("%d",&x);
printf("\nEnter the no. to be replaced on %d index = ",x);
scanf("%d",&y);

a[x]=y;

printf("\n\nArray New ");


for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
4.
#include<stdio.h>

int main()
{
int n,x,pos,found=0;
printf("Enter the size of array. ");
scanf("%d",&n);
int a[n];

for(int i=0;i<n;i++)
{
printf("Enter at %d index = ",i);
scanf("%d",&a[i]);
}

printf("\nEnter the element to search in array = ");


scanf("%d",&x);

for(int i=0;i<n;i++)
{
if(a[i]==x)
{
pos=i;
found=1;
}
}
if(found==0)
printf("%d is not in this array",x);
else
printf("%d is at index %d of the array ",x,pos);
return 0;
}

5.
#include<stdio.h>

int main()
{
int n,x;
printf("Enter the size of array. ");
scanf("%d",&n);
int a[n];

for(int i=0;i<n;i++)
{
printf("Enter at %d index = ",i);
scanf("%d",&a[i]);
}

printf("\nEnter the element to search in array = ");


scanf("%d",&x);

int start=0;
int end=n-1;

while(start<=end)
{
int mid=(start+end)/2;
if(a[mid]==x)
{
printf("%d is found at index %d",x,mid);
return 0;
}
else if(x<a[mid])
end=mid-1;
else
start=mid+1;
}

printf("not");
return 0;
}

7.
#include<stdio.h>
int main ()
{
int n,min,temp;
printf("Enter the number of element: ");
scanf("%d",&n);
int a[n];

printf("Enter the elements of an array: ");


for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

}
printf("The array is : ");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

8.
#include<stdio.h>
int main ()
{
int n,min,temp;
printf("Enter the number of element: ");
scanf("%d",&n);
int a[n];
printf("Enter the elements of an array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min = j;
}
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
printf("The array is : ");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

9.
#include<stdio.h>
int main ()
{
int n,min,temp,i,j;
printf("Enter the number of element: ");
scanf("%d",&n);
int a[n];

printf("Enter the elements of an array: ");


for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;

}
printf("The array is : ");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

LAB 6:

1.
//sum of matrix
#include<stdio.h>
int main()
{
int a[3][3] , b[3][3] , sum[3][3];

printf("\nEnter the elements in 1st array \n\n");


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("Enter at col %d and row %d = ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}

printf("\nEnter the elements in 2nd array\n\n");


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("Enter at col %d and row %d = ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}

for(int i=0;i<3;i++)
{
printf("\n");
for(int j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("%5d ",sum[i][j]);
}
}

return 0;
}

2.
//Multiplication of matrix
#include<stdio.h>
int main()
{
int a[3][3] , b[3][3] , mul[3][3];

printf("\nEnter the elements in 1st array \n\n");


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("Enter at col %d and row %d = ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}

printf("\nEnter the elements in 2nd array\n\n");


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("Enter at col %d and row %d = ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}

for(int i=0;i<3;i++)
{
printf("\n");
for(int j=0;j<3;j++)
{
mul[i][j]=0;
for(int k=0;k<3;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
printf("%5d ",mul[i][j]);
}
}

return 0;
}

Lab 7:
1. #include<stdio.h>
void circle(int);
void square(int);
void rectangle(int,int);
void traingle(int,int,int);

int main()
{
int n,a,b,c;
char ch;
do
{
printf("Enter which area and perimeter you want to find.\n");
printf("1 for circle.\n2 for square.\n3 for rectangle.\n4 for
traingle.\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("Enter the radius = ");
scanf("%d",&a);
circle(a);
break;

case 2: printf("Enter the length = ");


scanf("%d",&a);
square(a);
break;

case 3: printf("Enter the Length = ");


scanf("%d",&a);
printf("Enter the breath = ");
scanf("%d",&b);
rectangle(a,b);
break;

case 4: printf("Enter the 1st side = ");


scanf("%d",&a);
printf("Enter the 2nd side = ");
scanf("%d",&b);
printf("Enter the 3rd side = ");
scanf("%d",&c);
traingle(a,b,c);
break;
}
printf("\nEnter \"y\" to find again.\n");
scanf(" %c",&ch);
}while(ch=='y');
return 0;
}

void circle(int a)
{
int perimeter=2*3.14*a;
int area=3.14*a*a;
printf("\Circle");
printf("\n Perimeter = %d\n Area = %d\n", perimeter,area);
}

void square(int a)
{
int perimeter=4*a;
int area=a*a;
printf("\nSquare");
printf("\n Perimeter = %d\n Area = %d\n", perimeter,area);
}

void rectangle(int a,int b)


{
int perimeter=2*(a+b);
int area=a*b;
printf("\nRectangle");
printf("\n Perimeter = %d\n Area = %d\n", perimeter,area);
}

void traingle(int a,int b,int c)


{
int perimeter=a+b+c;
int s=perimeter/2;
int area=pow((s-a)*(s-b)*(s-c),1/2);
printf("\nTraingle");
printf("\n Perimeter = %d\n Area = %d\n", perimeter,area);
}

2.
#include<stdio.h>
void word(int);

int main()
{
int n,rev=0,a;
printf("Enter no to print in words.\n");
scanf("%d",&n);
while(n!=0)
{
a=n%10;
n/=10;
rev=rev*10+a;
}
word(rev);
return 0;
}

void word(int z)
{
char v[10][6]={"zero ","one ","two ","three","four ","five
","six ","seven","eight","nine "};
int b=z%10;
int c=z/10;
printf(" %s ",v[b]);
if(c==0)
return;

word(c);

Lab 9:

1. #include<stdio.h>
int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

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

fact = factorial(number);
printf("Factorial of %d is %d\n", number, fact);
return 0;
}

2.
#include <stdio.h>

int fibonacci(int num)


{

if (num == 0)
{
return 0;
}

else if (num == 1)
{
return 1;
}

else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}

int main()
{
int num;
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num);

for (int i = 0; i < num; i++)


{
printf("%d, ", fibonacci(i));
}

return 0;
}

3.
#include <stdio.h>

int power(int x, int n)


{

if (n == 0)
return 1;

if (x == 0)
return 0;

return x * power(x, n - 1);


}

int main()
{
int x = 2;
int n = 3;

printf("%d\n", power(x, n));


}

You might also like