Tuesday, January 17, 2012

Searching the Element in an Array


#include<stdio.h>
#include<conio.h>
void main()
{
int j=0,n,x[5];
clrscr();
printf(“enter the elements of array\n”);
for(j=0;j<5;j++)
scanf(%d”,&x[j]);
printf(“enter the element to search\n”);
scanf(%d”,&n);
for(j=0;j<5;j++)
{
if(x[j]==n)
break;
}
if(x[j]==n)
printf(“element found”);
else
printf(“element not found”);
getch();
}

Output:
enter the elements of array:
1
2
3
4
5
enter the elements to search
3
element found

Find Maximum Value in Array


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,i;
clrscr();
printf(“enter elements for the array\n”);
for(i=0;i<5;i++)
scanf(%d”,&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf(“the maximum value is%d”,max);
getch();
}

Output:
enter the elements for array
4
6
3
8
5
the maximum value is 8 

Calculate Electric Energy Bill


#include<stdio.h>
#include<conio.h>
void main()
{
float r,a=2.5,b=3.5,c=1.5;
clrscr();
printf(“enter the readings\n”);
scanf(%f”,&r);
if(r>=200)
printf(“rupees=%f”,r*b);
else if((r>=100)&&(r<200))
printf(“rupees=%f”,r*a);
else
printf(“rupees=%f”,r*c);
getch();
}

Output:
enter the readings
140
rupees=350 

Decimal to Binary Conversion


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,r,sum=0,i=0;
clrscr();
printf(“\nenter the number\n”);
scanf(%d”,&no);
while(no>0)
{
r=no%2;
sum=sum+pow(10,i)*r;
no=no/2;
i++;
}
printf(“\nthe binary value is %d”,sum);
getch();
}

Output:
enter the number
8
the binary value is 1000