Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

OOP Lab 02 Loop, Array and Methdos

This lab manual for Object Oriented Programming in Java focuses on loops, arrays, and methods. It includes tasks such as creating a program to compute the sum of digits in an integer, reversing digits, counting digits, and displaying 'Hello World' multiple times. Additionally, it provides exercises for reading integers and displaying them in reverse order, along with sample code implementations.

Uploaded by

Stricker Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP Lab 02 Loop, Array and Methdos

This lab manual for Object Oriented Programming in Java focuses on loops, arrays, and methods. It includes tasks such as creating a program to compute the sum of digits in an integer, reversing digits, counting digits, and displaying 'Hello World' multiple times. Additionally, it provides exercises for reading integers and displaying them in reverse order, along with sample code implementations.

Uploaded by

Stricker Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Raya University

Collage of Engineering and Technology


Department of Electrical and Computer Engineering
Object Oriented Programming in Java-Lab 02
This lab manual is prepared by: Gebriye Embafresu E-mail: gebriye14@gmail.com
This lab is mainly focus on loop, Array and Methods that we have discussed in class
1. Design and implement a java program (name it SumDigits) to compute the sum of all
digits in an integer number. The program defines the following method:

Method sumDigits (int number) returns the sum of all digits in number.
In the main method, prompt the user for an integer input and pass the input to method sumDigits()
to obtain the sum of its digits. Then display the entered number followed by the sum of its digits
as shown below. Document your code and properly label the input prompt and the outputs as
shown below.
Sample run 1:
You entered: 12345
Sum of digits: 15
2. method to reverse the digits of number
3. method to count the number of digits of a number
Loop Related Questions:
1. Write a Java program Using for loop and while loop that will display the word “Hello
World” 50 times on the screen in new line.
2. Write a Java program using for loop that asks the user to enter a number ‘N’ from the
keyboard and then prints all numbers from 1 to N separated by space in one row on the
screen. For example: if the number is 3, the output is 1 2 3 and if the number is 5, the
output is 1 2 3 4 5
4. program to find the sum of numbers from 1 to n.
Arrays Related Questions:
1. Write a java program that prompts the user to reads ten integers from the keyboard and
displays them in the reverse of the order in which they were read.
Smaple output

©2022, Instructor: Gebriye Embafresu Website: sites.google.com/site/techroomforcs ~1~


Enter ten integers: 1 2 3 4 5 6 7 8 9 100
Output: 100 9 8 7 6 5 4 3 2 1
Answer:
import java.util.Scanner;
public class Lab02_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter ten integers: ");
int[] n = new int[10];

// passing input values to array


for (int i = 0; i < 10; i++) n[i] = input.nextInt();
// displaying array in reverse order
for (int i = n.length - 1; i >= 0; i--) System.out.print(n[i] + " ");
}
}

©2022, Instructor: Gebriye Embafresu Website: sites.google.com/site/techroomforcs ~2~

You might also like