Bti1022 lab sheet 9 10

674 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
674
On SlideShare
0
From Embeds
0
Number of Embeds
64
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Bti1022 lab sheet 9 10

  1. 1. left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />BTI 1022: 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 by 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 by the user</li></ul>LAB EXERCISE 10 All-round Recap<br />Exercise 1.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 2.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 3.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</li></ul>Exercise 4.0: Compile and run the program below<br />#include<stdio.h><br />int conditionals(int a,int b);<br />int main(void)<br />{<br />int result;<br />result=conditionals(0,0);<br />printf(“%dn”,result);<br />result=conditionals(-2,5);<br />printf(“%dn”,result);<br />result=conditionals(4,-1);<br />printf(“%dn”,result);<br />return 0;<br />}<br />int conditionals(int a, int b){<br /> if (a>0||b<0){<br />return 100;<br />}else if (a+b>0){<br />return 200;<br />}else{<br />return 300;<br />}}<br /><ul><li>Explain how the calculation work</li></ul>Exercise 4.0: Compile and run the program below<br />#include<stdio.h><br />int main()<br />{<br />int i, j, k;<br />printf(“First: n”);<br />i=5;<br />while(i<10) {<br />i++;<br />printf(“%d”,i ) ;<br />}<br />printf(“nSecond: n”);<br />for (j=10;j>5;j--){<br />printf(“%d”,16-j);<br />}<br />printf(“nThird: n”);<br />for (k=6;k<=10;k++){<br />printf(“%d”,k);<br />}<br />return 0;<br />}<br /><ul><li>Explain how the calculation work

×