
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Large Number is Divisible by 11 in Java
A number is divisible by 11 if the difference between the sum of its alternative digits is divisible by 11.
i.e. if (sum of odd digits) – ( sum of even digits) is 0 or divisible by 11 then the given number is divisible by 11.
Program
import java.util.Scanner; public class DivisibleBy11 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number :"); String num = sc.nextLine(); int digitSumEve = 0; int digitSumOdd = 0; for(int i = 0; i<num.length(); i++) { if(i%2 == 0) { digitSumEve = digitSumEve + num.charAt(i)-'0'; } else { digitSumOdd = digitSumOdd + num.charAt(i)-'0'; } } int res = digitSumOdd-digitSumEve; if(res % 11 == 0) { System.out.println("Given number is divisible by 11"); } else { System.out.println("Given number is not divisible by 11"); } } }
Output
Enter a number : 121 Given number is divisible by 11
Advertisements