Problem 1 |
public void stars()
{
for(int i=1; i<=3; i++)
System.out.println("**********");
}
|
Problem 2 |
public void moreStars()
{
for (int i=1; i<=40; i++)
System.out.print("*");
} |
Problem 3 |
public void looping()
{
for (int i=1; i<=10; i++)
System.out.println("Number of times looped: "+i);
} |
Problem 4 |
public void fourToEightyFour()
{
for(int i=4; i<=84; i+=10)
System.out.print(i+" ");
} |
Problem 5 |
public void downToEleven()
{
for (int i=99; i>=11; i-=11)
System.out.print(i+" ");
} |
Problem 6 |
public void blastOff()
{
for(int i=10; i>=1; i--)
System.out.print(i+"...");
System.out.print("Blastoff!");
} |
Problem 7 |
public void steppingUp()
{
int step;
System.out.print("Enter the step value: ");
step=in.nextInt();
for (int i=8; i<=20; i+=step)
System.out.print(i+" ");
} |
Problem 8 |
public void multiplicationTable()
{
System.out.println("X X^2 X^3");
System.out.println("___________");
for (int i=4; i<=10; i+=2)
System.out.println(i+" "+" "+i*i+" "+" "+i*i*i);
} |
Problem 9 |
public void drivingAge()
{
int age, wait;
for (int i=1; i<=3; i++)
{
System.out.print("Enter the age of person number "+i+" :");
age=in.nextInt();
wait=16-age;
if(age>=16)
System.out.println("You can drive.");
else
System.out.println("You must wait "+wait+" years to drive.");
}
System.out.print("Goodbye.");
}
|
Problem 10 |
public void roomLabels()
{
for (int i=1; i<=25; i++)
{
System.out.println("________________");
System.out.println("Cheap Stay Motel");
System.out.println(" Room "+i);
System.out.println("________________");
System.out.println();
}
} |
Problem 11 |
public void numsAroundFive()
{
int num;
for (int i=1; i<=10; i++)
{
num=(int)(Math.random() *10) +1;
if (num>5)
System.out.println("On turn number "+i+" the number "+num+" was chosen and found to be greater than 5.");
else if (num<5)
System.out.println("On turn number "+i+" the number "+num+" was chosen and found to be less than 5.");
else
System.out.println("On turn number "+i+" the number "+num+" was chosen and found to be equal to 5.");
}
} |
Problem 12 |
public void summingRandomNums()
{
int num, sum=0;
System.out.print("The numbers chosen are: ");
for (int i=1; i<=3; i++)
{
num=(int)(Math.random() * 11) + 5;
sum+=num;
System.out.print(num+" ");
}
System.out.println();
System.out.println("The sum is "+sum);
} |
Problem 13 |
public void sumOddNumbers()
{
System.out.print("The sum of ");
for (int i=1; i<=15; i+=2)
{
if (i!=15)
System.out.print(i+"+");
else
System.out.print(i);
}
System.out.print(" is 64.");
} |
Problem 14 |
public void evenOrOdd()
{
int num, odd=0, even=0;
for (int i=1; i<=1000; i++)
{
num= (int)(Math.random() * 10)+0;
if (num%2==0)
even++;
}
System.out.println("There were "+even+" even numbers randomly chosen.");
odd=1000-even;
System.out.println("There were "+odd+" odd numbers randomly selected.");
} |
Problem 15 |
public void main factors()
{
int num;
System.out.print("Enter a positive whole number please: ");
num=in.nextInt();
System.out.print("The factors of "+num+" are ");
for (int i=1; i<=num; i++)
{
if (num%i==0)
System.out.print(i+" ");
}
} |
Problem 16 |
public void rollingDice()
{
int roll=0, die1=0,die2=0,turn=0;
while (roll!=12)
{
die1=(int)(Math.random() *6) +1;
die2=(int)(Math.random() *6) +1;
roll=die1+die2;
System.out.println("Roll "+turn+" = "+roll);
turn++;
}
} |
Problem 17 |
public void loggingIn()
{
String user="Bob", password="tomato", enterName="", enterPass="";
while (user.compareTo(enterName)!=0 | password.compareTo(enterPass) !=0)
{
System.out.print("Enter user name: ");
enterName=in.next();
System.out.print("Enter a password: ");
enterPass=in.next();
if(user.compareTo(enterName)!=0 | password.compareTo(enterPass) !=0)
System.out.println("Either the user or password are incorrect, please try again.");
}
System.out.print("Welcome "+user+", you now have access to the file server.");
}
|
Problem 18 |
public void guessingGame()
{
int rndmNum,guess=0;
rndmNum=(int)(Math.random()*100)+1;
while (guess != rndmNum)
{
System.out.print("Guess my number: ");
guess=in.nextInt();
if (guess>rndmNum)
System.out.println("Too high.");
else if (guess<rndmNum)
System.out.println("Too low.");
}
System.out.print("Correct!!");
} |
Problem 19 |
public void guessingGame()
{
int rndmNum,guess=0,turns=6;
rndmNum=(int)(Math.random()*100)+1;
while (guess != rndmNum & turns>0)
{
if(turns==1)
System.out.print("Guess my number, you have "+ turns + " turn left: ");
else
System.out.print("Guess my number, you have "+ turns + " turns left: ");
guess=in.nextInt();
if (guess>rndmNum)
System.out.println("Too high.");
else if (guess<rndmNum)
System.out.println("Too low.");
turns--;
}
if(guess==rndmNum)
System.out.print("Correct!!");
else
System.out.println("Sorry, you lose. The number was "+rndmNum+".");
} |
Problem 20 |
public void sumTheNumbers()
{
int entry=0, num=0, sum=0;
while (entry<3 | entry>8)
{
System.out.print("Enter a number from 3 to 8: ");
entry=in.nextInt();
if(entry<3 | entry>8)
System.out.println("Invalid entry.");
}
System.out.print("The numbers chosen are: ");
for(int i=1; i<=entry; i++)
{
num=(int)(Math.random() *20)+1;
sum+=num;
System.out.print(num+" ");
}
System.out.println();
System.out.print("The total is "+sum+".");
} |
Problem 21 |
public void addingGame()
{
int first=0, num=0, compNum=0, total=0;
String play="y";
while (play.compareToIgnoreCase("y")==0)
{
while (first!=1 && first!=2)
{
System.out.print("Do you want to go first? (1 or 2): ");
first=in.nextInt();
}
while (total<=100)
{
if (first==1)
{
num=0;
while (num<3 || num>12)
{
System.out.print("Enter your choice (3-12): ");
num=in.nextInt();
}
total+=num;
System.out.println("The total is now "+total);
first=2;
}
else
{
compNum=(int)(Math.random() * 10)+3;
total+=compNum;
System.out.println("I choose "+compNum);
System.out.println("The total is "+total);
first=1;
}
}
if (first==1)
System.out.println("I win. There are none that can defeat my massive computer brain.");
else
System.out.println("You win. Congratulations cheater.");
do
{
System.out.print("Do you want to play again? (Y/N): ");
play=in.next();
}
while (play.compareToIgnoreCase("y")!=0 && play.compareToIgnoreCase("n")!=0);
total=0;
first=0;
}
} |
Problem 22 |
public class Problem_22
{
public void questions()
{
for (int i=1; i<=5; i++)
{
for (int j=1; j<=20; j++)
{
System.out.print("?");
}
System.out.println();}
}
} |
Problem 23 |
public class Problem_23
{
public void rowsOfLetters()
{
int rows, width;
String letter;
System.out.print("Enter the number of rows: ");
rows=in.nextInt();
System.out.print("Enter the width of the row: ");
width=in.nextInt();
System.out.print("Enter the letter to use: ");
letter=in.next();
for (int i=1; i<=rows; i++)
{
System.out.println();
for (int j=1; j<=width; j++)
{
System.out.print(letter);
}
}
}
} |
Problem 24 |
public class Problem_24
{
public void ampersandsTriangle()
{
for (int i=1; i<=5; i++)
{
for (int j=1; j<=i; j++)
{
System.out.print("&");
}
System.out.println();
}
}
} |
Problem 25 |
public class Problem_25
{
public void ampersandsTriangle2()
{
for (int i=1; i<=5; i++)
{
for (int j=5; j>=i; j--)
{
System.out.print("&");
}
System.out.println();}
}
}
} |
Problem 26 |
public class Problem_26
{
public void inOrOut()
{
for (int i=1; i<=3; i++)
{
System.out.println("The value of the outside loop is "+i);
for (int j=1; j<=3; j++)
{
System.out.println("The value of the inside loop is "+j);
}
{
}
} |
Problem 27 |
public class Problem_27
{
public void xandy()
{
for (int r=20; r<=24; r++)
{
System.out.println("X="+r);
for (int i=1; i<=3; i++)
{
System.out.print("Y="+i+" ");
}
System.out.println();
}
}
} |
Problem 28 |
public class Problem_28
{
public void coins()
{
int total;
System.out.println("Quarters Dimes Nickels");
for (int quarter=0; quarter<=2; quarter++)
for (int dime=0; dime<=5; dime++)
for ( int nickel=0; nickel<=10; nickel++)
{
total=5*nickel+dime*10+25*quarter;
if (total==50)
System.out.println(quarter+" "+dime+" "+nickel);
}
}
} |
Problem 29 |
public class Problem_29
{
public void moreCoins()
{
int total;
System.out.println("Half Dollars Quarters Dimes Nickels");
for (int half=0; half<=2; half++)
for (int quarter=0; quarter<=4; quarter++)
for (int dime=0; dime<=10; dime++)
for ( int nickel=0; nickel<=20; nickel++)
{
total=5*nickel+dime*10+25*quarter+50*half;
if (total==100)
System.out.println(half+" "+quarter+" "+dime+" "+nickel);
}
}
} |
Problem 30 |
public class Problem_30
{
public void factorializer()
{
int factorial=1;
for (int i=1; i<=10; i++)
{
for (int j=i; j>=1; j--)
{
factorial*=j;
}
System.out.println(i+"! Is "+factorial);
factorial=1;
}
}
} |
Problem 31 |
public class Problem_31
{
public void primalNumbers()
{
int num, primeCheck=1;
System.out.print("The prime numbers are: ");
for (int i=2; i<=50; i++)
{
for (int j=i-1; j>=1; j--)
{
num=i%j;
if (num==0 && j!=1)
primeCheck=2;
}
if (primeCheck==1)
System.out.print(i+" ");
primeCheck=1;
}
}
} |
|
|
|