Some Programs on Digits of a Number

Count No of Digits.

 

  Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int count=0;
        while(n!=0)
        {
        count=count+1;
        n=n/10;
        
        }
        System.out.println("Count = " + count);

Sum of digits.

   Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int sum=0;
        while(n!=0)
        {
        sum=sum + n %10;
        n=n/10;
        
        }
        System.out.println("Sum = " + sum);

Reverse a Number

   Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int rev=0;
        while(n!=0)
        {
        rev=rev*10 + n %10;
        n=n/10;
        
        }
        System.out.println("Reverse = " + rev);

Check for a Palindrome Number

 Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int rev=0;
        int copy=n;
        while(n!=0)
        {
        rev=rev*10 + n %10;
        n=n/10;
        
        }
        if(n==rev)
        System.out.println("Palindrome");
        else
            System.out.println("Not a Palindrome");

dsfsdfsd

Leave a Reply