You are on page 1of 5

Lab No.

2 (Advance)

Name: YOGESH PARTH Dept: Avionics Question 1: Male to Female Ratio of the class. Program code: #include<stdio.h> #include<conio.h> Void main () { int m,f,r; clrscr(); printf("enter the no. of male student"); scanf("%d",&m); printf("enter the no. of female student"); scanf("%d",&f); r=(m/f); printf("ratio=%d",r); getch(); } Result: Enter the no. of male student 20 Enter the no. of female student 0 Discussion: The value of ratio is infinite so result didnt come.

Roll No. SC11B128 Sub: C Programming Lab

Question 2: Write a C Program to display the score of negative and positive numbers provided to program through keyboard. Terminate the program if user provides 0 as an input. Program code: #include<stdio.h> #include<conio.h> Void main () { Int number, positive =0, negative =0; do { printf("Number of positive numbers:%d \nNumber of negative numbers:%d\n",positive,negative);

printf("Enter any number:%d\n",number); scanf("%d",&number); if (number > 0) positive++; else if (number < 0) negative++; getch(); } while (number!=0); } Result: Number of positive numbers:0 Number of negative numbers:0 Enter any number:2293576 12 Number of positive numbers:1 Number of negative numbers:0 Enter any number:12 8 Number of positive numbers:2 Number of negative numbers:0 Enter any number:8 0 Press any key to continue . . Discussion: Use of do and while loop to terminate the program if 0 is provided as an input. Otherwise showing the result.

Question 3: Write a C program to divide two integers entered through keyboard and store the result in the variable result declared as float. Program code: #include<stdio.h> #include<conio.h> void main() { float result; int num1, num2; clrscr(); printf("Enter any 2 numbers\n"); scanf("%d%d",&num1,&num2); result = num1/num2; printf("\n %d/%d = %f",num1,num2,result); getch(); } Result:

Enter any 2 numbers 82 8/2 = 4.000000Press any key to continue . . . Discussion: We are making float equals to integer to perform the operation . Question 4: Write a program to read a five digit number and print the sum of its digits. Program code: #include<stdio.h> #include<conio.h> Void main() { long u; int v, w, x, y, z; printf("Enter any 5 digit number \n"); scanf("%ld",&u); v = (u%10); w= ((u%100)-v)/10; x= ((u%1000)-(u%100))/100; y = ((u%10000)-(u%1000))/1000; z= (u - (u%10000))/10000; printf("The sum of the digits of %ld is %d",u, v+w+x+y+z); getch(); } Result: Enter any 5 digit number 53689 The sum of the digits of 53689 is 31 Discussion: We are using long integer command ld to increase the range of numbers and using %to get the remainder of the number.

Question 5: To sweep the value of two variables with and without using third variable. Program code: 1. With third variable #include <stdio.h> #include<conio.h> void main() { int x, y, z; printf("Enter the value of x and y\n");

scanf("%d%d", &x, &y); z = x; x = y; y = z; printf("After Swapping\nx = %d\ny = %d\n",x,y); getch(); } 2. Without third variable #include <stdio.h> #include<conio.h> int main() { int a, b; printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b; printf("a = %d\nb = %d\n",a,b); getch(); } Result: Enter the value of x and y 2 5 X=5 Y=2 Enter two integers to swap 3 6 a=6 b=3 Discussion: We are using = to give the value of one variable to another and using a particular way to sweep the value of variables.

Question 6.(Bonus) Write a program to display the sine and cosine values for the interval 0 to 180 degrees in increment of 30 and display the result in table. Program code: #include<stdio.h> #include<conio.h> #include<math.h> #define pi 3.1416 #define max 180 main() { int angle; float x,y,z; angle= 0;

printf("angle\t\tcos(angle)\t\tsin(angle)\n"); while(angle<=max) { x=(pi/max)*angle; y=cos(x); z=sin(x); printf("%15d\t\t%13.4f\t\t%13.4f\n",angle,y,z); angle=angle+30; } } Result: A table is generated showing the value of cos(angle) and sin(angle) value for the interval 0 to 180 degree. angle cos(angle) sin(angle) 0 1.0000 0.0000 30 0.8660 0.5000 60 0.5000 0.8660 90 -0.0000 1.0000 120 -0.5000 0.8660 150 -0.8660 0.5000 180 -1.0000 -0.0000 Press any key to continue . . . Discussion: The While function is used to perform the operation. <math.h> is used for the calculation of sin(angle) and cos (angle).

You might also like