Anda di halaman 1dari 22

12 to 19EXPERIMENT

CONDITIONAL STATEMENTS Ex No: 12 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<0 X = (-B) / (2 * A) Y=-D/ (2*A) Print them in the format of (X + iY), (X iY) ROOTS OF A QUADRATIC EQUATION

ALGORITHM: Step 1: Enter the value of A, B, C Step 2: Find the value of D by using the formula D = (B*B) (4*A*C)

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 = (-B SQRT ((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), (X iY)

/* Root of a Quadratic Equation */ #include<stdio.h> #include<conio.h> #include<math.h> 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);

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.250000 i0.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

Ex No: 13 AIM

ELECTRICITY BILL PREPARATION

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 201-400 401-600 601 and above Rs.0.50 per unit Rs 100+ Rs. 0.65 per unit excess of 200 Rs 230+ Rs. 0.80 per unit excess of 400 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

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<stdio.h> #include<conio.h> void main() { float units,charges; clrscr(); printf("\n\n Enter the Units Values : "); scanf("%f",&units); if(units>=0 && units<=200) charges = 0.50*units; else if(units>=201 && units<=400) charges = 100+(0.65*units); else if(units>=401 && units<=600) charges = 230+(0.80*units); else charges = 390+(1*units); printf("\n\n Total Amount Is Rs. %f",charges); getch(); } OUTPUT FOR ELECTRICITY BILL PREPARATION Enter the Units Values: 101 Total Amount Is Rs. 50.50 Enter the Units Values: 356 Total Amount Is Rs. 331.40

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.

/* Reverse Order Number */ #include<stdio.h> #include<conio.h> 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

Ex No: 15 AIM:

ARMSTRONG NUMBER GENERATION

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.

/* Armstrong Number */ #include<stdio.h> #include<conio.h> 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.

Enter the Number: 407 This is Armstrong Number.

Ex No: 16 AIM:

SINE SERIES CALCULATION

To write a program to calculate Sine value using its series using for loop. Formula: Sin(x) =x - x3/3! +X5/5!-x7/7! +----Hint: Similarly do for the cosine series Cos (x) =1-x2/2! +X4/4!-x6/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).

/* Conditional Statements */ /* Sine Series */ #include<stdio.h> #include<math.h> 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 <= n) { pwr = pwr + 2; dr = dr * pwr * (pwr - 1); sum = sum + (nr / dr) * s; s = s * (-1); nr = nr * x1 * x1; i+= 2; } printf("\n\n The Sum of the Sine Series is : %0.3f",sum); getch(); }

**** Conditional Statements ****"

**** 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 AIM:

SORTING THE GIVEN ARRAY OF NUMBERS

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).

/* Arrays */ /* 1. Array Sorting */ #include<stdio.h> #include<conio.h> 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<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t=a[j]; a[j]=a[i]; a[i]=t; } } } printf("Sorted Array\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch();

OUTPUT:

Enter Integer value for total number of elements to be sorted: 5 Enter the values for 5 elements 12 11 10 9 8 Sorted Array 8 9 10

11

12

Ex No: 18 AIM:

MATRIX ADDITION

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.

/* Matrix Addition */ #include<stdio.h> #include<conio.h> 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<n;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } } printf("\n enter the Second Matrix Value :\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&b[i][j]); } } printf("\n\n The Addition of two Matrixes\n\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<m;j++) { c[i][j]=a[i][j]+b[i][j];

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

OUTPUT:

Enter the Rows and Columns Value: 2 2 Enter First Matrix Value:

3 5

4 6

Enter the Second Matrix Value:

1 7

2 8

The Addition of two Matrixes

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.

PROGRAM: /*STUDENT MARKLIST USING STRUCTURES*/ #include <stdio.h> #include <conio.h> 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<no;i++) { printf("\n Enter The Student Reg.No. : "); scanf("%s",stud[i].regno); printf("\n Enter The Student Name : "); scanf("%s",stud[i].name); printf("\n Enter The Student Mark1 : "); scanf("%d",&stud[i].mark1); printf("\n Enter The Student Mark2 : ");

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<50) stud[i].grade='F'; else if(stud[i].percentage<70) stud[i].grade='C'; else if(stud[i].percentage<90) stud[i].grade='B'; else stud[i].grade='A'; } for(i=0;i<no;i++) { printf("Details for student %d\n",i); printf("%s \t %s \t %d\t%d\t%d\t%d\t%.2f\t%c\n", stud[i].name,stud[i].regno,stud[i].mark1,stud[i].mark2, stud[i].mark3,stud[i].total,stud[i].percentage,stud[i].grade); } getch();} OUTPUT: Enter the Number of Students: 2 Enter The Student Reg.No. : 118071001 Enter the Student Name: Anand Enter the Student Mark1: 90 Enter the Student Mark2: 78 Enter the Student Mark3: 79 Enter the Student Reg.No. : 118071007 Enter the Student Name: Sathiya

Enter the Student Mark1: 56 Enter the Student Mark2: 67 Enter the Student Mark3: 57 Details for student 0 Anand 118071001 Details for student 1 Sathiya 118071007

90 56

78 67

79 57

247 180

82.00 B 60.00 C