The program should display a Reciprocal undefined message for an input of zero

# The program should display a reciprocal undefined

This preview shows page 7 - 11 out of 11 pages.

The program should display a “Reciprocal undefined” message for an input of zero. ********************************************************************/ #include<stdio.h> void main () { int number; float reciprocal; printf("please Enter an integer number: "); scanf("%d",&number); if (number>0 || number<0) { reciprocal = 1.0/number; printf("\nThe reciprocal of %d is 1/%d or %0.3f", number, number, reciprocal ); } else printf("\nReciprocal undefined"); } // end of main Page 7 of 11
Sample Output: Example#2: /************************************************************************** Write an interactive program that contains a compound if statement that may be used to compute the area of a square (area=side 2 ) or a triangle (area=base*height/2) after prompting the user to type the first character of the figure name (S or T). **************************************************************************/ /************************************************************************* Using If statement. #include<stdio.h> void main () { int side, height, base; double area; char D; printf("If you want area of a square press S or T for area of triangle :"); scanf("%c", &D); if (D=='S'||D==’s’) { printf("please input the side : "); scanf("%d", &side); area=side*side; printf("The area : %f\n", area); } else if (D=='T'||D==’t’) Page 8 of 11
{ printf("inter base and height of the triangle :"); scanf("%d %d", &base, &height); area=(0.5)*base*height; printf("The area of triangle is : %f", area); } else printf("Dear !, Your choice must be either 'S' or 'T' "); } // end of main Sample Output: /*************************************************************** Using Switch statement ****************************************************************/ #include<stdio.h> void main( ) { char choice; double side, height, base, area; printf("[To find the shape's area choose either 'S' for square , or 'T' for triangle] : "); printf("\nEnter your choice to find the area : "); scanf("%c", &choice); switch(choice) { case 'S': printf("\nEnter its side: "); scanf("%lf", &side); area = (side * side); Page 9 of 11
printf("The area of square is %.2f unit square\n", area); break; case 'T' : printf("\nEnter the height and base: "); scanf("%lf %lf", &height, &base); area = (0.5) * height * base; printf(" The area of triangle is %.2f unit square\n", area); break; default: printf("Sorry ,your choice is not included \nPlz try again "); } } // end of main Sample Output: Exercise Trace the output of the following program manually (Without using Computer) : #include<stdio.h> main( ) { int j=3, x=0; switch(j-1) { case 0 : case -1 : Page 10 of 11
x += 1; bre ak; c as e  1: c as e  2: c as e  3: x += 2; bre ak; de fault: x += 3; } printf("x = % d\n", x); } Exercise What is the output of the following program  #include <stdio.h>   void main() {  int x=10,y=5;    if(x>1)       if(x<10)          if(y>0) printf("One\n");          else printf("Two\n");       else printf("Three\n");    else if(y>0) printf("Four\n");       else printf("Five\n"); } Page 11 of 11

#### You've reached the end of your free preview.

Want to read all 11 pages?

• Spring '10
• zaman
• Equals sign, relational operator, printf