Saturday, April 30, 2016

Some Program related to Decision in C Programming Language

Question 1: Write a program to input any number and check whether number is even or odd.

 

Solution:

Logic --> here we take anyone random number from user and test whether it is even or odd number using condition. To test even number, it’s quite easy because even number are those that is divisible by 2 and those not will be even. Test given number by modular division by 2, if true, it is even number otherwise it is odd.

Program -->
#include<stdio.h>
#include<conio.h>
void min()
{int a;
clrscr();
printf("input any number");
scanf("%d",&a);
if (a%2==0)
printf("Number is even");
else
printf("Number is odd");
getch();
}

Output: Input any number....10
Number is even

Question 2: Write a program to find the roots of the following equation: ax^2+bx+c=0

 

Solution:

Logic --> Here we take all values of a,b and c which is required for us however x is not required because of unknown variable. We can find root by formula as:
                                                        x= (-b±√(b^2-4ac))/2a
However, we take + and - value separately. Root has both negative and positive value.
Step 1: First we find value inside root i.e (b^2-4ac) and save in new location.
Step 2: Then we check whether it is less than zero or not. If true root will have imaginary value.
Step 3: Otherwise calculate two roots separately and finally display required result.

Program -->
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{int a,b,c,r1,r2,d;
clrscr();
printf("Input value of a,b and c");
scanf("%d%d%d",&a,&b,&c);
d= ((b*b)-(4*a*c));
if (d<0)
printf("roots are imaginary");
else
{r1= (-b+sqrt (d))/ (2*a);
r2= (-b-sqrt (d))/ (2*a);
printf("%d%d",r1,r2);
getch();
}

Output: Input valur of a,b and c....5 10 3
-0.367, -1.632 

Note: we use "math.h" header file for defining sqrt function in step 13 and 14.   

Question 3: Write a program to find divisions obtained by students in class. The marks obtained by students in 5 different subjects are input through the keyboard. The students gets the division as per following rules ...

1. Percentage above or equal to 80-Distinction

2. Percentage above or equal to 60and less than 80-First

3. Percentage above or equal to 45 and less than 60-Second

4. Percentage above or equal to 35 and less than 45- Third

5. Percentage less than35-Fail

6. Result is fail, if a student fails in any one subject.

 

Solution:

Logic --> here, we take marks obtained by student in five different subjects and calculate their percentage and finally conclude their division. For this, we have to find first total marks obtained by student in five different subject i.e. total marks= marks1+marks2+marks3+marks4+marks5. Then we have to find percentage or average i.e. percentage=total marks/5. Finally we check for division from provided criteria in above question.
Step 1:  Input marks obtained by student in five different subject i.e. marks1, marks2, marks3, marks4, marks5.
Step 2: Calculate total marks as marks1+marks2+marks3+marks4+marks5.
Step 3: Calculate percentage. (Percentage=total marks/5)
Step 4: Check first whether student is fail in anyone subject otherwise he/she got any one division which can be calculated by condition. 

Program -->
#include<stdio.h>
#include<conio.h>
void main()
{int m1,m2,m3,m4,m5,Total,Per;
clrscr();
printf ("input marks obtained by student in five different subject");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
Total=m1+m2+m3+m4+m5;
printf("\n Total marks=%d",Total);
if (m1>35&&m2>35&&m3>35&&m4>35&&m5>35)
{Per=total/5;
if (Per>80)
printf("Distinction");
else if(Per>60&&Per<80)
printf("First Division");
else if(Per>45&&Per<60)
printf("Second Division");
else if(Per>35&&Per<45)
 printf("Third Division");
else
printf("Fail");
printf("\nPercentage=%d",per);
}
else
printf("Fail");
getch();
}

Output: Input marks obtained by student in five different subject...45 50 60 70 80
Total marks=305
First Division
Percentage=61

No comments:

Post a Comment

Popular Posts