Showing posts with label if-else. Show all posts
Showing posts with label if-else. Show all posts

Wednesday, 25 May 2011

if-else Statement

It is a conditional expression and easy to use.


#include<stdio.h> 
#include<conio.h>
void main()
{
 char ch;
 clrscr();
 ch=getche();
 if(ch=='y')
 printf("\nYou typed y");
 else
 printf("\nYou didn't type y");
 getch();
}


In the above code when you type 'y', the output will be "You typed y" other it will print "You didn't type y".


If you have any question, you can ask me freely!

Wednesday, 18 May 2011

Create a Calculator by using if-else statement

Just copy the code...


#include<stdio.h>
#include<conio.h>
void main (void)
{
 char ch;
 float num1,num2;
 clrscr();
 printf("What do you want to do?\n");
 printf("Addition, Subtraction, Multiplication or Division:");
 ch=getche();
 printf("\nEnter first number:");
 scanf("%f",&num1);
 printf("\nEnter second number:");
 scanf("%f",&num2);
 printf("\n\n");


if(ch=='A')   
 printf("%f %c %f = %.2f",num1,+,num2,num1+num2);

if(ch=='S')   
 printf("%f %c %f = %.2f",num1,-,num2,num1-num2);
if(ch=='M')   
 printf("%f %c %f = %.2f",num1,*,num2,num1*num2);
if(ch=='D')   
 printf("%f %c %f = %.2f",num1,/,num2,num1/num2);
getch();  



}


What is this?