Saturday, April 11, 2009

Calculate percentage of 5 subjects & display Divison resp.

/*The marks obtained by a student in 5 different subjects are input through keyboard.The student gets a division as per the following rules:

Percentage above or equal to 60 - First division
Per btw 50 and 59 - Second div
per btw 40 and 49-Third div
per less than 40 - Fail */

Solution Code:
=========

#include <stdio.h>
int main(void)
{
int m1,m2,m3,m4,m5,sum,per;
printf("Enter marks for Math : ");
scanf("%d",&m1);
printf("\nEnter marks for Chemistry : ");
scanf("%d",&m2);
printf("\nEnter marks for Biology : ");
scanf("%d",&m3);
printf("\nEnter marks for Physics : ");
scanf("%d",&m4);
printf("\nEnter marks for English : ");
scanf("%d",&m5);
sum = (m1 + m2 + m3 + m4 + m5);
per = (sum * 100 / 500);
printf("\nYou got %d percentage",per);

if ( per >= 60 )
printf("\nFirst Division");

else if (( per <= 59) && ( per >= 50 ))
printf("\nSecond Divison");

else if (( per <= 49 ) && ( per >= 40 ))
printf("\nThird Divison");

else
printf("\nSorry you Failed");
return 0;
}

2nd Solution Code:-
===========

#include
int main(void)
{
int m1,m2,m3,m4,m5,sum,per;
printf("Enter marks for Math : ");
scanf("%d",&m1);
printf("\nEnter marks for Chemistry : ");
scanf("%d",&m2);
printf("\nEnter marks for Biology : ");
scanf("%d",&m3);
printf("\nEnter marks for Physics : ");
scanf("%d",&m4);
printf("\nEnter marks for English : ");
scanf("%d",&m5);
sum = (m1 + m2 + m3 + m4 + m5);
per = (sum * 100 / 500);
printf("\nYou got %d percentage",per);

if ( per >= 60 )
printf("\nFirst Division");
else
{
if ( per >= 50 )
printf("\nSecond Division");

else
{
if ( per >= 40 )
printf("\nThird Division");

else
printf("\nFail");
}
}
return 0;
}


Example of compound statement & nested if

/*Prompts for and accepts input of an integer.Integer is tested to see that it meets the stated criteria.If it doesn't an error message is issued.If all criteria are met,execution terminates silently*/

*Solution Code:-
===========

#include <stdio.h>
int mod(int i);
int main(void)
{
int i;
printf("Enter an Even number less than 20 : ");
scanf("%d",&i);

if ( i < 20)
{
if ( mod(i))
printf("Oops you entered Odd number %d",i);
else if ( i <= 0 )
printf("Oops you entered number less than or equal to 0 %d",i);
}
else
printf("You entered a number greater than 20");



return 0;
}

int mod(int i)
{
return ( i % 2 );
}

Plays a one-time guessing game with user.User enters a number which is compared with target

/*Plays a one-time guessing game with the user.The user enters a number,which is compared with TARGET.The computer issues a diagnostic message & then the correct result*/

Solution Code:-
=========


#include <stdio.h>
#define TARGET 17
int test(int i,int target);
int main(void)

{
int i;
printf("Enter target number : ");
scanf("%d",&i);
test( i,TARGET);
return 0;
}

void test(int i,int target)
{
if ( i > target)
printf("\nTarget number is smaller than %d,try again",i);
else if ( i < target)
printf("\nTarget number is greater than %d,try again",i);
else if ( i == target )
printf("\nFinally you could make it");

}

Detect if number is odd or even by function call

/*With an if-else statement detect if input number is odd or even with a function call*/

Solution Code:
=========

#include <stdio.h>
int odd(int i);
int main(void)
{
int i;
printf("Enter a decimal integer : ");
scanf("%d",&i);

if (odd(i))
printf("\n%d is Odd number",i);
else
printf("\n%d is Even number",i);
return 0;
}

int odd(int i)
{
return ( i % 2 );
}

A decimal integer is input and added to constant while displaying the sum message is issued

/*A decimal integer is input,added to CONSTANT and the sum is displayed .If the sum is less than 20, a message is issued*/

Solution code:-
========

#include <stdio.h>
#define CONSTANT 7
int sum(int num,int constant);
int main(void)
{
int i,add;
printf("Enter a decimal integer : ");
scanf("%d",&i);

add = sum(i,CONSTANT);
if ( add < 20 )
printf("\nAmount %d is less than 20",add);


return 0;
}

int sum(int num,int constant)
{
return (num + constant);
}