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

Int Int Sum Count Sum Sum Count: in If Out

The document contains examples of Java code using for and while loops to iterate over input values and perform calculations. Multiple code snippets are provided that demonstrate different looping structures and accessing input from a scanner.

Uploaded by

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

Int Int Sum Count Sum Sum Count: in If Out

The document contains examples of Java code using for and while loops to iterate over input values and perform calculations. Multiple code snippets are provided that demonstrate different looping structures and accessing input from a scanner.

Uploaded by

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

2 ‫תרגיל‬

for (int i=0;i<10;i++) {


for (int j=0;j<6;j++) {
temp = in.nextInt();
sum+=temp;
if (temp<55) count++;
}
System.out.println(sum/6);
sum=0;
}
System.out.println(count);

4 ‫תרגיל‬
int sum = 0, wage, temp;
for (int i=1;i<=500;i++) {
wage = in.nextInt();
for (int j=0;j<25;j++) {
int hours = in.nextInt();
sum+=hours;
}
System.out.println(i+": "+sum*wage);
sum=0;
}

6 ‫תרגיל‬
for (int i=1;i<=6;i++) {
for (int j=1;j<=6;j++) {
System.out.printf("(%d,%d) ",i,j);
}
System.out.println();
}
10 ‫תרגיל‬
for (int i=1;i<=4;i++) {
for (int j=0;j<i;j++) {
System.out.print("$ ");
}
System.out.println();
for (int i=4;i<=1;i--) {
for (int j=0;j<i;j++) {
System.out.print("$ ");
}
System.out.println();

for (int i=1;i<=5;i++) {


for (int j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.println();

for (int i=1;i<=5;i++) {


for (int j=1;j<=i;j++) {
System.out.print(-+" ");
}
System.out.println();

12 ‫תרגיל‬
int idk=in.nextInt(), grade=in.nextInt(), sum=0, count=0;
while (idk!=0) {
while(grade!=-999) {
sum+=grade;
count++;
grade=in.nextInt();
}
System.out.println("the average is "+sum/count);
count=0; sum=0;
idk=in.nextInt();
}
13 ‫תרגיל‬
int num=in.nextInt(), min=10, temp=num;
while (num>=100&&num<=999) {
while(temp!=0) {
if (temp%10<min) { min=temp%10; }
temp/=10;
}
System.out.println(min);
num=in.nextInt();
temp=num; min=10;
}

‫ בשיעור‬14 ‫עשינו את תרגיל‬

15 ‫תרגיל‬
int count=0, temp=2;
boolean flag=false;
for (int i=0; i<30; i++) {
int num = in.nextInt();
while (temp<=num/2) {
if (num%temp==0) flag=true;
temp++; }
if (!flag) count++;
temp=2; flag=false;
}
System.out.println(count);
5 ‫תרגיל‬
static int RandInRange(int x, int y) {
int max = Math.max(x, y), min = Math.min(x, y);
return (int) (min+Math.random()*(max-min));
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int x=in.nextInt(), y=in.nextInt();
while (x!=-999||y!=-999) {
System.out.println(RandInRange(x,y));
System.out.println("input numbers");
x=in.nextInt(); y=in.nextInt();
}
}

6 ‫תרגיל‬
static int EvenDig(int num) {
int count = 0;
while(num!=0) {
if (num%10%2==0) count++;
num/=10;
}
return count;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int n = in.nextInt(), max=0, BestNum=0;
for (int i=0; i<n; i++) {
int num = in.nextInt();
if (EvenDig(num)>max)
max=EvenDig(num);
BestNum=num;
}
System.out.println(BestNum);
7 ‫תרגיל‬
static int items(int numPpl, int capacity) {
int num=0;
if (numPpl%capacity!=0) num=1;
return num+(numPpl/capacity);
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int ppl = in.nextInt(),
buses = items(ppl, in.nextInt()),
tables = items(ppl, in.nextInt()),
boats = items(ppl, in.nextInt());
System.out.printf("%d, %d, %d", buses, tables,
boats);
}

8 ‫תרגיל‬
static int items(int num1, int num2) {
for (double i=2; i<=Math.min(num1,num2); i++) {
if ((double)num1%i==0) return (int)i;
}
return -1; }

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int count=0;
for (int i=0; i<5; i++) {
if (items(in.nextInt(),in.nextInt())==-1)
count++;}
System.out.println(count);
}

You might also like