C PROGRAMS : CONTROL STRUCTURES

C PROGRAMS : CONTROL STRUCTURES
21.Program to award Grades according to the following:
/*
80% and above GRADE A
70 to 79%  GRADE B
60 to 69%  GRADE C
50 to 59  GRADE D
Less than 50% FAILED

Assume maximum marks for each subject is 100. 
You have to take marks input of 5 subject
*/

// Using if-else
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable for marks
 int m1,m2,m3,m4,m5;
 
 // Declaring variable for p=percentage
 int p;
 
 // Inputing marks
 printf("Enter marks of 5 subjects:");
 scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
 
 // Calculating and displaying percentage marks scored
 p=(m1+m2+m3+m4+m5)/5;
 printf("Percentage : %d\n",p);
 
 // Determining Grades and Displaying it
 if(p<50)
 printf("Failed");
 else if(p>=50 && p<=59)
 printf("Grade D");
 else if(p>=60 && p<=69)
 printf("Grade C");
 else if(p>=70 && p<=79)
 printf("Grade B");
 else
 printf("Grade A");
  
 getch(); // Linux user - Remove this

 return 0;
}


22.Grade Program without using if-else statement
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable for marks
 int m1,m2,m3,m4,m5;
 
 // Declaring variable for p=percentage
 int p,i,g;
 
 // Inputing marks
 printf("Enter marks of 5 subjects:");
 scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
 
 // Calculating and displaying percentage marks scored
 p=(m1+m2+m3+m4+m5)/5;
 printf("Percentage : %d\n",p);
 
 // Determining grades and Displaying it
 while(p<50)
 {
  printf("Failed");
  break;
 }
 while(p>=50 && p<=59)
 {
  printf("Grade D");
  break;
 }
 while(p>=60 && p<=69)
 {
  printf("Grade C");
  break;
 }
 while(p>=70 && p<=79)
 {
  printf("Grade B");
  break;
 }
 while(p>=80)
 {
  printf("Grade A");
  break;
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

23.Program to calculate the sum of individual digits of a number
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable for n=number, d=digit
 int n, d, sum=0;
 
 // Inputing number
 printf("Enter number: ");
 scanf("%d",&n);
 
 // Determing sum of digits
 while(n!=0)
 {
  d=n%10;  // Extracting last digit of a number
  sum=sum+d; // Calculating digit sum
  n=n/10;  // Extracting first two digits 
  
 }
 
 // Displaying sum of digits
 printf("Sum of digits : %d",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}

24.Program to determine whether a given year is leap year or not
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable for y=year
 int y;
 
 // Inputing year
 printf("Enter year to check leap year or not: ");
 scanf("%d",&y);
 
 // Determining and displaying whether leap year or not
 if(y%4==0)
 {
  if(y%100==0 && y%400!=0)
  printf("Century year, But not a leap year");
  else
  printf("Leap year");
 }
 else
 printf("Not a Leap Year");
 
 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »