1. WAP in c to input 5 elements in an array and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("\n enter aray elements\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n the entered values are: ");
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
getch();
}
output:
2. WAP in c to find sum and average age of 10 students.
#include<stdio.h>
#include<conio.h>
main()
{
int age[10],i,s=0;
clrscr();
float av;
printf("\n enter age of 10 students\n");
for(i=0;i<10;i++)
{
scanf("%d",&age[i]);
}
for(i=0;i<10;i++)
{
s=s+age[i];
}
av=s/10.0;
printf("\ntotal age = %d\nAverage age = %f",s,av);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i;
printf("\n enter aray elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\nall even numbers are:\n");
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
printf(" %d",a[i]);
}
}
getch();
}
Output:
4. WAP in c to add two 1D array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf("\n enter 5 elements in first array\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter 5 elements in second array\n");
for(i=0;i<5;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
}
printf("\nsum of two arrays are:\n");
for(i=0;i<5;i++)
{
printf(" %d",c[i]);
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
float a[10];
float max,min;
int i;
clrscr();
printf("\nenter 10 elements in an array:\n");
for(i=0;i<10;i++)
{
scanf("%f",&a[i]);
}
max=min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf("the minimum is %f and the maximum is %f",min,max);
getch();
}
Output:
6. LINEAR SEARCH
OR
SEQUENTIAL SEARCH
OR
WAP in C to search an element in an array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,n,f=0;
clrscr();
printf("\n enter 5 element\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the element to be searched: ");
scanf(" %d",&n);
for(i=0;i<5;i++)
{
if(a[i]==n)
{
printf("\n elements found at location %d",i+1);
f=1;
break;
}
}
if(f==0)
{
printf("\nElement not found");
}
getch();
}
Output:
Note: In binary search data should be in sorted order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20]={3,5,6,8,9,11,14,15,17,19,21,27,32,36,43,49,53,68,79,98},l=0,h=19,mid,i,n,f=0;
clrscr();
printf("\nArray elements are:\n ");
for(i=0;i<20;i++)
{
printf("%d ",a[i]);
}
printf("\nenter the element to be searched : ");
scanf("%d",&n);
while(l<=h)
{
mid=(l+h)/2;
if(a[mid]==n)
{
printf("\nFound");
f=1;
break;
}
else
{
if(a[mid]>n)
{
h=mid-1;
}
else
{
l=mid+1;
}
}
}
if(f==0)
{
printf("\nElement not found");
}
getch();
}
Output:
8. /*-------BUBBLE SORT-------*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("\n enter 10 elements in an array :");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\t the sorted array is : \n");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
getch();
}
Output:
9. /*----- Selection Sorting-----*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100], n, i, j, p, temp;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for ( i = 0 ; i < n ; i++ )
{
scanf("%d", &a[i]);
}
for ( i = 0 ; i < ( n - 1 ) ; i++ )
{
p = i;
for ( j = i + 1 ; j < n ; j++ )
{
if ( a[p] > a[j] )
{
p = j;
}
}
if(p!=i)
{
temp = a[i];
a[i] = a[p];
a[p] = temp;
}
}
printf("Sorted list:\n");
for ( i = 0 ; i < n ; i++ )
{
printf(" %d", a[i]);
}
getch();
}
Output:
10. WAP in c to convert decimal into binary.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],r,i=0,j,t,k,n;
clrscr();
printf("\n enter any numbar\n");
scanf("%d",&n);
while(n!=0)
{
r=n%2;
a[i]=r;
n=n/2;
i++;
}
k=--i;
j=0;
while(i>=j)
{
t=a[j];
a[j]=a[i];
a[i]=t;
j++;
i--;
}
printf("binary : ");
for(i=0;i<=k;i++)
{
printf(" %d",a[i]);
}
getch();
}
No comments:
Post a Comment