12 to 19 Experiements

download 12 to 19 Experiements

of 22

Transcript of 12 to 19 Experiements

  • 8/2/2019 12 to 19 Experiements

    1/22

    12 to 19EXPERIMENT

    CONDITIONAL STATEMENTS

    Ex No: 12 ROOTS OF A QUADRATIC EQUATION

    AIM:

    To write a program to find the roots of the Quadratic Equation.

    Formula:

    D=B2-4AC

    Condition 1: D= =0

    ROOT1=ROOT2= (-B+ D)/ (2*A)

    Condition 2: D>0

    ROOT1= (-B+D)/ (2*A)

    ROOT2= (-B-D)/ (2*A)Condition 3: D

  • 8/2/2019 12 to 19 Experiements

    2/22

    Step 3: If D is equal to zero then

    3.1 Print Roots are real and equal

    3:2 Find the two roots as ROOT1 = (-B + SQRT ((D)) / (2*A)

    3.3 Print the ROOT1 two times.

    Step 4: If D is greater than zero then

    4.1 Print Roots are real and unequal

    4.2 Find the two roots as

    ROOT1 = (-B + SQRT ((D)) / (2 * A)

    ROOT2 = (-BSQRT ((D)) / (2 * A)

    4.3 Print ROOT1 and ROOT2

    Step 5: If D is less than zero then

    5.1 Print the Roots are imaginary

    5.2 Find the two roots as

    X = (-B) / (2 * A)

    Y=-D/ (2*A)

    5.3 Print them in the format of (X + iY), (XiY)

  • 8/2/2019 12 to 19 Experiements

    3/22

    /* Root of a Quadratic Equation */

    #include

    #include

    #include

    void main()

    {

    float a,b,c,d,r,r1,r2;clrscr();

    printf("\n\n Enter the a,b and c values : ");

    scanf("%f %f %f",&a,&b,&c);

    d=(b*b)-(4*a*c);

    r=sqrt(d);

    if(d==0)

    {

    printf("\n\n Roots are Equal");

    r1=(-b+r)/(2*a);

    printf("\n\n Both Roots Values : %f",r1);

    }

    else if(d>0)

    {

    printf("\n\n Roots are Unequal");

    r1=(-b+r)/(2*a);

    r2=(-b-r)/(2*a);printf("\n\n Root1 and Root2 Values are : %f , %f",r1,r2);

    }

    else

    {

    printf("\n\n Roots are Imaginary");

    r1=-b/(2*a);

  • 8/2/2019 12 to 19 Experiements

    4/22

    r2=r/(2*a);

    printf("\n\n Root1 and Roots are : (%f + i%f) , (%f - i%f)",r1,r2,r1,r2);

    }

    getch();

    OUTPUT FOR ROOTS OF QUADRATIC EQUATION

    Enter the a,b and c values : 10 5 6

    Roots are Imaginary

    Root1 and Roots are: (0.250000 + i0.000000), (0.250000i0.000000)

    Enter the a, b and c values: 5 20 10

    Roots are Unequal

    Root1 and Root2 Values are: 0.585786,3.414214

    Enter the a, b and c values: 10 20 10

    Roots are Equal

    Both Roots Values: 1.000000

  • 8/2/2019 12 to 19 Experiements

    5/22

    Ex No: 13 ELECTRICITY BILL PREPARATION

    AIM

    To write a program to prepare the electricity bill.

    An electric power distribution company charges its domestic consumers as

    follows:

    CONSUMPTION UNITS RATE OF CHARGE

    0-200 Rs.0.50 per unit

    201-400 Rs 100+ Rs. 0.65 per unit excess of 200

    401-600 Rs 230+ Rs. 0.80 per unit excess of 400

    601 and above Rs 390+ Rs. 1 per unit excess of 600

    ALGORITHM:

    Step 1: Enter the value units

    Step 2: If units are in the range 0-200

    Charges=0.50*units

    Step 3: If units are in the range 201-400

    Charges=100 + 0.65*units

    Step 4: If units are in the range 401-600

  • 8/2/2019 12 to 19 Experiements

    6/22

    Charges=230 + 0.80*units

    Step 5: If units are in the range 601 and above

    Charges=390 + 1*units

    Step 6: Print the charges

    /* Electricity Bill Preparation */#include

    #include

    void main()

    {

    float units,charges;

    clrscr();

    printf("\n\n Enter the Units Values : ");

    scanf("%f",&units);

    if(units>=0 && units=201 && units=401 && units

  • 8/2/2019 12 to 19 Experiements

    7/22

    Enter the Units Values: 553

    Total Amount Is Rs. 672.40

    Enter the Units Values: 1129

    Total Amount Is Rs. 1519.40

    Ex No: 14 REVERSING THE GIVEN NUMBER

    AIM:

    To write a program to reverse the given integer using while loop.

    ALGORITHM:

    Step 1: Initialize SUM as 0

    Step 2: Enter the number, N to be reversed

    Step 3: While the number, N is not less than zero do

    3.1 R = Find the remainder of N divided by 10

    3.2 SUM = Multiply SUM by 10 and add to R

    3.3 N = Find the Quotient of N divided by 10

    Step 4: Print the value of SUM, which is the reversal of a given number.

  • 8/2/2019 12 to 19 Experiements

    8/22

    /* Reverse Order Number */#include

    #include

    void main()

    {

    int num,mod,rev=0;

    clrscr();

    printf("\n\n Enter a Number: ");

    scanf("%d", &num);while(num>0)

    {

    mod=num%10;

    rev=(rev*10)+mod;

    num=num/10;

    }

    printf("\n\n Reverse of the given Number: %d", rev);

    getch();

    }

    OUTPUT FOR REVERSING THE GIVEN NUMBER

    Enter a Number: 5867

    Reverse of the given Number: 7685

  • 8/2/2019 12 to 19 Experiements

    9/22

    Ex No: 15 ARMSTRONG NUMBER GENERATION

    AIM:

    To write a program to generate Armstrong numbers using do while loop.

    ALGORITHM:

    Step 1: Get limit of the generation N

    Step 2: Do the following for N times

    Step 2.1: SUM = sum of cubes of each digit of original number

    Step 2.2: If original number and SUM is same, then print the original

    number.

  • 8/2/2019 12 to 19 Experiements

    10/22

    /* Armstrong Number */

    #include

    #include

    void main()

    {

    int a,n,b=0,t;

    clrscr();printf("\n\n Enter the Number : hmm");

    scanf("%d",&n);

    t=n;

    while(n>0)

    {

    a=n%10;

    b=b+(a*a*a);

    n=n/10;

    }

    if(b==t)

    {

    printf("\n\n This is Armstrong Number");

    }

    else

    printf("\n\n This is not an Armstrong Number");

    getch();

    }

    OUTPUT FOR ARMSTRONG NUMBER

    Enter the Number: 547

    This is Not an Armstrong Number.

  • 8/2/2019 12 to 19 Experiements

    11/22

    Enter the Number: 407

    This is Armstrong Number.

    Ex No: 16 SINE SERIES CALCULATION

    AIM:

    To write a program to calculate Sine value using its series using for loop.

    Formula:

    Sin(x) =x - x3/3! +X

    5/5!-x

    7/7! +-----

    Hint: Similarly do for the cosine series

    Cos (x) =1-x2/2! +X

    4/4!-x

    6/6!

    ALGORITHM:

    Step 1: Get a degree X and No of terms in the series to be generated N

    Step 2: Assign X to Val, T, Sum for future purpose & the radian value of X

    in X.

    Step 3: Do the following for N+ 1 time

    Step 3.1: Find out each term in the series T

    Step 3.2: Find out sum of the terms T in SUM

    Step 4: Print SUM as Sin(X).

  • 8/2/2019 12 to 19 Experiements

    12/22

    /* Conditional Statements */

    /* Sine Series */

    #include

    #include

    void main()

    {

    int i = 2, n, s = 1, x, pwr = 1, dr;float nr = 1, x1, sum;

    clrscr();

    printf("\n\n **** Conditional Statements ****");

    printf("\n\n **** 8.Sine Series ****");

    printf("\n\n Enter the Angle : ");

    scanf("%d", &x);

    x1 = 3.142 * (x / 180.0);

    sum = x1;

    printf("\n\n Enter the number of Terms : ");

    scanf("%d", &n);

    while(i

  • 8/2/2019 12 to 19 Experiements

    13/22

    **** 8.Sine Series ****

    Enter the Angle : 65

    Enter the number of Terms : 11

    The Sum of the Sine Series is : 1.135

    Ex No: 17 SORTING THE GIVEN ARRAY OF NUMBERS

    AIM:

    To write a program to sort the given array of numbers in ascending /

    descending.

    ALGORITHM:

    Step 1: Enter the size of the array N.

    Step 2: Enter the elements of the array A.

    Step 3: Set an outer loop up to N-1

    Step 3.1: Set an inner loop up to N-1

    Step 3.2: If first number is greater than next number, then swap them

    Step 4: Print the ascending order of the given array (from 0th

    element to nth

    element).

    Step 5: Print the descending order of the given array (from nth

    element to 0th

    element).

  • 8/2/2019 12 to 19 Experiements

    14/22

    /* Arrays */

    /* 1. Array Sorting */

    #include

    #include

    void main()

    {

    int a[100],n,i,j,t;clrscr();

    printf("\n\n Enter Integer value for total number of elements to be sorted: ");

    scanf("%d",&n);

    printf("Enter the values for %d elements",n);

    for(i=0;i

  • 8/2/2019 12 to 19 Experiements

    15/22

    }

    OUTPUT:

    Enter Integer value for total number of elements to be sorted: 5

    Enter the values for 5 elements

    1211

    10

    9

    8

    Sorted Array

    8 9 10 11 12

  • 8/2/2019 12 to 19 Experiements

    16/22

    Ex No: 18 MATRIX ADDITION

    AIM:

    To write a program to ADD two matrices.

    ALORITHM:

    Step1: Enter the row I and column J of the matrix.

    Step2: Enter the elements of the matrix A.

    Step 3: Enter the elements of the matrix B.

    Step 4: Print the matrix A in the matrix form.

    Step 5: Print the matrix B in the matrix form.

    Step 6: Set a loop up for row values

    Step 6.1: Set an inner loop up for column values.

    Step 6.2: Add the elements of A and B in column wise and store the

    result in matrix C.

    Step 7: Print matrix C.

  • 8/2/2019 12 to 19 Experiements

    17/22

    /* Matrix Addition */

    #include

    #include

    Void main ()

    {

    int a[3][3],b[3][3],c[3][3],i,j,n,m;

    clrscr();printf("\n\n Enter the Rows and Columns Value : ");

    scanf("%d %d",&n,&m);

    printf("\n enter First Matrix Value :\n");

    for(i=0;i

  • 8/2/2019 12 to 19 Experiements

    18/22

    printf("%d\t",c[i][j]);

    }

    }

    getch();

    }

    OUTPUT:

    Enter the Rows and Columns Value:

    2

    2

    Enter First Matrix Value:

    3 4

    5 6

    Enter the Second Matrix Value:

    1 2

    7 8

    The Addition of two Matrixes

    4 6

  • 8/2/2019 12 to 19 Experiements

    19/22

    Ex No: 19 STUDENTS MARKS CALCULATION USING

    STRUCTURE

    AIM:

    To write a program to prepare students mark list using structure.

    ALGORITHM:

    Step1: Define the STUDENTS structure with details of Roll no, Name,

    Marks

    Step2: Get the number of students as N.

    Step3: Declare a structure variable with size N.

    Step4: Do the following for N times

    Step4.1: Get the details of the student.

    Step4.2: Calculate total, average, and Grade

    Step4.3: Print the students individual Mark list with total, average

    and Grade.

  • 8/2/2019 12 to 19 Experiements

    20/22

    PROGRAM:

    /*STUDENT MARKLIST USING STRUCTURES*/

    #include

    #include

    struct student {

    char name[25],regno[10],grade;

    int mark1,mark2,mark3,total;

    float percentage;

    }stud[50];

    void main()

    {

    int no,i;

    clrscr();

    printf("\n\n Enter the Number of Students : ");

    scanf("%d",&no);

    for(i=0;i

  • 8/2/2019 12 to 19 Experiements

    21/22

    scanf("%d",&stud[i].mark2);

    printf("\n Enter The Student Mark3 : ");

    scanf("%d",&stud[i].mark3);

    stud[i].total=stud[i].mark1+stud[i].mark2+stud[i].mark3;

    stud[i].percentage= stud[i].total/3;

    if(stud[i].percentage

  • 8/2/2019 12 to 19 Experiements

    22/22

    Enter the Student Mark1: 56

    Enter the Student Mark2: 67

    Enter the Student Mark3: 57

    Details for student 0

    Anand 118071001 90 78 79 247 82.00 B

    Details for student 1

    Sathiya 118071007 56 67 57 180 60.00 C