Download presentation
Presentation is loading. Please wait.
1
TK1114 Computer Programming
7 - while loop TK1114 Computer Programming
2
Repetition Statements (Loop)
Repetition statements allow us to execute a statement multiple times Java has three kinds of repetition statements: the for loop the while loop the do-while loop The programmer should choose the right kind of loop for the situation
3
for loop An example : for (initialization; condition; increment){
statement true condition evaluated false increment initialization for (initialization; condition; increment){ statements; } An example : for (int count=1; count <= 5; count++){ System.out.println (count); }
4
while Loop while ( condition ){ statement; } An example:
true false condition evaluated while ( condition ){ statement; } An example: int count = 1; while (count <= 5) { System.out.println (count); count++; }
5
do Loop An example : do { statement; } while ( condition )
true condition evaluated statement false do { statement; } while ( condition ) An example : int count = 0; do { count++; System.out.println (count); } while (count < 5);
6
Keep Positive Problem Description
Write a program that reads an unspecified number of integers, determines how many positive value have been read. Your program ends with the input 0. Input The data set consists of a list of positive integer, num (0 < num ≤ 100) that ends with 0. Output There is only one output for this problem, which is the number of integers positive. Sample Input Output Sample Input Sample Output 6
7
Keep Positive import java.util.Scanner; public class KeepPositive{ 2
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num; int count = 0; do { num = sc.nextInt(); if (num > 0) count++; } while (num != 0); System.out.println(count); } 2 -3 4 -5 6 7 -8 9 10 6
8
Min Max Problem Description
Write a program that reads 10 integers. Find the smallest and largest numbers. Input 10 integers num (0 < num ≤ 100). Output The smallest and largest integer. Sample Input Output Sample Input Sample Output 1 27
9
Min Max import java.util.Scanner; public class KeepPositive{ 12
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num; int max = 0, min = 100; for (int count = 0; count<10; count++) { num = sc.nextInt(); if (num > max) max = num; if (num < min) min = num; }; System.out.println(min + " " + max); } 12 3 2 5 4 27 1 9 10 7 1 27
10
Sum of digits of numbers
Problem Description Write a program to ask user for a positive integer and calculating sum of its digits then. Input integer positive number (n1>0 ). Output Sum of the digits. Sample Input Output Sample Input Sample Output 123 6
11
Sum of digits of numbers
Analysis Input: 123 Output: = 6 We can use modulo operator to solve this problem. 123 % 10 = 3 123 / 10 = 12 12 % 10 = 2 12 / 10 = 1
12
Sum of digits of numbers
public class KeepPositive{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int num, lastnum=0, sum=0; num = in.nextInt(); if (num>0){ while (num != 0){ lastnum = num % 10; sum += lastnum; num /= 10; } System.out.println(sum); 123 6
13
Area of a circle Problem Description
Write a program to calculate the area of a circle. Input The first line of input is the number of input n, where 1 ≤ n ≤ 20. For each of the following n lines there are radius for the circle. Output For each input, print the area. Sample Input Output Sample Input Sample Output 4 3 34 12 28.26 452.16 50.24
14
Area of a circle import java.util.Scanner; public class Trial {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); double radius; double area; int n; n = sc.nextInt(); for (int i=0; i<n; i++){ radius = sc.nextDouble(); area = radius * radius * 3.14; System.out.println (area); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.