--------
if..else
--------
#include<stdio.h>
using namespace std;
main()
{
int a,b;
a=2;
b=3;
if(a>b)
{
printf("a is greater than b\n");
}
else
{
printf("b is greater than a\n");
}
}
---------------
switch..case
---------------
#include<stdio.h>
using namespace std;
main()
{
int op;
float a;
float b;
float result;
while(1)
{
printf("Select the Arithmetic Operation: \n 1 - Addition\n 2 - Subtraction\n 3 - Multiplication\n 4 - Division\n \n \n");
scanf("%d", &op);
printf("Enter first number: ");
scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
switch (op)
{
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
if (b == 0)
{
printf("Cannot divide by zero.");
}
result = a / b;
break;
default:
{
printf("invalid choice");
break;
}
}
printf("The result is: %f\n",result);
printf("\n");
}
}
-------------
while loop
-------------
#include<stdio.h>
using namespace std;
main()
{
char ch;
printf("Enter your choice:\n");
scanf("%c",&ch);
while(ch!='y')
{
printf("No\n");
printf("No\n");
printf("No\n");
printf("No\n");
break;
}
}
if..else
--------
#include<stdio.h>
using namespace std;
main()
{
int a,b;
a=2;
b=3;
if(a>b)
{
printf("a is greater than b\n");
}
else
{
printf("b is greater than a\n");
}
}
---------------
switch..case
---------------
#include<stdio.h>
using namespace std;
main()
{
int op;
float a;
float b;
float result;
while(1)
{
printf("Select the Arithmetic Operation: \n 1 - Addition\n 2 - Subtraction\n 3 - Multiplication\n 4 - Division\n \n \n");
scanf("%d", &op);
printf("Enter first number: ");
scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
switch (op)
{
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
if (b == 0)
{
printf("Cannot divide by zero.");
}
result = a / b;
break;
default:
{
printf("invalid choice");
break;
}
}
printf("The result is: %f\n",result);
printf("\n");
}
}
-------------
while loop
-------------
#include<stdio.h>
using namespace std;
main()
{
char ch;
printf("Enter your choice:\n");
scanf("%c",&ch);
while(ch!='y')
{
printf("No\n");
printf("No\n");
printf("No\n");
printf("No\n");
break;
}
}
No comments:
Post a Comment