Wednesday 29 October 2014

C program for even or odd

Write a program to determine whether a given number is “odd” or “even” and print
       the message  NUMBER IS EVEN
      OR
      NUMBER IS ODD
      (a)  Without using the else option.
      (b) With else option.
Algorithm:–
Without using the else option
Step 1:  Read  x.
Step 2:  Check x%2==0.
Step 3:  If true then go to step 4 and otherwise go to step 5.
Step 4:  Display “The number is even” and exit.
Step 5:  Display “The number is odd”.
With else option
Step 1:  Read  x.
Step 2:  Check x%2==0.
Step 3:  If true then go to step 4 and otherwise go to step 5.
Step 4:  Display “The number is even”.
Step 5:  Display “The number is odd”.

Program:–
Without using the else option
//Write a program to determine whether a given number is “odd” or “even” and print the message
//NUMBER IS EVEN
//Or
//NUMBER IS ODD
//(a)      Without using the else option.
//(b)      With else option.
// Date : 13/03/2010
 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 void main()
 {
   int x;
   clrscr();
   printf(“Enter an integer number: “);
   scanf(“%d”,&x);
   if(x%2==0)
   {
    printf(“The number entered is even”);
    getch();
    exit(0);
   }
    printf(“The number entered is odd”);
    getch();
 }
Output:–
Enter an integer number: 5
The number entered is odd
With else option
//Write a program to determine whether a given number is “odd” or “even” and print the message
//NUMBER IS EVEN
//Or
//NUMBER IS ODD
//(a)      Without using the else option.
//(b)      With else option.
// Date: March 13,2010
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
   int x;
   clrscr();
   printf(“Enter an integer number: “);
   scanf(“%d”,&x);
   if(x%2==0)
     printf(“The number entered is even”);
   else
     printf(“The number entered is odd”);
   getch();
 }
Output:–
Enter an integer number: 5
The number entered is odd


5.2 Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.


Algorithm:–

Step 1: Store 100 to Num & 0 to Sum.
Step 2: if Num%7=0 then go to Step 3
Step 3: Compute Count=Count+1 & Sum=Sum+Num & Num=Num+1.
Step 4: if Num<=200 then go to Step 2 otherwise go to Step 5.
Step 5: Display Count & Sum.

Program:–
//Write a program to find the number of and sum of all
//integers greater than 100 and less than 200 that are divisible by 7.
// Date : 13/03/2010
 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 void main()
 {
            int Num,Sum,Count;
            clrscr();
            Num=100;
            Sum=Count=0;
            Loop:
            if (Num%i==0)
            {
                        Sum=Sum+Num;
                        Count=Count+1;
            }
            Num=Num+1;
            if(Num<=100)
                        goto Loop;
            printf(“Count:– %d\n”,Count);
            printf(“Sum:– %d”,Sum);
 }


5.3 A set of two linear equation two unknows x1 and x2 is given below:
                       
                                    ax1 + bx2 = m
                                    cx1 + dx2 = n
      The set has a unique solution

                                      x1=(md-bn)/(ad-cb)
                                      x2=(na-mc)/(ad-cb)

Algorithm:–
Step 1: Read a,b,c,d,m and n.
Step 2: Compute a*d-c*b and store the result Dr.
Step 3: Check if Dr! =0.
Step 4: If true then go to Step 5 and otherwise go to step 9.
Step 5: Compute (m*d-b*n)/(a*d-c*b) and store the result x1.
Step 6: Compute (n*a-m*c)/(a*d-c*b) and store the result x2.
Step 7: Display x1.
Step 8: Display x2 and go to step 10.
Step 9: Display “The division is not possible”.
Step 10: Stop.

Program:–

//A set of two linear equation two unknows x1 and x2 is given below:
//                                  ax1 + bx2 = m
//                                  cx1 + dx2 = n
//      The set has a unique solution
//                                    x1=(md-bn)/(ad-cb)
//                                    x2=(na-mc)/(ad-cb)
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
   int a,b,c,d,m,n,Dr;
   float x1,x2;
   clrscr();
   printf(“Enter the value of a, b, c, d, m, n: “);
   scanf(“%d%d%d%d%d%d”,&a,&b,&c,&d,&m,&n);
   Dr=(a*d-c*b);
    if(Dr!=0)
    {
            x1=(m*d-b*n)/dr;
            x2=(n*a-m*c)/dr;
            printf(“\n The value of x1= %f \n The value of x2= %f”,x1,x2);
    }
    else
            printf(“The division is not possible and result is an abrupt value “);
   getch();
 }
5.4 Given the list of marks ranging from 0 to 100,write a program to compute and print the number of students:
a) who have obtained more than 80 marks.
b) who have obtained more than 60 marks
c) who have obtained more than 40 marks
d) who have obtained 40 or less marks
e) in the range 81 to 100
f) in the range 61 to 80
g) in the range 41 to 60
h) in the range 0 to 40

The program should use minimum number of if statements.

5.5 Admission to a professional course in subject to the following conditions:
a) Marks in mathematics >=60
b) Marks in Physics >=50
c) Marks in Chemistry >=40
d) Total in all three subjects >=200
or
Total in mathematics and physics>=150.
Given the marks in the three subjects, write a program to process the applications to the eligible candidates.

Algorithm:–

Step 1: Read Maths, Phy and Chem.
Step 2: Compute Maths+Phy+Chem and store the result in Total
Step 3: Compute Maths+Phy and store the result Total_MP
Step 4: Check Maths>=60 && Phy>=50 && Chem>=40 && Total>=200
Step 5: If Step 4 true then go to step 6 otherwise go to step 7.
Step 6: Display “The candidate is eligible for the course” and go to step 11.
Step 7: Check Total_MP>=150
Step 8: If Step 7 true then go to step 9 otherwise go to step 10.
Step 9: Display “The candidate is eligible for the course” and go to step11
Step 10: Display “The candidate is not eligible for the course” and go to step 11.
Step 11: Stop.
Program:–

//Admission to a professional course in subject to the following conditions:
//a) Marks in mathematics >=60
//b) Marks in Physics >=50
//c) Marks in Chemistry >=40
//d) Total in all three subjects >=200
//or
//Total in mathematics and physics>=150.
//Given the marks in the three subjects, write a program to process the applications to the eligible candidates.
//Date: 13/03/2010
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
   int Maths,Phy,Chem,Total,Total_MP;
   clrscr();
   printf(“Enter the marks of maths :”);
   scanf(“%d”,&Maths);
   printf(“Enter the marks of phy :”);
   scanf(“%d”,&Phy);
   printf(“Enter the marks of chem :”);
   scanf(“%d”,&Chem);
   Total=Maths+Phy+Chem;
   Total_MP=Phy+Maths;
   if (Maths>=60 && Phy>=50 && Chem>=40 && Total>=200)
      printf(“The candidate is eligible for the admission”);
   else
   {      
            if(Total_MP>=150)
                        printf(“The candidate is eligible for the admission”);
            else
            `           printf(“The candidate is not eligible for the admission”);
   }
  getch();
   }

No comments:

Post a Comment