125k views
4 votes
Write a function called activity which takes an integer parameter X that does the following:

• Prints the heading “Numbers:” followed by all numbers from 1 to X
• Prints the heading “Evens:” followed by all even positive numbers less than X
• Prints the heading “Activity” followed by the numbers from 1 to X with the following

replacement
• Multiples of 5 replaced with the string “fiver” • Multiples of 7 replaced with the string “ssss” • Other numbers printed as-is

User Slashterix
by
5.4k points

1 Answer

0 votes

Answer:

here is code in java.

import java.util.*;

class Main

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// Scanner class object to read the input integer

Scanner scr=new Scanner(System.in);

// ask user to enter the input

System.out.print("enter a number:");

// read the input

int x=scr.nextInt();

// call the function with parameter "x"

activity(x);

}catch(Exception ex){

return;}

}

// activity function to perform given tasks

public static void activity(int x)

{

// print all the Numbers from 1 to x

System.out.print("Numbers:");

for(int i=1;i<=x;i++)

{

System.out.print(i+" ");

}

System.out.println();

// print all the Evens from 1 to x

System.out.print("Evens:");

for(int i=1;i<=x;i++)

{

if(i%2==0)

System.out.print(i+" ");

}

System.out.println();

// replace divisible of 5 and 7 with "fiver" and "ssss"

System.out.print("Activity:");

for(int i=1;i<=x;i++)

{

if(i%5==0)

System.out.print("fiver ");

else if(i%7==0)

System.out.print("ssss ");

else

System.out.print(i+" ");

}

}

}

Step-by-step explanation:

Read an integer with the help of Scanner class object and assign it to variable "x". Call the function activity() with parameter "x". In the activity function, first print "Numbers" followed by all numbers from 1 to X. Then print header "Evens" followed by all the even number from 1 to x. In last print "Activity" header followed by numbers from 1 to x and replace a number with "fiver" if it is divisible by 5 or replace with "ssss" if it divisible by 7.

Output:

enter a number:17

Numbers:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Evens:2 4 6 8 10 12 14 16

Activity:1 2 3 4 fiver 6 ssss 8 9 fiver 11 12 13 ssss fiver 16 17

User Cornel Creanga
by
5.5k points