I need some help getting this code to work.
I need to be able to Write a program that counts how many times three six-sided dice must be rolled until the values showing are all different.
Instructions:
Write a driver that generates 10 output runs.
Here is an example of two output runs.
2 6 5
count = 1
5 3 5
3 5 3
3 3 4
1 3 3
2 5 4
count = 5
Here is my code so far, I don't exactly know where and how to apply DeMorgan's law to this.
import java.util.Random;
public class P4_Icel_Murad_Rolling
{
public static void main(String[] args){
P4_Icel_Murad_Rolling obj = new P4_Icel_Murad_Rolling();
obj.rolling(10);
}
public void rolling(int number){
int counter = 0;
Random num = new Random();
for(int i = 0; i < number; i++){
int A = num.nextInt(6);
System.out.print(A + " ");
int B = num.nextInt(6);
System.out.print(B + " ");
int C = num.nextInt(6);
System.out.print(C + " ");
if((){
counter++;
}
System.out.println();
}
}
}
Try this:
(I don't know hot to apply de Morgan's Laws here.)
public static void main(String[] args){
P4_Icel_Murad_Rolling obj = new P4_Icel_Murad_Rolling();
obj.rolling(10);
}
public void rolling(int number){
int counter = 1;
Random num = new Random();
for(int i = 0; i < number; i++) {
int A = num.nextInt(6) + 1;
System.out.print(A + " ");
int B = num.nextInt(6) + 1;
System.out.print(B + " ");
int C = num.nextInt(6) + 1;
System.out.print(C + "\n");
if(A == B || A == C || B == C) {
counter++;
}
System.out.println("count = " + counter);
}
}
Related
//Program: Tribonacci Series
import java.util.*;
import java.io.*;
class Tribonacci
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number till u want Tribonacci series: ");
int n=sc.nextInt();
int a = 0,b = 0,c = 1;
int d = a+b+c;
System.out.println("\nTribonacci Series: ");
System.out.print(a+"\t"+b+"\t"+c);
for(int i=4;i<=n;i++)
{
System.out.print("\t"+d);
a=b;
b=c;
c=d;
d=a+b+c;
}
System.out.println();
}
}
output:
Enter number till u want Tribonacci series: 8
Tribonacci Series:
0 0 1 1 2 4 7 13
now i want this series value pass in array then print any index value which i want,How to do this?
You can declare a List and keep storing your generated tribonacci numbers into that list. And then can access any tribonacci number by passing index 1 to n.
Try using this code,
public static void main(String args[]) {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.print("Enter number till u want Tribonacci series: ");
int n = sc.nextInt();
int a = 0, b = 0, c = 1;
int d = a + b + c;
List<Integer> triboList = new ArrayList<Integer>();
if(n <= 0) {
System.out.println("Invalid number. Number must be greater than zero");
return;
}
System.out.println("\nTribonacci Series: ");
if (n == 1) {
System.out.print(a);
triboList.add(a);
} else if (n == 2) {
System.out.print(a + "\t" + b);
triboList.add(a);
triboList.add(b);
} else if (n >= 3) {
triboList.add(a);
triboList.add(b);
triboList.add(c);
System.out.print(a + "\t" + b + "\t" + c);
}
for (int i = 4; i <= n; i++) {
System.out.print("\t" + d);
triboList.add(d);
a = b;
b = c;
c = d;
d = a + b + c;
}
System.out.println();
System.out.println("Enter a number between 1 and " + n + " to print tribonacci number for that index.");
int index = sc.nextInt();
if (index < 1 || index > n) {
System.out.println("Invalid index. Index can only be 1 to " + n);
} else {
System.out.println(triboList.get(index - 1));
}
} finally {
if (sc != null) {
sc.close();
}
}
}
I have the following sequence of numbers:
S1 = N, S2 = S1 + 1, S3 = 2*S1 + 1, S4 = S1 + 2, S5 = S2 + 1, S6 = 2*S2 + 1, S7 = S2 + 2 ...
Using the ArrayDeque<E> class, I have to write a program to print its first 50 members for given N.
Examples:
input 2
output 2 3 5 4 4 7 5 6 11 7 5 9 6 ...
This is my code. The problem is that I can't update next S
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class p04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numN = scanner.nextInt();
scanner.close();
int counter = 1;
int nexS = numN;
Queue<Integer> fifty = new ArrayDeque<>();
for (int i = 0; i < 50; i++) {
if (i == 0){
fifty.add(numN);
}else {
if (counter == 1){
counter++;
numN = nexS + 1;
fifty.add(numN);
}else if (counter == 2){
counter++;
numN = (nexS * 2) + 1;
fifty.add(numN);
}else {
counter = 1;
numN = nexS +2;
fifty.add(numN);
nexS = nexS + 1;
}
}
}
for (Integer integer : fifty) {
System.out.print(integer + " ");
}
}
}
The way you solve this problem it's easier to solve It with ArrayList. I think that my solution is more queue oriented and that was your task. So this is my take:
import java.util.ArrayDeque;
import java.util.Scanner;
public class SequenceQuestion {
public static void constructSequence(int start, int seqLength) {
ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(start);
System.out.print(start);
for (int i = 0; i < seqLength - 1; i++) {
int print = 0;
if (i % 3 == 0 && i != 0) queue.remove();
if (i % 3 == 0) {
print = queue.peek() + 1;
queue.add(print);
} else if (i % 3 == 1) {
print = queue.peek() * 2 + 1;
queue.add(print);
} else if (i % 3 == 2) {
print = queue.peek() + 2;
queue.add(print);
}
System.out.print(", " + print);
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
constructSequence(s.nextInt(), 50);
}
}
You don't need counters because you already have one (i) and if you always check for mod 3 at the beginning and if equals to 0, remove the first element from queue. I see that this is the place you had trouble with.
I am new to Java and stackoverflow. I am writing a program that can add power in Java, for example: 2^1, 2^1+2^2, 2^1+2^2+2^3,.. so on.
I have written below program and I don't know what I am doing wrong when I am trying to add the powers. I just get 2^1 2^2 2^3,... as output. I hope you get the idea from my code and it will be a great help if you guys can help me learn.
Thank you in advance!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter b: ");
int b = sc.nextInt(); //b = second number
System.out.print("Enter t: ");
int t = sc.nextInt(); //t = no. of iterations
int x=0, sum = 0;
for (int i = 0; i < t;) {
for (int j = 0; j < t; j++) {
int pow = (int) Math.pow(2, i);
x = a + (pow * b);
i++;
System.out.printf("%d ", x);
sum = x;
}
sum = x + sum;
System.out.println(sum);
}
}
According to Mathematics rules, if it is addition among the numbers, for example 2^1 + 2^2 + 2^3 + 2^4... Then it is simple you don't need two loops and the t variable. You just need the base and the last exponent limit.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter iterations: ");
int b = sc.nextInt(); //b = No of iterations
int sum = 0;
for (int i = 1; i <= b; i++) {
sum += Math.pow(a, i);
}
System.out.println("The sum is " + sum);
}
But if there is multiplication among the numbers, then you will add the exponents if the base is same. Fox example 2^1 * 2^2 * 2^3 * 2^4.... Then you may do it as below.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base: ");
int a = sc.nextInt(); //a = first number
System.out.print("Enter iterations: ");
int b = sc.nextInt(); //b = No of iterations
Double res;
int powerSum = 0;
for (int i = 1; i <= b; i++) {
powerSum += i;
}
System.out.println("Power sum is " + powerSum);
res = Math.pow(a, powerSum);
System.out.println("The result is " + res);
}
In your inner loop
int pow = (int) Math.pow(2, i);
shouldn't you be using j instead of i?
Very simply you can do it as below:
public static int getPow(int num, int pow) {
if (pow < 2) {
return num;
}
return (int) Math.pow(num, pow) + getPow(num, --pow);
}
Usage:
int pow = getPow(2, 4);// 2*1 + 2*2 + 2*2*2 + 2*2*2*2 = 2+4+8+16 = 30
System.out.println("pow = " + pow);
And Result:
pow = 30
Here's my code so far:
public class EvenOdd
{
public static void main(String[]args)
{
System.out.print("Even numbers between 50 and 100: ");
int e = 50;
while (e <= 100) {
System.out.print(" " + e);
e += 2;
}
System.out.print("\nOdd numbers between 50 and 100: ");
int i = 51;
while (i <= 100) {
System.out.print(" " + i);
i += 2;
}
}
}
How can I reduce these two while loops to just one while loop?
Store the numbers in two StringBuilders, one for the odd numbers and the other for the even numbers :
public class EvenOdd
{
public static void main(String[]args)
{
StringBuilder even = new StringBuilder();
StringBuilder odd = new StringBuilder();
int e = 50;
while (e <= 100) {
if (e%2 == 0)
even.append (" " + e);
else
odd.append (" " + e);
e++;
}
System.out.print("Even numbers between 50 and 100: " + even.toString());
System.out.print("\nOdd numbers between 50 and 100: " + odd.toString());
}
}
public static void main(String[]args)
{
StringBuilder evenString = new StringBuilder("Even numbers between 50 and 100: ");
StringBuilder oddString = new StringBuilder("Odd numbers between 50 and 100: ");
int e = 50;
while (e <= 100) {
if((e % 2) == 0)
{
evenString.append(" " + e);
}
else
{
oddString.append(" " + e);
}
e++;
}
System.out.println(evenString);
System.out.println(oddString);
}
Use a two StringBuilders to store the strings you want to display then write them out after your done looping. Each loop do modular division (%) to see if its even or odd. If the remainder is 0 then it's even if it's not then it's odd. Based on which one it is just append it to the appropriate StringBuilder.
Reducing code duplication is often a good thing. The most basic tool that Java provides are methods, so you could extract the behavior of your loop in a method and write an equivalent program:
public class EvenOdd
{
public static void main(String[]args)
{
System.out.print("Even numbers between 50 and 100: ");
printEverySecondNumber(50, 100);
System.out.print("\nOdd numbers between 50 and 100: ");
printEverySecondNumber(51, 100);
}
public static void printEverySecondNumber(int start, int end){
int current = start;
while (current <= end) {
System.out.print(" " + current);
current += 2;
}
}
}
Note that this program behaves just the same: The loop will stil get executed twice, but it is not duplicated in code.
Use lists. They print out nicely.
public class EvenOdd
{
public static void main(String[]args)
{
List<String> even = new ArrayList<String>();
List<String> odd = new ArrayList<String>();
int e = 50;
while (e <= 100) {
if (e%2 == 0)
even.add(String.valueOf(e));
else
odd.add(String.valueOf(e));
e++;
}
System.out.print("Even numbers between 50 and 100: " + even);
System.out.print("\nOdd numbers between 50 and 100: " + odd);
}
}
Here is a solution without storing numbers
public static void main(String[] args) {
int i = 0;
System.out.print("Even numbers between 50 and 100: ");
while (i <= 50) {
if (i == 26) {
System.out.print("\nOdd numbers between 50 and 100: ");
}
if (i <= 25) {
System.out.print (" " + (2 * i + 50));
} else {
System.out.print (" " + (2 * (i - 25) + 49));
}
i++;
}
}
If you are determined to do a single loop
int n = 50;
System.out.print("Even numbers between 50 and 100:");
while(n < 151){
if(n <= 100)
System.out.print(" " + n);
else
System.out.print(" " + (n-50));
if(n != 100)
n = n + 2;
else{
System.out.print("\nOdd numbers between 50 and 100:");
n = n + 1;
}
}
The benefit to this is you aren't building any unnecessary objects or having to manage anything other than n really.
If your goal is to reduce loop iterations
StringBuilder evenString = new StringBuilder();
StringBuilder oddString = new StringBuilder();
int n = 50;
while(n <= 100){
evenString.append(" " + n);
if(n != 100)
oddString = oddString.append(" " + (n + 1));
n = n + 2;
}
System.out.println("Even numbers between 50 and 100:" + evenString);
System.out.print("Odd numbers between 50 and 100:" + oddString);
Something to notice, this cuts your iterations in half over using a single while with if statements.
Just re-start iterations by re-setting the counter:
public class EvenOdd
{
public static void main(String[]args)
{
int e = 0;
while (1) {
if (e==0) {
System.out.print("Even numbers between 50 and 100:");
e = 50;
}
else
if (e==102) {
System.out.print("\nOdd numbers between 50 and 100:");
e = 51;
}
else
if (e==101) {
System.out.print("\n");
break;
}
System.out.print(" " + e);
e += 2;
}
}
}
I'm working on a simple java code that outputs all factors of a user-inputted number. How do I count and then display the number of factors outputted?
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
}
}
In the code above, it's the number of integers outputted in 'w' For instance, if the number inputted is 8 and its factors are 1,2,4,8, how do I write a code that says '8 has 4 factors' ?
Thanks
You simply need a variable to count factors:
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
int nFactors = 0;
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
++nFactors;
}
}
System.out.println(d + " has " + nFactors + " factors");
You need a counter variable. Here is the code:
int counter =0;
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
counter++;
System.out.println(w);
}
System.out.println(d + " has " + counter + "factors ");
Try with this code:
import java.util.Scanner;
public class EmbalzadoFactorial {
public static Scanner sc;
public static void main(String[] args) {
int Number, i;
sc = new Scanner(System.in);
System.out.print("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
System.out.println("The factors are: ");
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
System.out.print ("and");
System.out.format("%s %n ", i);
}
}
}
}