Tuesday, November 25, 2014

C Looping pro



C Program to Find the Sum of first 50 Natural Numbers using for Loop

#include 
#include
void main()
{
    int  num, sum = 0;
    for (num = 1; num <= 50; num++)
    {
        sum = sum + num;
    }
    printf("Sum = %4d\n", sum);
    getch();
}
 
Output

Sum = 1275

C Program to accept two Integers and Check if they are Equal

 
#include 
#include
void main()
{
    int m, n;
    clrscr();
    printf("Enter the values for M and N\n");
    scanf("%d %d", &m, &n);
    if (m == n)
        printf("M and N are equal\n");
    else
        printf("M and N are not equal\n");
    getch();
}
 
Output

Enter the values for M and N
3 3
M and N are equal

C Program to Accept the Height of a Person & Categorize as Taller, Dwarf & Average

#include 
#include
void main()
{
float height;
clrscr();
printf("Enter  the Height (in centimetres) \n");
scanf("%f", &height);
if (height < 150.0)
printf("Dwarf \n");
else if ((height >= 150.0) && (height <= 165.0))
printf(" Average Height \n");
else if ((height >= 165.0) && (height <= 195.0))
printf("Taller \n");
else
printf("Abnormal height \n");
getch();
}
 
Output
 
Enter  the Height (in centimetres)
190
Taller

C Program to Read a Grade & Display the Equivalent Description

#include 
#include
#include 
#include 
void main()
{
    char remark[15];
    char grade;
    printf("Enter the grade \n");
    scanf("%c", &grade);
    grade = toupper(grade);
    switch(grade)
    {
    case 's':
        strcpy(remark, " Super");
        break;
    case 'a':
        strcpy(remark, " Very good");
        break;
    case 'b':
        strcpy(remark, " Fair");
        break;
    case 'y':
        strcpy(remark, " Absent");
        break;
    case 'f':
        strcpy(remark, " Fails");
        break;
    default :
        strcpy(remark, "Error in grade \n");
        break;
    }
    printf("Result  : %s\n", remark);
    getch();
}
 
Output

Enter the grade
s
Result:  Super

No comments:

Post a Comment