Saturday, April 14, 2012

Reverse Floyds Triangle

output
7 8 9 10
4 5 6
2 3
1


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
i=7;
for(j=3;j>=0;j--)
{
for(k=0;k<=j;k++)
{
printf("%d ",(i+k));
}
i=i-j;
printf("\n");
}
getch();
}

Area and Perimeter of Rectangle

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,l,p;
clrscr();
printf(“enter the l and b value:\n”);
scanf(%f%f”,&l,&b);
a=l*b;
p=2*(l+b);
printf(“area=%f\n perimeter=%f\n”,a,p);
getch();
}


Output:
enter the l and b value:
6
8
area=48.000000
perimeter=28.000000

Area and Perimeter of Square

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the 2 numbers”);
scanf(%d%d”,&a,&b);
(a>b?printf(“a is greater”):printf(“b is greater”));
getch();
}

Output:
enter the two numbers
6
3
a is greater

Greatest of two Numbers - Conditional Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the 2 numbers”);
scanf(%d%d”,&a,&b);
(a>b?printf(“a is greater”):printf(“b is greater”));
getch();
}

Output:
enter the two numbers
6
3
a is greater

Menu Driven Calculator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
clrscr();
printf(“\n1.add\n2.subtract\n3.multiply\n4.division\n5.remainder\n);
printf(“\nenter your choice\n”);
scanf(%d”,&ch);
switch(ch)
{
case1:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a+b;
printf(“\nthe answer is %d”,c);
break;
case2:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a-b; 
printf(“\nthe answer is %d”,c);
break;
case3:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a*b; 
printf(“\nthe answer is %d”,c);
break;
case4:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a/b; 
printf(“\nthe answer is %d”,c);
break;
case5:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a%b; 
printf(“\nthe answer is %d”,c);
break;
default:
printf(“\nenter the correct choice”);
break;
}
getch();
}

Output:
1.add
2.subtract
3.multiply
4.division
5.remainder
enter your choice
2
enter the values  of a and b
7
4
the answer is 3

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

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

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

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