Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:

Java
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

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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900