Friday, 4 December 2015

SUM OF DIGITS

AIM
To write a C program to find the sum of digits of a given number.
ALGORITHM
Step 1: Start
Step 2: Read num
Step 3: rß(num%10)
            numß(num/10)
            sumß(sum+r)
Step 4: if (num>0) go to step 3
Step 5: Print sum
Step 6: Stop
PROGRAM
#include<conio.h>
void main()
{
    int num,r,s=0;
    printf("Enter the number\n");
    scanf("%d",&num);
    while(num>0)
    {
    r=num%10;
    s=s+r,num=num/10;
    }
    printf("Sum of digits = %d",s);

}

‘n’ NATURAL NUMBERS AND ITS SUM

AIM
To write a C program to find the sum of ‘n’ natural numbers and its sum.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: if (n=0), if not go to step 5
Step 4: Print Invalid number, go to step 2
Step 5: ißn, sß1, Print i
Step 6: if (i=0), if not go to step 8
Step 7: Print s, go to step 10
Step 8: sß(s+1)
Step 9: iß(i-1), go to step 4
Step 10: Stop
PROGRAM
#include<conio.h>
#include<stdio.h>
void main()
{
int i,n,s=0;
            printf(“Enter your limit:”);
            scanf(“%d”,&n);
            if (n==0)
                        printf(“\nEnter a valid number”);
            printf(“Numbers:”);
            for (i=1;i<=n;i++)
            {
                        printf(“%d\t”,i);
                        s=s+i;
}
printf(“\nSum:%d”,s);
            getch();

}

AVERAGE MARK AND GRADE OF STUDENTS

AIM
To write a C program to calculate the average mark and grade of students using switch.
ALGORITHM
Step 1: Start
Step 2: Read n, m1, m2, m3, m4, m5, iß0
Step 3: if (i<n), if not go to step 19
Step 4: avgß(m1+m2+m3+m4+m5)/5
Step 5: iß(i+1), avg1ß(avg*100)
Step 6: if (avg1>90), if not go to step 8
Step 7: Print Grade A, avg, go to step 18
Step 8: if (avg1>80), if not go to step 10
Step 9: Print Grade B, avg, go to step 18
Step 10: if (avg1>70), if not go to step 12
Step 11: Print Grade C, avg, go to step 18
Step 12: if (avg1>60), if not go to step 14
Step 13: Print Grade D, avg, go to step 18
Step 14: if (avg1>50), if not go to step 16
Step 15: Print Grade E, avg, go to step 18
Step 16: if (avg1<=50)
Step 17: Print Grade F, avg
Step 18: Go to step 3
Step 19: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
    float m1,m2,m3,m4,m5,avg;
    int grade;
    printf("Enter mark of 5 subjects\n");
    scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
    avg=(m1+m2+m3+m4+m5)/3;
    printf("Average = %f\n",avg);
    if(avg>=85)
        grade=1;
    else if(avg>=70)
        grade=2;
    else if(avg>=60)
        grade=3;
    else if(avg>=50)
        grade=4;
    else if(avg>=40)
        grade=5;
    else
        grade=6;
    switch(grade)
    {
    case 1:
        printf("grade is A");
        break;
    case 2:
        printf("grade is B");
        break;
    case 3:
        printf("grade is C");
        break;
    case 4:
 printf("grade is D");
    case 5:
        printf("grade is E");
        break;
    case 6:
        printf("FAIL");
        break;
    }
    getch();

}

ODD OR EVEN NUMBER

AIM
To write a C program to check the given number is odd or even.
ALGORITHM
Step 1: Start
Step 2: Read a
Step 3: if (a%2=0), if not go to step 5
Step 4: Print Given number is even
Step 5: Print Given number is odd
Step 6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
            printf(“Enter a number:”);
            scanf(“%d”,&a);
            if (a%2==0)
                        printf(“\nGiven number is even.”);
            else
                        printf(“\nGiven number is odd.”);
            getch();
}


ROOTS OF A QUADRATIC EQUATION

AIM
To write a C program for finding roots of a quadratic equation
ALGORITHM
Step 1: Start
Step 2: Read values of a,b and c
Step 3: Calculate value of discriminant
            D←b*b-(4*a*c)
Step 4: if D←0
             Root←(-1*b/(2*a))
             Display Root
Step 5: if D>0
             Root1←(-b+√D)/2a
             Root2←(-b-√D)/2a
             Display Root1 and Root2
Step 6: if D<0, Display ‘roots are imaginary’
Step 7: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    float a,b,c,d,r1,r2;
    printf("Enter the values of a,b,c\n");
    scanf("%f%f%f",&a,&b,&c);
    d=(b*b)-(4*a*c);
    r1=((-1*b)+sqrt(d))/(2*a);
    r2=(-b-sqrt(d))/(2*a);
        printf("Roots are real and distinct:\n%f\n%f",r1,r2);
    }
    else if(d==0)
    {
        r1=-b/(2*a);
        printf("Roots are real and equal:\n%f",r1);
    }
    else
    {
        printf("The roots are imaginary");
    }
    getch();
}

RESULT
The C program for findng the roots of a quadratic equation is completed successfully and the output is verified.


SWAPPING TWO NUMBERS WITHOUT USING TEMPORARY VARIABLE

AIM
To write a C program for swapping two numbers without using temporary variable
ALGORITHM
Step 1: Start
Step 2: Read two numbers a,b
Step 3: a←a+b
Step 4: b←a-b
Step 5: a←a-b
Step 6:Print the numbers a,b
Step 7:Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("Before swapping\na=%d\nb=%d\n",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\nAfter swapping\na=%\nb =%d ",a,b);
    getch();
}
RESULT

The C program for swapping two numbers is completed successfully and the output is verified.




Menu Driven Program To Find Volume Of Sphere And Cube

AIM
To write a menu driven program for finding the volume of a sphere and a cube
ALGORITHM
Step 1:   Start
Step 2:   Print choice sphere ,cube
Step 3:   if cube go to  step 7
Step 4:   read radius r
Step 5:   vr*r*r*(4/3)*3.14
Step 6:   Print v
Step 7:   read side s
Step 8:   vs*s*s
Step 9: print v
Step 10: print want to continue?
Step 11: if yes,go to step 1
Step 12: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
    char t,y,Y,n;
    float radius,side,pi;
    int ch;
    pi=3.14;
     do

   {
    printf("\nMENU\n1)volume of sphere\n2)volume of cube\n");
    scanf("%d",&ch);
    switch (ch)
    {
        case 1:
        printf("Enter the value of radius\n");
        scanf("%f",&radius);
        printf("the volume of the sphere=%f cm3",radius*radius*radius*pi*(1.33));
        break;
        case 2:
        printf("\nEnter the value of side\n");
        scanf("%f",&side);
        printf("the volume of the cube=%f cm3",side*side);
        break;
        default:printf("INVALID CHOICE");
        break;
    }
        printf("\n\nDO YOU WANT TO CONTINUE:\nThen press y else n:");
        fflush(stdin);
        scanf("%c",&t);
   }
        while(t=='y'||t=='Y');
        getch();
}
RESULT
The menu driven program for finding the volume of sphere and cube was written , executed and the output  verified successfully.


OUTPUT
Menu Driven Program To Find Volume Of  Sphere  And Cube