Você está na página 1de 26

Solution of Exercise on If else TO WRITE ANY PROGRAM YOU SHOULD HAVE LOGIC .THEN WHAT IS LOGIC ?

IN C LANGUAGE THE LOGIC IS THE WAY YOU PROCEED.HERE I AM going to provide you logic of every question Logic .Making a mistake is depend upon you but to avoid some common mistakes I giving you Tool Basic mistakes c in my words .after all these I am going to provide you live image output which conform that the code given in ans has %0 error

1)Any integer is input by the user. Write a program to find out whether it is an odd number or even number Solution :-

#include<stdio.h> void main() { int x; printf("enter the number:-"); scanf("%d",&x); if(x%2==0) printf("enter number is even"); else printf("enter number is odd"); }

Logic If no is even after division it give remainder value as 0 and if no is odd its remainder is not zero.

Basic mistakes c in my words Note: we cant use reminder function in float .most of student forget this and error take place Live output image:

2) Find the absolute value of a number entered by the user. {Note: I just remined you that absolute value is nothing but modulus . i.e. |-3|=3 & |3|=3 }

solution:-

#include<stdio.h> void main() { int x; printf("enter the number:-\t"); scanf("%d",&x); if(x>0) printf("The absolute value of number is:-\t %d",x); else { x=-x; printf("The absolute value of number is:\t %d",x); } } Logic: We have to take modulus of givien number . if value is positive i.e. value is greater than zero then print it directly and if value is less than 0 it means the value is negative put the ve sign to the value to make it +ve { i.e. -(-a) =a } and print it.

Basic mistakes c in my words When you writing the program someone put printf( enter the number:- ) sometime it creat confusion between dash and ve sign .to avoid this I prefer to use the \t to give tab to enter the value . but it is not nessasery to write \t in exam . Live output image:

3) Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 5000. Solution:-

#include<stdio.h> void main() { float x,y,total_expense,dis; printf("enter the item price:-"); scanf("%f",&x); printf("enter the quantity:-"); scanf("%f",&y); total_expense= x*y; if(total_expense<5000) printf("The total expense is :-%f",total_expense); else { dis=10; total_expense=total_expense-total_expense*dis/100; printf("The total expense is :-%f",total_expense); } } Logic: Here the logic is if total expense is less than 5000 then print it directly. If total expense is more than 5000 then give the discount of 10%.

The formula for discount Dis= total-total*dis/100 Live output image:

5) find largest no amongs three number using if else. # include <stdio.h> void main() { int a, b, c ; printf("Enter three numbers : ") ; scanf("%d %d %d", &a, &b, &c) ; if(a > b) { if(a > c) printf("\n%d is the biggest number", a) ; else

printf("\n%d is the biggest number", c) ; } else { if(c > b) printf("\n%d is the biggest number", c) ; else printf("\n%d is the biggest number", b) ; } } Live output image:

4) Write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred. Cost price and selling price of an item is input by the user. Solution:#include<stdio.h> void main() { float x,y,result; printf("enter the cost price:-\t"); scanf("%f",&x); printf("enter the selling price :-\t"); scanf("%f",&y); result=y-x; if(result>0) { printf("profit\n"); printf("seller profit is :-\t%f",result);

} else { if(result<0) { printf("loss\n"); printf("seller loss is :-\t%f",-result);

} else { printf("No profit no loss"); } } }

Logic: If result is +ve then seller in profit and the seller s profit is nothing but profit .if result is ve then seller in loss and loss is result { i.e. (-result)=result }.is writing last statement is making any sence? Yes if the result is 0 then no profit and loss sometimes student forget this statement and you loss your marks .

Basic mistakes c in my words Be careful to putting the braces { } .see how I put them is systemetic way because the avoid the mistake of closing of brass { } .

Live output image:

5) If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of the three. #include<stdio.h> void main() { float x,y,z; printf("enter the ages of Ram:-"); scanf("%f",&x); printf("enter the Sulabh :-"); scanf("%f",&y); printf("enter the Ajay :-"); scanf("%f",&z); if(x<y)

{ if(x<z) { printf("ram is youngest of the three"); } else { printf("Ajay is youngest of the three"); } }

else { if(y<z) { printf("Sulabh is youngest of the three"); } else { printf("Ajay is youngest of the three"); } } }

Logic: The logic I used here is nesting of if .else .i used two times the nesting of loops. Case-1 (x>y) 1) if(x<y)-i.e ram is younger than Sulabh .if this condition is true than I used the nesting of if....else loop in which I give condition that if(x<z)- i.e ram is younger than Ajay.if this condition is true then it print ram is youngest of the three otherwise it print "Ajay is youngest of the three". Case-2 ( else)
1) ) if(x<y)- goes wrong then program jump to the else block.but here also I using nesting

of ifelse .if if(y<z)-i.e Sulabh is younger than Ajay then it print "Sulabh is youngest of the three" otherwise it print "Ajay is youngest of the three".

Live image output

Basic mistakes c in my words Looking at the size of code do you think that this is perfect code for given program ? No ,this is not perfect code for the program. there are several of code for one program and many progrmmer for one code but the ultimate winner is who write short simple and fastest code .i am not saying that is wrong code or writing this code you loss your marks .this is perfectly correct program gives perfect output .i just want you to take another aproch to program if possible Here an alternative- using logical operator

#include<stdio.h> void main() { float x,y,z; printf("enter the ages of Ram:-"); scanf("%f",&x); printf("enter the Sulabh :-"); scanf("%f",&y); printf("enter the Ajay :-"); scanf("%f",&z); if (x<y && x<z) printf("ram is youngest of the three"); else if (y<x && x<z) printf("Sulabh is youngest of the three"); else printf("Ajay is youngest of the three"); }

Live image output

6) Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered by the user. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

#include<stdio.h> void main() { float x,y,z,w; printf("enter the angle:-"); scanf("%f\n%f\n%f",&x,&y,&z); w=x+y+z; if(w==180) {

printf("triangle is valid"); } else { printf("triangle is invalid"); } } Logic: x+y+z=180

Live image output

7) Any year is input by the user. Write a program to determine whether the year is a leap year or not. Solution C in my word Student dont understand the logic of leap year they think that leap year is 366 days it is not sufficent to write program we have to obsorve the parrten in which leap year procced i am giving some rule that help you to understand the concept of leap year hand help you to write the program
Rule 1: A year is called leap year if it is divisible by 400. For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year. Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year. For example: 2004, 2008, 1012 are leap year.

This is the way to think #include<stdio.h> void main() { int year; printf("enter the year"); scanf("%d",&year); if((year%400==0&&year%100!=0)||(year%4==0)) printf("leap year"); else printf("not leap year"); } Live output image

8) In a company an employee is paid as under: If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input by the user write a program to find his gross salary.

#include<stdio.h> void main() { float C,Gross_salary,basic_salary,HRA,DA; printf("Enter basic salary of Employee :-"); scanf("%f",&basic_salary); if (basic_salary<1500) { HRA=0.1*basic_salary;

DA=0.9*basic_salary; } else { HRA=500; DA=0.98*basic_salary; } Gross_salary=basic_salary+HRA +DA; printf("Gross salary is %f",Gross_salary); }Logic: If salary<1500 it follw the condition of HRA=0.1*basic_salary;

DA=0.9*basic_salary; Else it follow the HRA=500; DA=0.98*basic_salary;

Live image output

9) Write a program to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls. #include<stdio.h> void main() { float x,bill; printf("enter the no of calls:-"); scanf("%f",&x); if(x<100) { bill=200;

printf("you bill is:- %f",bill); } else if(x>100 &&150>x) { bill=200+(x-100)*0.6; printf("you bill is:- %f",bill); } else if(x>150 &&200>x) { bill=200+(50*0.6)+(x-150)*0.5; printf("you bill is:- %f",bill); } else { bill=200+(50*0.6)+(50*0.5)+(x-200)*0.4; printf("you bill is:- %f",bill); } } Logic: Here we are going to use else if clause but only writting of else if is not sufficent we have to use nesting of elseif clause using logical operator as per given condition Thinking to solve should be like that:1)What is the first condition is given- Minimum Rs. 200 for upto 100 calls. i.e upto 100 call the charge should be same 200 rs.

Therefore i use if(x<100) means if claa is less than 100 calls it directally print bill as 100 2)If call is more then 100 then upto next 50 calls the call rate is 0 .60 rs Therefore I use condition for call is between 100 and 150 else if(x>100 &&150>x) And for printing bill I use logic that upto 100 call bill is 200 and for next call upto 150 charge is 0.6 Therefore I print billl =200+(x-100)*0.6 3) call is more then 150 then upto next 50 calls the call rate is 0 .50 rs Therefore I use condition for call is between 150 and 200 else if(x>150 &&200>x) And for printing bill I use logic that upto 100 call bill is 200 and for next call 50 calls 50*0.6 next call from 150 to 200 charge is 0.5 Therefore I print bill=200+(50*0.6)+(x-150)*0.5; 4) call is more then 200 then upto next100 to 150callsi.e. next 50 calls the call rate is 0 .50 rs And next150 to 200 calls i.e. next 50 calls the call rate is 0 .60 rs.after this call from more than 200 charges is 0.4. Therefore I use condition for more than 200 call is else or u can use else if(x>200) And for printing bill I use logic that upto 100 call bill is 200 and for next call 50 calls 50*0.6 next call from 150 to 200 charge is 0.5and call more than 200 charge is 0.4 Therefore I print bill=200+(50*0.6)+(50*0.5)+(x-200)*0.4 Basic mistakes c in my words There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&. Dont use the single symbol | and &. These single symbols also have a meaning. They are bitwise operators may you get the same value using single one but I prefer to use && the reason I will tell you later . Live image output

10) Write a program to find the roots of and quadratic equation of type ax2+bx+c where a is not equal to zero. 12) The marks obtained by a student in 5 different subjects are input by the user. The student gets a division as per the following rules: Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 - Fail Write a program to calculate the division obtained by the student.

#include<stdio.h> void main() { int sub1,sub2,sub3,sub4,sub5,percentage;

printf("Enter marks of five subjects "); scanf("%d\n%d\n%d\n%d\n%d",&sub1,&sub2,&sub3,&sub4,&sub5); percentage=(sub1+sub2+sub3+sub4+sub5)/5; if(percentage>=60) printf("1st division"); else if(percentage>=50) printf("2nd division"); else if(percentage>=40) printf("3rd division"); else printf("Fail"); }

Live image output

12) Any character is entered by the user; write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters . Characters AZ az 09 special symbols ASCII Values 65 90 97 122 48 57 0 - 47, 58 - 64, 91 - 96, 123 127

#include<stdio.h> void main() { char ch; printf("Enter any character:"); scanf("%c",&ch); if (ch>=65 && ch<=90) printf("Character is a capital letter"); else if (ch>=97 && ch<=122) printf("Character is a small letter"); else if (ch>=48 && ch<=57) printf("Character is a digit"); else if ((ch>0 && ch<=47)||(ch>=58 && ch<=64)||(ch>=91 && ch<=96)||(ch>=123 && ch<=127)) printf("Character is a special symbol"); }

Live image output

Você também pode gostar