Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday 10 March 2022

20.if-else conditional control structure

 

if-else conditional control structure:              

 

The simple if control structure is used to solve any problem with any number of possibilities.

There should be as much number of conditions as the number of possibilities.

if-else control structure is used to solve any problem with only two possibilities by writing a single condition.

According to the syntax

If the condition is true, then the statement or block of statements under if conditional statement is executed, otherwise the statement or block of statements under else part is executed.

At any given time either if or else part is executed.

Both if and else parts of if-else control structure are never executed at once.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:


#include<stdio.h>
void main()
{
int n;
if(10<20)
{
printf("one");
printf("four");
}
else
printf("three");
}

Output:

One

Four

 

Example explained:

(10<20) returns true, hence the if part printf("One"); is executed and else part printf("Two"); is skipped from the execution.

(0) is false, hence the if part printf("Three"); is not executed and else part printf("Four"); is executed.

 

Let us practice some examples:

 

Specification 1:

Accept any number and print whether the number is an even or odd

Program


#include<stdio.h>
void main()
{
int n;
printf("enter n value");
scanf("%d",&n);
if(n%2==0)
printf("Even number");
if(n%2==1)
printf("Odd number");
}
OUTPUT:
enter n value:3
odd number

EXAMPLE::
#include<stdio.h>
main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n%2==0)
     printf("Even number");
else
     printf("Odd number");
}

 Execution 1:

Enter any number: 25

Odd number

 Execution 2:

Enter any number: 12

Even number

 

Specification 2:

Accept two numbers and print whether they are equal or not equal.

 Program:


#include<stdio.h>
void main()
{
int a,b;
if(a==b)
   printf("Equal");
if(a!=b)
   printf("Not equal");
}
OUTPUT:
equal

EXAMPLE:
#include<stdio.h>
main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
if(a==b)
    printf("Equals");
else
    printf("Not equals");
}
 
 Execution 1:

Enter two numbers:

25    45

Not equals

 Execution 2:

Enter two numbers:

25     25

Equals

 

Specification 3:

Accept the age of a person and print whether has right to vote or not.

 

Program:


#include<stdio.h>
main()
{
int age;
if(age><18 a="" age="" as="" d="" if="" no="" nter="" of="" person:="" printf="" right="" scanf="" the="" to="" vote="">=18)
  printf("Has right to vote");
else
  printf("Has no right to vote");
}

Execution 1:

Enter the age of a person: 45

Has right to vote

 Execution 2:

Enter the age of a person: 15

Has no right to vote

 

Specification 4:

Accept any year and print whether the given year is a leap year or not.

 

Program:


#include<stdio.h>
void main()
{
int year;
if(year%4==0)
   printf("Leap year");
if(year%4!=0)
   printf("Not a leap year");
}
OUTPUT::
leap year
EXAMPLE :
#include<stdio.h>
main()
{
int year;
printf("Enter the year:");
scanf("%d",&year);
if(year%4==0)
   printf("Leap year");
else
   printf("Not a leap year");
}
 
Execution 1:

Enter the year: 1975

Not a leap year

Execution 2:

Enter the year: 2000

Leap year

 

Specification 5:

Accept any three digit number and print whether the number is a palindrome number or not.

 

Logic:

If the reverse of a number is equal to the same number then the number is called palindrome number.

In case of a three digit number the first and third digits must be equals.

 

Program:


#include<stdio.h>
void main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==third)
printf("Palindrome number");
if(first!=third)
printf("Not a palindrome number");
}
OUTPUT::
enter any three digit number:353
palindrome number

EXAMPLE :
#include<stdio.h>
main()
{
int n;
printf("enter any three digits number");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==third)
   printf("Palindrome number");
else
   printf("Not a palindrome number");
}

Execution 1:

Enter any three digit number: 343

Palindrome number


 Execution 2:

Enter any three digit number: 345

Not a palindrome number

 

Specification 6:

Accept any four digit number and print whether the number is a palindrome number or not.


Program:


#include<stdio.h>
void main()
{
int n,first,second,third,fourth,rev,temp;
printf("Enter any four digit number:");
scanf("%d",&n);
temp=n;              
fourth=n%10;
n=n/10;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
rev=fourth*1000+third*100+second*10+first*1;
if(temp==rev)
printf("Palindrome number");
if(temp!=rev)
printf("Not a palindrome number");
}
OUTPUT:: 
enter any four digits number:5445
palindrome number
#include<stdio.h>
main()
{
int n,first,second,third,fourth,rev,temp;
printf("Enter any four digit number:");
scanf("%d",&n);
temp=n;              
fourth=n%10;
n=n/10;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
rev=fourth*1000+third*100+second*10+first*1;
if(temp==rev)
   printf("Palindrome number");
else
   printf("Not a palindrome number");
}

Execution 1:

Enter any four digit number: 2345

Not a palindrome number

 

Execution 2:

Enter any four digit number: 2442

Palindrome number

 

Example explained:

In case of a four digit number, it is not enough to compare first and fourth digits.

Hence we need to compare the actual number with its reverse.

"n" will become zero while separating individual digits from the number.

That is why the value of "n" is stored in "temp" to compare with reverse later.

 

 

Specification 7:

Accept the salary, sales of a sales person and print the salary, commission and the gross salary. Grant 7% commission if the sales exceeds to 35000 otherwise grant 3% commission.

Program:

#include<stdio.h>
void main()
{
int salary,sales;
float comm.,gross;
printf("Enter the salary:");
scanf("%ld",&salary);
printf("Enter the sales:");
scanf("%ld",&sales);
if(sales>35000)
   comm.=(float)sales*7/100;
   else
   comm.=(float)sales*3/100;
gross=salary+comm.;
printf("Salary %f",salary);
printf("\nCommission %f",comm.);
printf("\nGross salary %f",gross);
}

Execution 1:

Enter the salary: 15000

Enter the sales: 48000

Salary 15000

Commission 3360

Gross salary 18360

 

Execution 2:

Enter the salary: 10000

Enter the sales: 32000

Salary 10000

Commission 960

Gross salary 10960

 

Specification 8:

Accept the salary of an employee and print the HRA, DA and gross salary. If salary exceeds 10000 then HRA is 15% of salary and DA is 25% of salary. If salary is less than or equal to 10000 then HRA is 10% of salary and DA is 15% of salary.

 

Program:
#include<stdio.h>
void main()
{
long int sal;
float hra,da,gross;
printf("Enter the salary:");
scanf("%ld",&sal);
 if(sal>10000)
 {
  hra=(float)sal*15/100;
  da=(float)sal*25/100;
  }
else
 {
     hra=(float)sal*10/100;
     da=(float)sal*15/100; 
 }
gross=sal+hra+da;
printf("Salary %f",sal);
printf("\nHouse Rent Allowance %f",hra);
printf("\nDearness Allowence %f",da);
printf("\nGross salary %f",gross);
}

Execution 1:

Enter the salary: 15000

House Rent Allowance 2250

Dearness Allowance 3750

Gross salary 21000

 

Execution 2:

Enter the salary: 7500

House Rent Allowance 750

Dearness Allowance 1125

Gross salary 9375

 

 

Specification 9:

Accept any three digit number and print whether number is an Armstrong number or not.

 

Logic:

If the sum of cubes of individual digits is equal to the given number then the number is called Armstrong number.

Examples: 153, 370, 371, 407

 

 

 

 


Program:


#include<stdio.h>
void main()
{
 int n,first,second,third,sum,temp;
 printf("Enter any three digit number:");
scanf("%d",&n);
temp=n;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
sum=first*first*first+second*second*second+third*third*third;
if(sum==temp)
   printf("Armstrong number");
else
   printf("Not an Armstrong number");
}

 

Execution 1:

three digit number: 153

Armstrong number

 

Execution 2:

Enter any three digit number: 654

Not an Armstrong number

 

Specification 10:

Accept the pervious, current month readings of energy meter and print the number of units and bill. If number of units exceeds 500 then charge 5.75 per unit otherwise charge 3.25 per unit.

 

Program:



#include<stdio.h>
void main()
{
int cmr,pmr,nu;
float bill;
printf("Enter the current month reading:");
scanf("%d",&cmr);
printf("Enter the previous month reading:");
scanf("%d",&pmr);
nu=cmr-pmr;
if(nu>500)
   bill=nu*5.75;
else
   bill=nu*3.25;
printf("No of units %d\nTotal bill %f",nu,bill);
}
 

Execution 1:

Enter the current month reading: 500

Enter the previous month reading: 200

No of units 300

Total bill 975

 

Execution 2:

Enter the current month reading: 1500

Enter the previous month reading: 800

No of units 700

Total bill 4025

0 comments:

Post a Comment