Click here to Skip to main content
14,665,013 members
Rate this:
Please Sign up or sign in to vote.
See more:
Find the sum of n digit pallindromic numbers which are divisible by 8 and contains no zero in their decimal representation.

What I have tried:

import java.util.*; 

class GFG 
{ 

static boolean isPalindrome(String s) 
{ 
	int left = 0, right = s.length() - 1; 
	while (left <= right) 
	{ 
		if (s.charAt(left) != s.charAt(right)) 
		{ 
			return false; 
		} 
		left++; 
		right--; 
	} 
	return true; 
} 

static long getSum(int n) 
{ 

	int start = (int) Math.pow(10, n - 1); 
	int end = (int) (Math.pow(10, n) - 1); 

	long sum = 0; 

	for (int i = start; i <= end; i++) 
	{ 
		String s = String.valueOf(i); 
		
		// Append
		if (isPalindrome(s)) 
		{ 
			sum += i; 
		} 
	} 

	return sum; 
} 

public static void main(String[] args) 
{ 
	int n = 1; 
	long ans = getSum(n); 
	System.out.print(ans); 
} 
}
Posted
Updated 30-Mar-20 2:32am
v2
Comments
OriginalGriff 30-Mar-20 7:23am
   
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hints:
  • an integer i is divisible by 8 if (i%8)!=0
  • you have the string representation of the number, it is not a daunting task to find there is a '0' character in such a string
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100