Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is what i have tryied but i cant find the solution plz help

What I have tried:

package com.dibyendu;


import java.util.Scanner;

public class assign11 {
    public static void main(String[] args) {
        //  n = 1
        // 3n + 2 = 5 Not a multiple of 4
        //  n = 2
        // 3n + 2 = 8 is a multiple of 4 ... stop

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println("Enter a number");
        int sum;
        for (; sum % 4 != 0; n++)

            sum = 3 * n + 2;
        System.out.println(sum);


    }
}
Posted
Updated 24-Jul-20 20:22pm
Comments
Richard MacCutchan 25-Jul-20 4:56am    
Same answer as yesterday.

1 solution

Read the question carefully.
It says that you need to read a value from the user into a variable called n . Do you do that?
Then you need to a loop that runs 20 times - to print 20 terms.
Inside the loop, calculate 2 * n + 1 and store it in n
Print the new value of n
Check n: if it is a multiple of 4, exit the loop.

It's not complicated, just do each =step as a separate item, test it, and when it works properly, move on to the next step.

Give it a try!
 
Share this answer
 
Comments
Callme Dibyendu 25-Jul-20 3:55am    
thank you very much sir i got it please check sir is it correct or not

package com.dibyendu;


import java.util.Scanner;

public class assign11 {

public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
int n =sc.nextInt();
for(int sum;n<=20;n++)
{
sum=3*n+2;
System.out.println(sum);
if(sum%4==0){
break;
}



}
}
}
OriginalGriff 25-Jul-20 3:58am    
Stop panicking, start thinking.
Look at what I said, and think about each step in turn.
You've done the first one - read the input from the user.
Have you done the second? Really? Are you sure?

Don't try to "jump ahead" - do each bit in sequence and make sure it works before you move on - that way you don't have so many complications to think about each time.
Callme Dibyendu 25-Jul-20 4:11am    
sir i need help can i contact u in other platform please sir in telegram or anywhere please sir please....
Callme Dibyendu 25-Jul-20 4:12am    
sir your are great please sir give me opportunity to be your student i want to learn something from you
OriginalGriff 25-Jul-20 4:27am    
Sorry, but I can't: we are all volunteers here, most of us with "actual jobs" that take precedence (they pay the rent and food bills!) and mentoring someone is a massive amount of time and energy that I just don't have to spare. And I get this request a couple of times a month - I couldn't take one everyone even if I wanted to! You already have a teacher and probably a course book to follow - try working with that and see how far you can get.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900