I am trying to iterate over an array and add the sum of the array except the number 13 and the number after it.
Example
[1,1,1,1,13,2] = [1,1,1,1,0,0] = 4
this is what I have so far the main things I need to know is how do I check if the array has a number 13 in it and how do I change it to a 0
public static int sum13(int[] nums) {
for(int i=0; i < nums.length; i++) {
if(nums.indexOf(i) == 13) {
}
}
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
sum13(a);
}
}
You can try this , to skip adding all number when you get 13 in your array :
public static int sum13(int[] nums) {
int sum = 0;
for(int i=0; i < nums.length; i++) {
if(nums[i] == 13) {
break;
}
sum += nums[i];
}
return sum;
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
System.out.println(sum13(a));
}
Try this.
public static int sum13(int[] nums) {
return IntStream.of(nums).takeWhile(i -> i != 13).sum();
}
I don't know what is the problem with my code. It should print all the perfect numbers between 1-100. I tried with nested for-loop, do while loop and for-loop. However, the code seems to be incorrect.
class CompProject1
{
public static void main()
{
int num, sum=0;
int i;
for(num=1; num<100; num++)
{
for(int j = 1; j<=num ; j++)
{
if(num%j==0)
{
sum = sum+j;
}
}
if(sum==num)
{
System.out.println(sum);
}
}
}
}
Change your code to :
public static void main(String[] s1) throws Exception {
int num, sum = 0;
int i;
for (num = 1; num < 100; num++) {
for (int j = 1; j <= num - 1; j++) { // change made here
if (num % j == 0) {
sum = sum + j;
}
}
if (sum == num) {
System.out.println(sum);
}
sum = 0; // change made here
}
}
Key takeaways:
Reset sum to 0 once done with inner iteration
In your inner for-loop you need to check if till num - 1 and not num because every number is divisible by itself
1) you definitely need to reset your sum variable for every iteration, so you should do int sum = 0; in every loop.
2) you need to iterate while j <= num/2;!
3) consider using Java 8, I'll write some sample here for you.
See my example here, this is so beautiful:
public class PerfectNumbersDemo {
public static void main(String[] args) {
IntStream.range(1, 100)
.filter(PerfectNumbersDemo::isPerfect)
.forEach(System.out::println);
}
private static boolean isPerfect(int number) {
return number == IntStream.rangeClosed(1, number / 2)
.filter(i -> number % i == 0)
.sum();
}
}
This seems to be an assignment or homework question. You are meant to solve this by yourself and not ask it to the people on Stack overflow.
However, what you are looking for has an answer here. Beware! This code prints if the input number is perfect number or not but does not print all the numbers below 100 that could be perfect numbers. That is your homework.
You need to:
sum = 0 with every loop iteration
iterate until < num and not <= num
Here's the fixed code:
public static void main(String[] args) {
int sum;
for(int num = 1; num < 100; num++) {
sum = 0;
for(int j = 1; j< num; j++) {
if(num % j == 0) {
sum += j;
}
}
if(sum == num) {
System.out.println(sum);
}
}
}
Output:
6
28
So, your code have some minor problems and I will try to pinpoint them out.
1.First of all your sum variable should be inside the first for loop
2. The limit upto which the second loop will run will be j<num not j<=num because, for the perfect number, the number itself shouldn't be counted in the sum.
You code will look like this.
I don't know what is the problem with my code. It should print all the perfect numbers between 1-100. I tried with nested for-loop, do while loop and for-loop. However, the code seems to be incorrect.
class CompProject1 {
public static void main()
{
int num;
for(num=1; num<100; num++)
{
int sum = 0;
for(int j = 1; j<=num ; j++)
{
if(num%j==0)
{
sum = sum+j;
}
}
if(sum==num)
{
System.out.println(sum);
}
}
}
}
public class factors{
public static void main(String args[]){
int sum=0;
for(int k=2;k<=30;k++){
for(int i=1;i<k;i++)
{
if(k%i==0)
sum=sum+i;
}
if(k==sum)
System.out.println(sum);
sum=0; //sum=0 is very important.
}
}
}
OUTPUT
6
28
class PERFECT
{
public static void main(String args[])
{
int i,j,S,
for(i=1;i<=100;i++)
{
S=0
for(j=1;j<i;j++)
{
if(i%j==0)
S+=j;
if (S==i)
System.out.println(i+"is perfect");
}
}
}
}
Here is an alternate way of finding perfect numbers.
if 2p-1 is prime when p is prime. Then (2p-1)(2p-1) is a perfect number. 2p-1 is known as a Mersenne Prime
As these numbers get real big real fast, using BigInteger is recommended.
This computes the first 10 perfect numbers.
int N = 10;
int count = 1;
for(int i = 2; i < 10_000; i += i == 2 ? 1 : 2) {
BigInteger val = BigInteger.valueOf(i);
if (val.isProbablePrime(99)) {
BigInteger mersenne1 = (BigInteger.ONE.shiftLeft(i)).subtract(BigInteger.ONE);
if (!mersenne1.isProbablePrime(99)) {
continue;
}
BigInteger mersenne2 = BigInteger.ONE.shiftLeft(i-1);
System.out.printf("%3d: %,d\n",count, mersenne1.multiply(mersenne2));
if (count++ >= N) {
break;
}
}
}
prints
1: 6
2: 28
3: 496
4: 8,128
5: 33,550,336
6: 8,589,869,056
7: 137,438,691,328
8: 2,305,843,008,139,952,128
9: 2,658,455,991,569,831,744,654,692,615,953,842,176
10: 191,561,942,608,236,107,294,793,378,084,303,638,130,997,321,548,169,216
public class ClassOne {
public static void main(String[] args) {
System.out.println(av(5, 6, 7));
}
public static int av(int... numbers) {
int total = 0;
for (int x: numbers) total = total += x;
return total / numbers.length;
}
}
I am a java beginner.
I can't understand really why it fails if you had intuitively added two curly brackets at the end of the "for loop" like this below, which accordingly to presently acquired knowledge looks the most normal and natural to do it.
for (int x: numbers) {
total = total += x;
return total / numbers.length;
}
You can check that now. It fails after you added the curly brackets:
public class ClassFour {
public static void main(String[] args) {
System.out.println(av(5, 6, 7));
}
public static int av(int... numbers) {
int total = 0;
for (int x: numbers) {
total = total += x;
return total / numbers.length;
}
}
}
I would be grateful if anyone could clarify. Thanks.
I need help on how to calculate sum of the numbers that while loop prints. I have to get numbers 1 to 100 using while loop and calculate all those together. Like 1+2+3...+98+99+100. I can get numbers but can't calculate them together. Here's my code:
public class Loops {
public static void main(String[] args) throws Exception {
int i = 1;
while (i < 101) {
System.out.print(i);
i = i + 1;
}
}
}
How do I make it only print the last sum? If I try to trick the equation it just hangs.
Use another variable instead of i which is the loop variable:
int i = 1;
int sum = 0;
while (i < 101) {
sum += i;
i++;
}
Now sum will contain the desired output. In the previous version, you didn't really loop on all values of i from 1 to 101.
First either change your sum variable or index variable
public class Loops {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i < 101) {
sum = sum + i;
++i;
}
System.out.println(sum);
}
Your sum (i) is also your index. So each time you add to it, you're skipping numbers which you want to add to it.
public class Loops {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i < 101) {
//System.out.print(i);
sum = sum + i;
++i;
}
System.out.println(sum);
}
Alternatively, use the Gauss sum: n(n+1)/2
So, the end sum is 100(101)/2 = 5050
Try the following. Moved the values around a bit to ensure that you add up to 100 and always show the sum.
public static void main(String[] args) throws Exception {
int i = 1;
long tot = 1;
while (i < 100) {
i += 1;
tot += i;
System.out.print("Number :" + i + " ,sum="+tot);
}
}
public class Loops {
public static void main(String[] args) throws Exception {
int i = 1;
int sum = 0;
while (i < 101) {
sum = i + 1;
}
System.out.print(sum);
}
}
Here is an easy one that I am having trouble with. The problem requires me to write a method called fractionSum which accepts an integer parameter and returns a double of the sum of the first n terms.
for instance, if the parameter is 5, the program would add all the fractions of (1+(1/2)+(1/3)+(1/4)+(1/5)). In other words, it's a form of the Riemann sum.
For some reason, the for loop does not accumulate the sum.
Here's the code:
public class Exercise01 {
public static final int UPPER_LIMIT = 5;
public static void main(String[] args) {
System.out.print(fractionSum(UPPER_LIMIT));
}
public static double fractionSum(int n) {
if (n<1) {
throw new IllegalArgumentException("Out of range.");
}
double total = 1;
for (int i = 2; i <= n; i++) {
total += (1/i);
}
return total;
}
}
you need to type cast to double
try this way
public class Exercise01 {
public static final int UPPER_LIMIT = 5;
public static void main(String[] args) {
System.out.print(fractionSum(UPPER_LIMIT));
}
public static double fractionSum(int n) {
if (n<1) {
throw new IllegalArgumentException("Out of range.");
}
double total = 1;
for (int i = 2; i <= n; i++) {
total += (1/(double)i);
}
return total;
}
}
The operation
(1/i)
is working on integers and hence will generate the result in terms of int. Update it to:
(1.0/i)
to get the fractional result and not the int result.