Write a method power0f Two that takes an integer argument an

Write a method power0f Two that takes an integer argument and prints to the console what power of 2 the given number is. If the number is not a power of 2. then simply print a message that says so. For example. power0fTwo(64);//should print: \"64 is 2 to the 6\" power0fTwo(73);//should print: \"73 is not a power of 2\" Write a method numBackward that reverses the digits of a given integer (that is. an integer passed in as an argument). Use a single loop and modular arithmetic to accomplish this goal. Play around with ideas on paper first. After you reverse the digits, compare the number you got with the original argument and determine if the original argument is a palindrome. Your method should print a message to the screen that states the reversed number and whether or not it is a palindrome. For example, numBackward(123456789)//should print: \"backward: 987654321, not palindrome!\" numBackward(123454321)//should print: \"backward: 123454321, palindrome!\"

Solution

1)

import java.io.*;
import java.util.Scanner;
public class power_of_two
{
public static void main(String args[])
{

Scanner in=new Scanner(System.in);
System.out.println(\"Enter the number : \");
int num = in.nextInt();

if((num & (num-1)) == 0)
{
int result=isPowerOfTwo(num);
System.out.println(num+\" is 2 to the power \"+result);
}
else
{
System.out.println(num+\" is not a power of two\");
}
}

static int isPowerOfTwo (int x)
{
int count=0;
while (((x % 2) == 0) && x > 1) /* While x is even and > 1 */
{
x /= 2;
count++;
}
return count;
}
}

b)

import java.util.Scanner;
public class pallindrome
{
public static void main(String args[])
{

Scanner in=new Scanner(System.in);
System.out.println(\"Enter the number : \");
int num = in.nextInt();

       int reverse=numBackward(num);
       if (num== reverse) {
System.out.println(\"backward: \"+reverse+\" ,palindrome!\");
}
else
       {
           System.out.println(\"backward: \"+reverse+\" ,not palindrome!\");
       }
}
public static int numBackward(int number) {
int palindrome = number;
int reverse = 0;

while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
return reverse;
}
}

 Write a method power0f Two that takes an integer argument and prints to the console what power of 2 the given number is. If the number is not a power of 2. the
 Write a method power0f Two that takes an integer argument and prints to the console what power of 2 the given number is. If the number is not a power of 2. the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site