हिंदी

Write a program to print the digits of a number in new line each (reverse order).

Advertisements
Advertisements

प्रश्न

Write a program to print the digits of a number in new line each (reverse order).

कोड लेखन
Advertisements

उत्तर

//Using while loop
import java.util.*;
class clDigits
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int N, d, cn;       
        System.out.print("Enter a number : "); N = sc.nextInt();
        cn = N;        
        System.out.println("Digits in reverse order : ");
        while(cn > 0)
        {
            d = cn % 10;
            System.out.println(d);
            cn = cn / 10;
        }
    }
}
//Using for loop
import java.util.*;
class clDigits
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int N, d;
        System.out.print("Enter a number : ");
        N = sc.nextInt();
        System.out.println("Digits in reverse order : ");
        for(int cn = N; cn > 0; cn = cn / 10)
        {
            d = cn % 10;
            System.out.println(d);
        }
    }
}

Output:

Enter a number : 963 

Digits in reverse order: 

3

6

9

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - Digit Handling [पृष्ठ २५१]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
Digit Handling | Q 1. | पृष्ठ २५१
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×