0% found this document useful (0 votes)
38 views

Exercises

The document contains 13 questions about using for loops in Java programs. It provides sample code solutions to problems like displaying odd numbers between 1-100, finding divisors of a number, checking if a number is perfect, finding minimum/maximum of input numbers, checking if a number is prime, displaying digits of a number, and displaying even numbers between 1-100. The code examples use for loops to iterate through conditions and display or return results.

Uploaded by

Amine Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Exercises

The document contains 13 questions about using for loops in Java programs. It provides sample code solutions to problems like displaying odd numbers between 1-100, finding divisors of a number, checking if a number is perfect, finding minimum/maximum of input numbers, checking if a number is prime, displaying digits of a number, and displaying even numbers between 1-100. The code examples use for loops to iterate through conditions and display or return results.

Uploaded by

Amine Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Exercises Java TS1 RS

chapterid Question
java for loop

Write a java program (class) that display the odd integer numbers between 1 and 100 using for loop.

class odd1to100{
public static void main(String args[]){
int i;
for(i=1;i<=100;i=i+2)
System.out.println(i);
}}

Page 1 of 13
chapterid Question
java for loop

4-Write a java program (class) that display the divisors of an argument integer number using for loop.

class divisors{
public static void main(String args[]){
int i,x;
x=Integer.parseInt(args[0]);
for(i=1;i<=x/2;i++)
if (x%i==0)
System.out.println(i);
System.out.println(x);}}

to compile: c:\jdk1.2.1\bin\Javac divisiors.java


example to run:
c:\jdk1.2.1\bin\java divisors 35
c:\jdk1.2.1\bin\java divisors 100

Page 2 of 13
chapterid Question
java for loop

5-Write a java program (class) that display if


an argument integer number is perfect or no using for loop. Perfect number mean its double value=sum of its divisors.

class perfect{
public static void main(String args[]){
int i,x,s=0;
x=Integer.parseInt(args[0]);
for(i=1;i<=x/2;i++)
if (x%i==0)
s=s+i;
s=s+x;
if (s==2*x)
System.out.println(x+” is a perfect number”);
else
System.out.println(x+” is not a perfect number”);}}

Page 3 of 13
chapterid Question
java for loop

9-Write a java program that accept 10 integer numbers as arguments then to display their minimum value.

class minimum{
public static void main(String args[]){
int min,x,I;
min=Integer.parseInt(args[0]);
for(i=1;i<=9;i++){
x=Integer.parseInt(args[i]);
if (x<min)
min=x;
}
S.o.p(min);
}}

Page 4 of 13
chapterid Question
java for loop

10-Write a java program that accept 10 integer numbers as arguments then to display their minimum and maximum values.

Class maxmin{
Public static void main(String args[]){
int min,max,x,i;
x=Integer.parseInt(args[0]);
max=x;
min=x;
for(i=1;i<=9;i++){
x=Integer.parseInt(args[i]);
if (x<min) min=x;
else
if (x>max) max=x;}
S.o.pln("minimum is"+min);
S.o.pln("maximum is"+max);
}}

Page 5 of 13
chapterid Question
java for loop

1-Write a java code to display if an argument integer a is prime or not.


2-Write a java code to display the first 50 integer numbers if primes or not.
3-Write a java code to display the list of integer numbers between an argument lower limit and argument upper lint if primes or not.

1-class prime1{
public static void main(String [] args){
int x,i;
boolean isprime=true;
x=Integer.parseInt(args[0]);
for(i=2;i<=x/2;i++)
if (x%i==0)
{isprime=false;break;}
System.out.println(x + " isprime "+isprime);
}}

--OR

1-class prime2{
public static void main(String [] args){
int x,i;
boolean isprime=true;
x=Integer.parseInt(args[0]);
for(i=2;i<=x/2;i++)
if (x%i==0)
{isprime=false;break;}
if (isprime==true)
System.out.println(x + " is prime number");
else
System.out.println(x + " is not prime number");
}}

--OR
2-class prime3{
public static void main(String [] args){
int x,I;
boolean isprime;

Page 6 of 13
chapterid Question
for(x=1; x<=50;x++){
isprime=true;
for(i=2;i<=x/2;i++)
if (x%i==0)
{isprime=false;break;}
System.out.println(x + " isprime "+isprime);
}
}}

java while loop

7-Write a java program (class) that accept an integer number as argument then display its digits, one digit each row.

class Digits1{
public static void main(String args[]){
int i,x;
x=Integer.parseInt(args[0]);
while (x>0) {
i=x%10; x=x/10; System.out.println(i);}
}}

Page 7 of 13
chapterid Question
java while loop

10-Write a java program that accept 10 integer numbers as arguments then to display their minimum and maximum values.

Class maxmin{
Public static void main(String args[]){
int min,max,x,i=1;
x=Integer.parseInt(args[0]);
max=x;
min=x;
while(i<=9)
{
x=Integer.parseInt(args[i]);
if (x<min) min=x;
else
if (x>max) max=x;
i++;}
S.o.pln("minimum is"+min);
S.o.pln("maximum is"+max);
}}

Page 8 of 13
chapterid Question
java for loop

2-Write a java program (class) that display the even integer numbers between 1 and 100 using for loop.

Class loop_even{
Public static void main(String args[]){
int i;
for(i=2;i<=100;i=i+2)
{
System.out.println(i);
}

}}
i
2-
4
6
8
.
.
.
98
100

i=102

Page 9 of 13
chapterid Question
java for loop

2-Write a java program (class) that display the even integer numbers between 1 and 100 using for loop. (second method)

class loop_even{
public static void main(String args[]){
int i;
for(i=1; i<=100; i++)
If (i%2==0)
System.out.println(i);}}

I
--
1
2 -

Page 10 of 13
chapterid Question
java for loop

1-Write a java program (class) that display the integer numbers between 1 and 10 using for loop.

Class loop1{
Public static void main(String args[]){
int i;
for(i=1; i<=10; i++)
System.out.println(i);
}}

--OR

Class loop1{
Public static void main(String args[]){
int i=1;
for(; i<=10; i++)
System.out.println(i);
}}

--OR ..the default value of step is +1 in ascending and -1 in descending

Class loop1{
Public static void main(String args[]){
int i=1;
for(; i<=10; )
System.out.println(i);
}}

int limit=10,s=0;
for(int i=1,j=0; i<limit; s+=i*j,i++, j++);

Page 11 of 13
chapterid Question
java for loop

3-Write a java program (class) that display the divisors of the number 35 using for loop.

Class loop_divisors{
Public static void main(String args[]){
Int i;
for(i=1; i<=35; i++)
if (35%i==0)
System.out.println(i);}}

Page 12 of 13
chapterid Question
java for loop

3-Write a java program (class) that display the divisors of the number 35 using for loop. (second method)

Class loop_divisors{
Public static void main(String args[]){
Int i;
System.out.println(“1”);
for(i=2;i<=35/2;i++)
if (35%i==0)
System.out.println(i);
System.out.println(“35”);}}

Page 13 of 13

You might also like