Sunday, June 21, 2020

Write a program to read a number having more than 5 digits and print whether the given number is Divisible by 11 or not.( A number is divisible by 11 if and only if the difference of the sums of digits at odd positions and even positions is either zero or divisible by 11)

26)  Write a program to read a number having more than 5 digits and print whether the given number is Divisible by 11 or  not.( A number is divisible by 11 if and only if the difference of the sums of digits at odd positions and even positions is either zero or divisible by 11)

import java.util.*;
public class number_divisible_by_11
{
    public static void main(String args[ ])
    {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a number");
        int a = sc.nextInt();
        int m = a;
        int k = a;
        int e = 0,o = 0;
        int c =0;
        while(k>0)
        {
            k=k/10;
            c++;
        }
        if(c>5)
        {
            while(m>0)
            {
                int d = m%10;
                if(c%2==0)
                {
                    e=e+d;
                }
                else
                {
                    o=o+d;
                }
                c--;
                m=m/10;
            }
            if
            ((o%11==0&&e%11==0)||o-e==0)
            {
              if(a%11==0)
              {
                  System.out.println(a+" is divisible by 11");
              }
              else
              {
                  System.out.println(a+" is not divisible by 11");
              }
            }
            else
            {
                  System.out.println(a+" is not divisible by 11");
            }
        }
        else
        {
                  System.out.println(a+" is not divisible by 11");
        }
    }
}

No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj