Question

This is java. Please let me know about those code. Please do not use array and...

This is java. Please let me know about those code.
Please do not use array and scanner. Just do by loop.
// a) Task: Find the sum of all multiples of 3 and 5 less than 1000.
        // b) Task: Given two integers k and n, print out every multiple of k less than n
        // c) Task: Given an integer n, print out k^k for every k=1...n
        // d) Task: Write a function called "factorProduct" that takes as input a whole number "n", and returns the product of all numbers that divide n (excluding n).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Task a:


import java.io.*;

class GFG {
   public static void main (String[] args) {
       int i,j,sum=0;
       for(i=3;i<1000;i=i+3) //multiples of 3
       sum+=i;
       for(j=5;j<1000;j=j+5) //multiples of 5
       sum+=j;
       System.out.println("Sum of all multiples of 3 and 5 less than 1000 is "+sum);
   }
}

Output:

Task b:


import java.io.*;
import java.util.*;

class GFG {
   public static void main (String[] args) {
       int k,n;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the values of k and n:");
       k=sc.nextInt();
       n=sc.nextInt();
       System.out.println("Multiples of "+k+" less than "+n+":");
       for(int i=k;i<n;i=i+k)
       System.out.println(i);
   }
}

Output:

Task c:


import java.io.*;
import java.util.*;
import java.lang.Math;

class GFG {
   public static void main (String[] args) {
       int n;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the value n:");
       n=sc.nextInt();
       for(int i=1;i<=n;i++)
       System.out.println(i+"^"+i+"= "+Math.pow(i, i));
   }
}

Output:

Task d:


import java.io.*;
import java.util.*;

class GFG {
public static int factorProduct(int n){
int product = 1;
for(int i=2;i<n;i++){
if(n%i==0)
product*=i;
}
return product;
}
   public static void main (String[] args) {
       int n;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the value n:");
       n=sc.nextInt();
       System.out.println("Product of all factors of "+n+" is "+factorProduct(n));
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
This is java. Please let me know about those code. Please do not use array and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array...

    Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array lDon real array (aiim eo ← any integer such that l < 10 < 231-1 for i = 1 to n do 4 ← remainder when /.til IS divided by 2. 31-1 231 end for

  • In Java: Create an array of Integers with 10 elements, loop (use a for loop) through...

    In Java: Create an array of Integers with 10 elements, loop (use a for loop) through the array of integers printing their values to the screen. Create an array of Strings with 10 elements, loop (use a for loop) through the array of Strings printing their values to the screen. Finally, create an ArrayList of Integers. Use the loop from part 1 above and add each integer as you print it to the ArrayList as well, then start a final...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • You are given an array A of integers in sorted order. However, you do not know...

    You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...

  • Using Java, write a program that teachers can use to enter and calculate grades of individual...

    Using Java, write a program that teachers can use to enter and calculate grades of individual students by utilizing an array, Scanner object, casting, and the print method. First, let the user choose the size for the integer array by using a Scanner object. The integer array will hold individual assignment grades of a fictional student. Next, use a loop to populate the integer array with individual grades. Make sure each individual grade entered by the user fills just one...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT