Computer programming- II (CS 2300) Java Lab Exercise #04 Student Name & Number Lab Session - #05 Loops Evaluation: Date: Monday 2/3/2015 M, 11/5/1436 H. Instructor: Maha & dhekra Assignment Policy: 1. 2. 3. 4. 5. 6. 7. Late assignments will NOT be accepted. Cheating is forbidden in this course, and will be considered a -10 mark. For any problem, you need to show steps for solving the problem and then show your implementation (code). In your code, comments have to be clearly written for each step and variable names should have a clear meaning. Your submitted work has to be neat and clean. Assignments should be stapled or placed in an unsealed envelope. Please clearly write your name, section number, and student number. Substantial departures from the above guidelines will NOT be graded. Q1: Predicating the Future Tuition Problem: Suppose that the tuition for a university is $10,000 this year and tuition increases 7% every year. In how many years will the tuition be doubled? double tuition = 10000; int year = 1 // Year 1 tuition = tuition * 1.07; year++; // Year 2 tuition = tuition * 1.07; year++; // Year 3 tuition = tuition * 1.07; year++; // Year 4 ... Program public class Q1{ public static void main(String args[]){ double tuition =10000; int year=0; while(tuition<20000){ tuition=tuition*1.07; year++; System.out.println("after "+year+" year\t, tuition= "+tuition ); } System.out.println("----------------------------------------------"); System.out.println("tuition will be doubled in "+ year +" years"); System.out.println("----------------------------------------------"); }} 1 Computer programming- II (CS 2300) Lab Session - #05 Q2: Finding the Greatest Common Divisor Problem: Write a program that prompts the user to enter two positive integers and finds their greatest common divisor. Solution: Suppose you enter two integers 4 and 2, their greatest common divisor is 2. Suppose you enter two integers 16 and 24, their greatest common divisor is 8. So, how do you find the greatest common divisor? Let the two input integers be n1 and n2. You know number 1 is a common divisor, but it may not be the greatest commons divisor. So you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2. Program import java.util.Scanner; public class Q2{ public static void main(String args[]){ Scanner input=new Scanner(System.in); int n1,n2; System.out.print("Enter the first num:"); n1=input.nextInt(); System.out.print("Enter the second num:"); n2=input.nextInt(); int k=1; int maxDivisor=1; while(k<=n1 && k<=n2) { if(n1%k==0 && n2%k==0) maxDivisor=k; k++; } System.out.println("----------------------------------------------"); System.out.println("Greatest Common Divisor= "+maxDivisor); System.out.println("----------------------------------------------"); } } 2 Computer programming- II (CS 2300) Lab Session - #05 Q3: Displaying Prime Numbers Problem: Write a program that displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Solution: The problem can be broken into the following tasks: •For number = 2, 3, 4, 5, 6, ..., test whether the number is prime. •Determine whether a given number is prime. •Count the prime numbers. •Print each prime number, and print 10 numbers per line. Program public class Q3{ public static void main(String args[]) { int num = 2; boolean isprime=true; System.out.println("First 50 prime numbers are :-"); for ( int count = 1 ; count <=50 ; ) { for ( int i = 2 ; i <= num/2 ; i++ ) { if ( num%i == 0 ) { isprime=false; break; } } if ( isprime ) { if(count%10==0) System.out.print(num+"\n"); else System.out.print(num+"\t"); count++; } isprime = true; num++; } } } 3 Computer programming- II (CS 2300) Lab Session - #05 Q4: How many times will the following code print “Tutorial Week 5” ? a) int counter=0; while(++counter <5) { System.out.println("Tutorial Week 5"); } 4 times b) int counter=0; while(counter++ <5) { System.out.println("Tutorial Week 5"); 5 times Q4: Write java program that print all multiples of 3 between 1 and 100 using for statements? public class Q4{ public static void main(String[] args({ for (int i=1;i<=100;i++) { if (i%3==0( System.out.println(i); } } } Q5: Write a java code that produce the following outputs using for loop? a) 97531 for (int i = 9; i>=1; i--) { if (i%2!=0) System.out.print(i+" ");} OR: for(int j = 9; j>=0; j-=2) System.out.print(j+ " "); b) ****4321 for (int k=8; k>=1;k--) { if (k>=5) System.out.print("*"); else System.out.print(k);} 4 Computer programming- II (CS 2300) Lab Session - #05 c) A#B#C#D#E#F#G# for (int i = 0; i<=6; i++) { System.out.print((char)('A'+ i)); System.out.print('#'); } d) 1,2 2,4 3,6 4,8 5,10 6,12 for(int i=1;i<=6;i++) System.out.println(i+","+i*2); 5

Download
# C# Lab Exercise