Halloween Costume ideas 2015

Categories

Search This Blog

Write a Java (BlueJ) Program to Find Reverse of a Given Number and String with Output


Program Input:

import java.util.*;

public class RevNumString
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Please enter a number: ");
int num = scanner.nextInt();

System.out.println("Please enter a string: ");
String str = scanner.next();

RevNumString rns = new RevNumString();
int revNum = rns.reverse(num);
String revStr = rns.reverse(str);

System.out.printf("\n The reverse of number %d is %d ", num, revNum);
System.out.printf("\n The reverse of string '%s' is '%s' ", str, revStr);
}

// Method to return the reverse of a number
public int reverse(int num) {
int revNum = 0;
while (num > 0) {
int rem = num % 10;
revNum = (revNum * 10) + rem;
num = num / 10;
}
return revNum;
}

// Method to return the reverse of a string
public String reverse(String str) {
StringBuilder revStr = new StringBuilder();
for (int i = str.length()-1; i >= 0; i--) {
revStr.append(str.charAt(i));
}
return revStr.toString();
}
}

Program Output:


Please enter a number:
1234
Please enter a string:
Java

The reverse of number 1234 is 4321
The reverse of string 'Java' is 'avaJ'

Post a Comment

Labels

2017 3 Stoogies 4kids Action Adventure Alison Android Android Apps Android Games Animation Apple Apps Art astronomy Baahubali Basic Electronics Batman BBC Beginners Bible Bilderberg Group biology Block Diagram Blogging BlueJ Books BoomBeach Brand Business CAD Call Of Duty Cars Cement chemistry Christian Bale City Clan Wars Clash of Clans Clash Royale CoC Coinsrewarder collaborations Collections Comedy Comedy Heist Componets Computer Conspiracy Conspiracy Theories Conspiracy Theory Cooking Coursera Cowboy Craft Crime DAP Digg Discovery Channel Documentary Documentary Collections Download Drama earn Earn from home Earning Online Education edX Electronics EngagemeTV Entertainment Example Facts Family Fantasy Fashion Food Foods Free Free Course Freebies Freemasons Functions Gadgets Gallery Gaming geography George Clooney Ghost Giants Graphic Design Guide haunting Historical drama History Horror Comedy Hugh Jackman i ICC Illusion India Instagram International Trade Intrigue iPad James Bond Khan Academy kids La La Land Lastfm learning Letters Of Credit Lifestyle LinkedIn Logan martial arts mashups mathematics maths Media Mobile Mobile Downloads Motion Design Motivational Movies Mr. Bean murder Music Musical Drama Must Watch Myspace Mystery MYSTERY & INTRIGUE Nature Nephilim Niche NPK Offers Ohm's Law OneAd opportunity Overview Parallel Circuit paranormal Parenting PC PC Downloads Pc Games People Performances Phone Photography physical training physics PLR Print Design Programming Rankings Raw Cashew Nuts Recipe Requirements Review Reviews Risks Romance Rules SBLC Sci-Fi Secret Societies serial killer Series Series Circuit Server Share Market Share Trading Short sites Social Soundcloud Spaghetti Westerns special report Specifications Spirititual SPOOFS Sports sprituality Squidoo Stock Market Stock Trading story StumbleUpon Super Hero Super Heroes SWIFT Symbols Technology Test Thirukkural Thriller Tips & Tricks Title Tom Hanks Top 10 top 9 Tourism Town Hall Trailer Travel Trilogies tutorial TV Twitter UCP UCR Udemy Unboxing Under 30 Update Urea Video Vidya vox Virus Visual illusion war Web Design Yahoo Answers Youtube

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget