Successfully reported this slideshow.

Dti2143 lab sheet 9

371 views

Published on

  • Be the first to comment

  • Be the first to like this

Dti2143 lab sheet 9

  1. 1. left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />DTI 2143: COMPUTER PROGRAMMING<br />LAB EXERCISE 9 RECURSIVE and All-round Recap<br />Exercise 1.0: Compile and run the program below<br /># include<stdio.h><br /># include<conio.h><br />void recursiveFunction(int num) {<br /> if (num < 4)<br /> recursiveFunction(num + 1);<br /> printf("%dn", num);<br />}<br />int main()<br />{<br /> int x = 2;<br /> recursiveFunction(x);<br /> <br />getch();<br />return 0;<br />}<br /><ul><li>Explain how the calculation work
  2. 2. Edit the coding so that the value of x is keyed in my the user</li></ul>Exercise 2.0: Compile and run the program below<br /># include<stdio.h><br /># include<conio.h><br />int factorial(unsigned int number)<br />{<br /> if(number <= 1)<br /> return 1;<br /> return number * factorial(number - 1);<br />}<br />int main()<br />{<br /> int x = 5;<br /> printf("factorial of %d is %d",x,factorial(x));<br />getch();<br />return 0;<br />}<br /><ul><li>Explain how the calculation work
  3. 3. Edit the coding so that the value of x is keyed in my the user</li></ul>Exercise 3.0: Compile and run the program below<br />int main(){int x,y=0; printf("Masukkan sebarang nombor ");scanf("%d",&x); <br />while ( y <x){printf( "%dn", y );y++; }<br />getchar();}<br /><ul><li>Complete the coding
  4. 4. Modify while to do-while loop
  5. 5. Modify the while loop to for loop</li></ul>Exercise 4.0: Compile and run the program below<br />printf("Enter an integer to choose a color(red=1,green=2,blue=3):n");scanf("%d",&color);switch(color){case 1: printf("you chose red colorn"); break;case 2:printf("you chose green colorn"); break;case 3:printf("you chose blue colorn"); break;default:printf("you did not choose any colorn");}<br /><ul><li>Complete the coding
  6. 6. Modify the switch case to if else selection</li></ul>Exercise 5.0: Compile and run the program below<br /> double circumferencecal(int rads){<br /> double x;<br /> float pi=3.1415;<br /> x=2*pi*rads;<br /> return(x);<br /> }<br /><ul><li>Complete the coding

×