0% found this document useful (0 votes)
26 views2 pages

Coding Round - TCS Ninja Day 7

Uploaded by

Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Coding Round - TCS Ninja Day 7

Uploaded by

Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programs for reversing a number

Case 1:
import java.util.*;

class Demo

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number");

int num = sc.nextInt();

int rev = 0;

while(num!=0)

rev = rev*10+num%10;

num=num/10;

System.out.println("reverse of number is "+rev);

Case 2:
import java.util.*;

class Demo

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number");

int num = sc.nextInt();


StringBuffer sb = new StringBuffer(String.valueOf(num));

System.out.println("reversed number is "+sb.reverse());

Case 3:

import java.util.*;

class Demo

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number");

int num = sc.nextInt();

StringBuilder sb = new StringBuilder();

sb.append(num);

System.out.println("reversed number is "+sb.reverse());

You might also like