Decission Making

Decissional Programming.
In this kind of programming we implement different decissions making statements in order to confirm whether a task is to be done or not. following are the decission making statement supported by C.

Decissional programming-:
  1. if - statement
  2. if-else statement
  3. Switch case statement


The Conditional Operator


The if statement -:
The if statement puts a condition which is always in the form of realation and after the judgement of that condition either true or false c compiler is redirected to another group of instructions.

Syntax-:
void main
{
______
______
______
if (condition)
{
_______
_______
_______
}
________
________
________
getch();
}


Some Basic Programs.



Program to check which of the two numbers Entered is greater.

#include< stdio.h>
#include< conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two values");
scanf("%d%d",&a&b);
if(a>b)
{
printf("hi this is abc");
printf("the first value is greater");
}
printf("program ended");
printf("thanks for watching");
getch();
}

Program to check whether person is Senior Citizen or not.

#include< stdio.h>
#include< conio.h>
void main()
{
int a;
clrscr();
printf("Enter age of person");
scanf("%d",&a);
if(a>60)
{
printf("senior citizen");
}
printf("program ended");
printf("thanks for watching");
getch();
}

Program to check whether the character is capital letter or small.

#include< stdio.h>
#include< conio.h>
void main()
{
char ch1;
clrscr();
printf("Enter the character");
scanf("%c",&ch1);
if(ch1>=65&&ch1<=90)
{
printf("the character is in capital letter");
}
printf("program ended");
printf("thanks for watching");
getch();
}

Program to check the character corresponds to Male or Female.

#include< stdio.h>
#include< conio.h>
void main()
{
char ch1;
clrscr();
printf("Enter your gender m for male f for female");
scanf("%c",&ch1);
if(gender=='m')
{
printf("THE USER IS MALE");
}
printf("program ended");
printf("thanks for watching");
getch();
}


The if-else statement-:



When we have to handle both true and false conditions we have to use the if els statement.here,if handles the true case while else handles the false case.

Syntax-:
____________
____________
____________
____________
if (condition)
{
____________
____________
____________
}
else
{
____________
____________
____________
}
____________
____________
____________

Program to check which of the two numbers Entered is greater.

#include< stdio.h>
#include< conio.h>
void main()
{
int num1,num2;
clrscr();
printf("Enter two values");
scanf("%d%d",&num1&num2);
if(num1>num2)
{
printf("the first value is greater");
}
else
{
printf("second is greater");
}
printf("thanks for watching");
getch();
}

Program to check number is Even or Odd.


#include< stdio.h>
#include< conio.h>
void main()
{
int num1;
clrscr();
printf("Enter value to check it is even or odd");
scanf("%d",&num1);
if(num1%2==0)
{
printf("number is even");
}
else
{
printf("number is odd");
}
printf("thanks for watching");
getch();
}

Program to check that the number is pallindrome or not (only for three digit number).


#include< stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf("Enter value to check it is pallindrome or not");
scanf("%d",&num);
x=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
n=(a*100)+(b*10)+(num*1);
if(num==x)
{
printf("number is pallindrome");
}
else
{
printf("number is not pallindrome");
}
printf("thanks for watching");
getch();
}

Program to check whether the student is Pass or Fail depending or the criteria.


#include< stdio.h>
#include< conio.h>
void main()
{
int m1,m2,m3,m4,m5,t,p;
clrscr();
printf("Enter the marks of 5 students ");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
t=m1+m2+m3+m4+m5;
p=(t/500)*100;
if(p>=50)
{
printf("the student is pass");
}
else
{
printf("THE STUDENT IS FAIL");
}
printf("thanks for watching");
getch();
}

Program to Compute the Basic Salary of a person.

#include< stdio.h>
#include< conio.h>
void main()
{
long int ta,da,hra,basic;
printf("Enter the basic salary of employee");
scanf("%ld",&basic);
if (basic<=50000)
{
da=(15*basic)/100;
ta=(12*basic)/100;
hra=(12*basic)/100;
gs=ta+da+hra+basic;
printf("the gross salary is%ld",gs");
}
else
{
da=10000;
ta=8000;
hra=(18*basic)/100;
gs=ta+da+hra+basic;
printf("the gross salary is%ld",gs);
}
getch();
}




Use of logical operator in logical OR and in logical AND statement


Logical AND combines several conditions in such a way that the if part of if-else statement will execute only when all conditions are true.
Syntax-:
if(condition&&condition&&condition)
{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________
}

Logical OR in if statement combines several conditions in such a way that the if part will execute for any true condition.
the else statement executes only when all conditions are false.
Syntax-:
if(condition||condition||condition)

{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________
}


#include< stdio.h>
#include< conio.h>
void main()
{
char n,l;
int a;
clrscr();
printf("Enter your nationality if you are indian press i else press o");
scanf("%c",&n);
fflush(stdin);
printf("if you have driving license press y else press n");
scanf("%c",&l);
printf("Enter your age");
scanf("%d",&a);
if(n=='i'&&l=='y'&&a>18)
{
printf("elligible for job");
}
else
{
printf("not elligible for job");
}
getch();
}

Logical NOT-: when added to a condition in if else statement it works for exact opposite of that condition, while working with logical not in if else statement we must take care of our true and false blocks.
Syntax-:
if(NOT condition)
{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________ }

Without not Operator


if(age>18)
{
printf("elligible");
}
else
{
printf("not elligible");
}

With not Operator


if(age!>18)
{
printf(" NOT elligible");
}
else
{
printf(" elligible");
}

Working with logical AND,OR,and NOT together-:


In C language there is a priority system among logical operators. Thus according to priority Operator are arranges as follows.



Some Basic Program.



Program to Calculate whether the Entered year is a Leap year Or Not.


#include< stdio.h>
#include< conio.h>
void main()
{
char n,l;
int a;
clrscr();
printf("Enter the year to be checked as leap year");
scanf("%d",&y);
y=y%4;
if(l!=0)
{
printf("not a leap year");
}
else
{
printf("LEAP YEAR!!!!!!!!!");
}
getch();
}

Nesting of if-else statement


A simple if-else statement is designed to work with only two conditions at a time. The one is true and other is false but if we want to handle more than two conditions via if-else, we must nest this statement nesting can be acheived in two of the following ways.
  1. Nesting of true case
  2. Nesting of false case


Nesting of true case.

Here, the new if-else statement is nested inside of if section previous if else statement
Syntax
if( condition1)

{
if(condition)
{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________
}
}

else
{
____________
____________
____________
____________
}

Program to Calculate whether a person is eligible to vote or not


#include
#include
void main()
{
char n,l;
int a;
clrscr();
printf("Enter your nationality if you are indian press i else press o");
scanf("%c",&n);
fflush(stdin);
printf("if you have driving license press y else press n");
scanf("%c",&l);
printf("Enter your age");
scanf("%d",&a);
if(n=='i')
{
if(l=='y')
{
if(a>18)
{
printf("elligible ");
}
else
{
printf("not elligible ");
}
}
else
{
printf("not elligible ");
}
}
else
{
printf("not elligible ");
}
getch();
}

Program to check that the number is divisible by 6 or not


#include< stdio.h>
#include< conio.h>
void main()
{
int n;
clrscr();
printf("Enter the value to be checked");
scanf("%d",&n);
if(n%2==0)
{
if(n%3==0)
{
printf("DIVISIBLE BY 6!!!!");
}
else
{
printf("not divisible by 6");
}
}
getch();
}


Nesting of False case.


While working with nesting of false case the new if else statement is nested inside of the else block of previous if-else statement.

Syntax
if( condition1)

{

____________
____________
____________
____________
}
else
{
if(condition)

{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________
}
}

Program to whether the student is elligible for Admission or not


#include< stdio.h>
#include< conio.h>
void main()
{
int hin,eng,scs;
clrscr();
printf("Enter the students subject marks in hindi ");
scanf("%d",&hin);
printf("Enter the students subject marks in english");
scanf("%d",&eng);
printf("Enter the students subject marks in science");
scanf("%d",&scs);
if(hin>65)
{
if(eng>70&&eng<=80)
{
if(scs>90)
{
printf("Elligible for admission ");
}
else
{
printf("not elligible for admission");
}
}
else
{
printf("Not elligible for admission");
}
}
else
{
printf("not elligible for admission");
}
getch();
}


Program to whether the number is divisible or Not.


#include< stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number");
scanf("%d",&num);
x=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;

if(c%a==0)
{
if(b%a==0)
{
printf("divisible");
}
else
{
printf("not divisible");
}
}
else
{
printf("not divisible");
}
getch();
}


else-if ladder

The else-if ladder simplifies the nesting of else case .here,the nested if is wriiten along with else and thus every else becomes even simpler to understand
Syntax-:
if( condition1)

{

____________
____________
____________
____________
}
else if( condition1)
{
____________
____________
____________
____________

}
else if( condition1)
{
____________
____________
____________
____________
}
else
{
____________
____________
____________
____________
}


The goto statement

goto is a keyword that is a capable to change the natural direction of compilation .goto provides a jump onto a paricular label .this jump can either be forward or backward .a forward jump is not a problem at all but a backward jump can cause an infinite loop .

Syntax-:

Forward jump via goto-:
void main()
{
________
________
________
________
abc:
________
________
________
abc:
________
________
}


backward jump via goto-:
void main()
{
________
________
________

abc:
________
________
________
goto abc:
________
________
________
}

NOTE-:The infinite loop caused by backward goto can be handeled simply by placing the goto statement in a condition.

Switch case

Switch case is a way to implement menu driven programming .we first display a menu and then design a switch statement having several cases and a default case in it.now depending upon the choice any one case of the switch is executed.
Syntax:-
switch (switching variable)
{
case 1:
______
______
______
break;
case 2:
______
______
______
break;
case 3:
______
______
______
break;
default:
______
______
______
break;
}

Write a program that can tell the day of the week depending on the Choice between 1-7


#include< stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf("Enter a number from 1-7");
scanf("%d",&num);
switch(num)
{
case 1:
printf("sunday");
break;
case 2:
printf("monday");
break;
case 3:
printf("tuesday");
break;
case 4:
printf("wednesday");
break;
case 5:
printf("thursday");
break;
case 6:
printf("friday");
break;
case 7:
printf("saturday");
break;
default:
printf("wrong choice");
break;
}
getch();
}


Write a program that can perform Addition or Substraction dependig upon the choice.


#include< stdio.h>
#include< conio.h>
void main()
{
int num1,num2,choice,sum,difference;
clrscr();
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
printf("Enter your choice");
printf("Enter your choice");
printf("1->addition");
printf("2->substraction");
printf("Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
sum=num1+num2;
printf("the sum is %d",sum);
break;
case 2:
difference=num1-num2;
printf("the difference is %d difference");
break;
default:
printf("wrong choice");
break;
}
getch();
}

Write a program that can perform Addition, Substraction, Multiplication, Division, Modulus Division dependig upon the choice.


#include< stdio.h>
#include< conio.h>
void main()
{
int num1,num2,choice,sum,difference,product,result,mod,square,cube;
clrscr();
printf("Enter your choice");
printf("1->addition");
printf("2->substraction");
printf("3->multiplication);
printf("4->division");
printf("5->modulus division");
printf("6->square");
printf("7->cube");
printf("Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
sum=num1+num2;
printf("the sum is %d",sum);
break;
case 2:
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
difference=num1-num2;
printf("the difference is %d ",difference");
break;
case 3:
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
product=num1*num2;
printf("the product is %d ",product);
break;
case 4:
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
result=num1/num2;
printf("the result is %d ",result);
break;
case 5:
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
mod=num1%num2;
printf("the result is %d ",mod);
break;
case 6:
printf("Enter the first number");
scanf("%d",&square);
square=square*square;
printf("the result is %d ",square);
break;
case 7:
printf("Enter the first number");
scanf("%d",&cube);
cube=cube*cube*cube;
printf("the result is %d ",cube);
break;
default:
printf("wrong choice");
break;
}
getch();
}


Switches with characters:-


Syntax:-
char x;
switch (x)
{
case a:
______
______
______
break;
case b:
______
______
______
break;
case c:
______
______
______
break;
default:
______
______
______
break;
}

Program that can Tell a Word depending on the letter of Vowel Entered.


#include
#include
void main()
{
char ch;
clrscr();
printf("1-a\n2-e\n3-i\n4-o\n-u");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 'a':
printf("apple");
break;
case 'e':
printf("elephant");
break;
case 'i':
pintf("icecream");
break;
case 'o':
printf("owl");
break;
case 'u':
printf("umbrella");
break;
default:
printf("wrong choice");
break;
}
getch();
}

Program that can calculate area of circle, area of square, area of rectangle, area of triangle, area of cylinder depending upon the choice entered.


#include< stdio.h>
#include< conio.h>
void main()
{
char ch;
float r,area,side,length,breadth,base,height,h;
clrscr();
printf("Enter your choice");
printf("a->area of circle");
printf("b->area of square");
printf("c->area of rectangle");
printf("d->area of triangle");
printf("e->area of cylinder");
printf("Enter your choice");
scanf("%d",&ch)
switch(ch)
{
case a:
case A:
printf("Enter the radius of circle");
scanf("%d",&r);
area=pi*r*r;
printf("the area of circle is %d",area);
break;
case b:
case B:
printf("Enter the side of square");
scanf("%d",&side);
area=side*side;
printf("the area of square is %f",area);
break;
case c:
case C:
printf("Enter the length of rectangle");
scanf("%d",&length);
printf("Enter the breadth of rectangle");
scanf("%d",&breadth);
area=length*breadth;
printf("the area of rectangle is %f",area);
break;
case d:
case D:
printf("Enter the base of triangle");
scanf("%d",&base);
printf("Enter the height of triangle");
scanf("%d",&height);
area=(base*height)/2;
printf("the area of triangle is %f",area)
break;
case e:
case E:
printf("Enter the radius of cylinder");
scanf("%d",&r);
printf("Enter the height of cylinder");
scanf("%d",&h);
area=2*pi*r*h;
printf("the area of cylinder is %f",area)
break;
default:
printf("wrong choice");
break;
}
getch();
}

Nesting of switch


Syntax:-
switch (ch1)
{

case 1:
______
______
______
break;
case 2:
______
______
______
switch (ch2)
{
case 1:
______
______
______
break;
case 2:
______
______
______
break;
default:
______
______
______
break;
}
break;
case 3:
______
______
______
break;
default:
______
______
______
break;
}