Sunday 23 April 2023

Exercise 2-To demonstrate use of decision making statements such as if and if-else.


SET A
1. Write a program to accept an integer and check if it is even or odd.

#include<stdio.h>
#include<conio.h>

void main()
{
 int no;
 clrscr();

 printf("\nFinding giving no is even or odd:");
 printf("\nEnter the number:");
 scanf("%d",&no);

 if(no%2==0)
   printf("\n%d NO is even:",no);
 else
   printf("\n%d NO is odd:",no);

 getch();

}
/*

Finding giving no is even or odd:                                              
Enter the number:4                                                             
                                                                               
4 NO is even:                                                                  
Finding giving no is even or odd:                                              
Enter the number:5                                                             
                                                                               
5 NO is odd:                                                                   
*/

  2] Write a program to accept three numbers and check whether the
  first is between the other two numbers.

#include<stdio.h>
#include<conio.h>

void main()
{
 int a,b,c,mid,min,max;
 clrscr();

 printf("\nEnter first no:");
 scanf("%d",&a);
 printf("\nEnter Second no:");
 scanf("%d",&b);
 printf("\nEnter third no:");
 scanf("%d",&c);

 if(a<b&&a>c||a>b&&a<c)
   mid=a;
 if(b>a&&b<c||b<a&&b>c)
   mid=b;
 if(c>a&&c<b||c<a&&c>b)
   mid=c;

 printf("\nMid:%d",mid);
 getch();
}
/*

Enter first no:20                                                              
                                                                               
Enter Second no:10                                                             
                                                                               
Enter third no:30                                                              
                                                                               
Mid:20                                                                         
*/
 3]Accept a charcter as input and check whether the character
 is digit.

#include<stdio.h>
#include<conio.h>

void main()
{
 char ch;
 clrscr();

 printf("\nEnter the charcter:");
 scanf("%c",&ch);
 if(ch>='0' && ch<='9')
   printf("\n character is a digit between 0 to 9 charcters:");
 else
   printf("\n character is not a digit between 0 to 9:");

 getch();
}
/*

Enter the charcter:4
character is a digit between 0 to 9 charcters:

Enter the charcter:A
character is not a digit between 0 to 9:
*/

  4]Write a program to accept a number and check if it is
  divisible by 5 and 7.

#include<stdio.h>
#include<conio.h>

void main()
{
 int no;
 clrscr();

 printf("\nEnter the no:");
 scanf("%d",&no);

 if((no%5==0)&&(no%7==0))
   printf("\n%d is divisible by 5 and 7:",no);
 else
  printf("\n%d is not divisible by 5 and 7:",no);

 getch();
}
/*

Enter the no:35                                                                
                                                                               
35 is divisible by 5 and 7:                                                    
Enter the no:55                                                                
                                                                               
55 is not divisible by 5 and 7:                                                
*/

  5] Write a program,which accepts annual basic salary of an employee and calculates
  and display the income tax as per the following rules.
  Basic:<1,50,000               Tax=0
     1,50,000 to 3,00,000   tax=20%
     >300000                tax=30%

#include<stdio.h>
#include<conio.h>

void main()
{

 float tax=0,as,limit;
 clrscr();
 printf("\nEnter the Annual salary of emp: ");
 scanf("%f",&as);

 limit=150000;
 if(as<=limit)
      printf("\nfor %f NO Tax: ",as);
 else if(as>150000&&as<300000)
      tax=(as-limit*0.1)*0.1;
 else if(as>300000)
      tax=(as-limit*0.1)*0.3;
 printf("\nOn salary: %f Tax is:%f",as,tax);

 getch();
}
/*

Enter the Annual salary of emp: 7000                                           
                                                                               
for 7000.000000 NO Tax:                                                        
On salary: 7000.000000 Tax is:0.000000                                         
Enter the Annual salary of emp: 200000                                         
                                                                               
On salary: 200000.000000 Tax is:18500.000000                                   
Enter the Annual salary of emp: 500000                                         
                                                                               
On salary: 500000.000000 Tax is:145500.000000                                  
*/
6 ] Accept a lowercase character from the user and check whether
  the character is a vowel or consonant.

#include<stdio.h>
#include<conio.h>

void main()
{
 char c;
 clrscr();

 printf("\nEnter the lowercase character:");
 scanf("%c",&c);

 if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
   printf("\n%c charcter is vowel:",c);
 else
   printf("\n%c not vowel:",c);

 getch();
}
/*
Enter the lowercase character:E                                                
                                                                               
E charcter is vowel:                                                           
Enter the lowercase character:e                                                
                                                                               
e charcter is vowel:                                                           
Enter the lowercase character:d                                                
                                                                               
d not vowel:                                                                   
*/
SET B
1]Write a program to check whether gien character is a digit or character in
 lowecase or uppercase alphabet.

#include<stdio.h>
#include<conio.h>

void main()
{
 char c;
 int ch;
 clrscr();

 printf("\nEnetr the charcter:");
 scanf("%c",&c);

 ch=c;
 if(ch>=48&&ch<=58)
   printf("\n%c Character is digit:",c);
 else if(ch>=97&&ch<=122)
   printf("\n%c Charcter is lowercase Alphabet:",c);
 else
   printf("\n%c Charcter is uppercase Alphabet:",c);


 getch();
}
/*

Enetr the charcter:A                                                           
                                                                               
A Charcter is uppercase Alphabet:                                              
Enetr the charcter:a                                                           
                                                                               
a Charcter is lowercase Alphabet:
Enetr the charcter:4

4 Character is digit:


Enetr the charcter:+

Special character:
*/
 2]Accept the time as hour,minute and second and check whether the
  time is valid.

#include<stdio.h>
#include<conio.h>

void main()
{
 int h,m,s;
 clrscr();

 printf("\nEnter the time");
 printf("\nEnter hours:");
 scanf("%d",&h);
 printf("\nEnter minute:");
 scanf("%d",&m);
 printf("\nEnter second:");
 scanf("%d",&s);

 if(h<=24&&m<=60&&s<=60)
    printf("\nTime is valid:");
 else
    printf("\nTime is not valid:");

 getch();
}
/*

Enter the time                                                                 
Enter hours:22                                                                 
                                                                               
Enter minute:55                                                                
                                                                               
Enter second:33                                                                
                                                                               
Time is valid                                                                  
Enter the time                                                                 
Enter hours:26                                                                 
                                                                               
Enter minute:33                                                                
                                                                               
Enter second:33

hours is invalid:
Enter the time
Enter hours:22

Enter minute:61

Enter second:33

Minute is invalid:
*/
  3]Accept any year as input through the keyboard.write a program
  to check whether the year is a leap year or not.

#include<stdio.h>
#include<conio.h>

void main()
{
 int yr;
 clrscr();
 printf("\nEnter the year:");
 scanf("%d",&yr);

 if(yr%4==0 && !(yr%100==0))
    printf("\n%d year is leap year :",yr);
 else
    printf("\n%d year is not leap year:",yr);

 getch();
}
/*
Enter the year:2017
                                                                               
2017 year is not leap year:                                                    
Enter the year:2018                                                            
                                                                               
2018 year is not leap year:                                                    
Enter the year:2019                                                            
                                                                               
2019 year is not leap year:                                                    
Enter the year:2020                                                            
                                                                               
2020 year is leap year :                                                       
*/
  4] Accept three sides of triangle as input,and print whether
  the triangle is valid or not.(hint:the triangle id valid if the sum
  of each of the two sides is greater than the third side )

#include<stdio.h>
#include<conio.h>

void main()
{
 int a,b,c;
 clrscr();

 printf("\nEnter the sides of ABC triangle:");
 printf("\nEnter the  AB lenght in cm:");
 scanf("%d",&a);
 printf("\nEnter the BC lenght in cm:");
 scanf("%d",&b);
 printf("\nEnter the Ac lenght in cm:");
 scanf("%d",&c);

 if((a+b)>c)
   printf("\nABC is valid triangle:");
 else
   printf("\nABC is not valid triangle:");

 getch();
}
/*

Enter the sides of ABC triangle:
Enter the  AB lenght in cm:3

Enter the BC lenght in cm:5

Enter the Ac lenght in cm:6

ABC is valid triangle:

Enter the sides of ABC triangle:
Enter the  AB lenght in cm:0

Enter the BC lenght in cm:-2

Enter the Ac lenght in cm:
5

ABC is not valid triangle:
 */
 5] Accept the X and Y co-ordinate of a point and find
  the quadrant in which the point lies.

#include<stdio.h>
#include<conio.h>

void main()
{
 int x1,y1,x2,y2;
 clrscr();

 printf("\nEnter Point A co-ordinate:");
 printf("\nEnter X co-ordinate:");
 scanf("%d",&x1);
 printf("\nEnter Y co-ordinate:");
 scanf("%d",&y1);
 printf("\nEnter Point B co-ordinate:");
 printf("\nEnter X co-ordinate:");
 scanf("%d",&x2);
 printf("\nEnter the y co-ordinate:");
 scanf("%d",&y2);

 getch();
}
/*

Enter Point A co-ordinate:                                                     
Enter X co-ordinate:1                                                          
                                                                               
Enter Y co-ordinate:2                                                          
                                                                               
Quadrant I:                                                                    
Enter Point A co-ordinate:                                                     
Enter X co-ordinate:-1

Enter Y co-ordinate:2

Quadrant II:
Enter Point A co-ordinate:
Enter X co-ordinate:-1

Enter Y co-ordinate:-4

First Quadrant: III
Enter Point A co-ordinate:
Enter X co-ordinate:4

Enter Y co-ordinate:-3

First Quadrant:IV

Enter Point A co-ordinate:
Enter X co-ordinate:0

Enter Y co-ordinate:0

Origin:
*/
/*Lab Course:1 Exercise:2 Set:B
  Example:6-Write a program to roots of a quadratic equation.
  Consider all possible cases.
*/

#include<stdio.h>
#include<conio.h>
#include<math .h>
void main()
{
 double r1,r2,a,b,c,dm,img,realpart;
// clrscr();
 printf("\nEnter the cofficent a,b,c:");
 scanf("%lf%lf%lf",&a,&b,&c);
 dm=b*b-4*a*c;
 if(dm>0)
   {
    r1=-b+sqrt(dm);
    r2=-b-sqrt(dm);
    printf("\nroot1=%lf\nroot2=%lf",r1,r2);
   }
 else if(dm==0)
    {
     r1=r2=-b/2*a;
     printf("\nroot1=root2=%lf",r1);
    }
 else if(dm<0)
    {
     realpart=-b/2*a;
     img=sqrt(-dm)/2*a;
     printf("\nroot1=%lf+i%lf\nroot2=%lf-i%lf",realpart,img,realpart,img);
    }

 getch();
}
/*
Enter the cofficent a,b,c:1

1
1

root1=-0.500000+i0.866025
root2=-0.500000-i0.866025
Enter the cofficent a,b,c:1
4

1

root1=-0.535898
root2=-7.464102
Enter the cofficent a,b,c:1
2
1

root1=root2=-1.000000

*/
  Example:7-Accept the cost price and seeling price from the keyboard.
  Find out if the seller has made a profit or loss and display
  how much profit or loss has been made.

#include<stdio.h>
#include<conio.h>
void main()
{
 int cp,sp,x;
// clrscr();
 printf("\nEnter Cost prize:");
 scanf("%d",&cp);
 printf("\nEnter Seeling prize:");
 scanf("%d",&sp);
 x=sp-cp;
 if(x>0)
    printf("\nProfit is %d",x);
 else
    printf("\nLoss is %d",x);
 getch();
}
/*
Enter Cost prize:10                                                            
Enter Seeling prize:12                                                         
Profit is 2                                                                    
Enter Cost prize:20                                                            
Enter Seeling prize:15                                                         
 Loss is -5                                                                     
 */
SET C Write programs to solve the following problems
/*
1. Write a program to accept marks for three subjects and find the total marks secured ,
average and also display the class obtained. (Class I – above __%, class II – ___% to ___%,
pass class – ___% to ___% and fail otherwise)
*/

#include<stdio.h>
#include<conio.h>
void main()
{
 float m1,m2,m3,total,classs,avg ;
 clrscr();
 printf("\nEnter 3 subject marks:");
 scanf("%f%f%f",&m1,&m2,&m3);
 total=m1+m2+m3;
 printf("\nTotal marks: %f",total);
 avg=total*100/300;
 printf("\nAvg : %f%",avg);
 if(avg>=70 && avg<=100)
    printf("\nFirst Class with distinction.");
 else if(avg>=60 &&avg<70)
         printf("\n First Class");
      else if(avg>=55 && avg<60)
              printf(" Higher second class");
            else if(avg>=50 &&avg<55)
                    printf("second class");
                 else if(avg>=40 && avg<50)
                         printf("\nPass CLass");
              else
             printf("\nFail");

 getch();
}
/*

Enter 3 subject marks:40                                                       
40                                                                             
40                                                                             
                                                                               
Total marks: 120.000000                                                        
Avg : 40.000000%                                                               
Pass CLass                                                                     
Enter 3 subject marks:55                                                       
55                                                                             
55                                                                             
                                                                               
Total marks: 165.000000                                                        
Avg : 55.000000% Higher second class                                           
Enter 3 subject marks:45                                                       
89                                                                             
50                                                                             
                                                                               
Total marks: 184.000000                                                        
Avg : 61.333332%
 First Class
*/

No comments:

Post a Comment

Questions

  Exercise 1 Set A . Apply all the three program development steps for the following examples.  1. Accept dimensions of a cylinder and p...