Important Concept about string Flashcards
Even number Analysis:
If(8%2 = 0 ) // check if I is even
If (I % 2 == 1) // check if I is odd number
1. even numbers are numbers that are divisible by 1 and the number itself.
- odd number are numbers that are not divisible by two(2) evenly. they have a remainder
Display the sum of an integer
enter by the user
while(true){
// condition is always true.
// so we need a break statement
// to comes out of the loop.
}
1.sum of the Strict divisor of an Integer: Example: 6 = 1+2+3=6
2. what do we means by strict divisor of a number?
The strict divisor of a number is n / 2.
the strict divisor of a number is an integer that evenly divides another number without leaving a remainder, except for the number itself. in order words, it is a divisor that is smaller than the number itself.
the strict divisor of 12 are : 2,3,4, and 6. 1 is a divisor of 12, but it is not a strict divisor because 1 is equal to the number itself(12).
1. i <= n/2 means
2.sum += i means that we are adding the value of i to sum.
3. if( number%i == 0) means we check if the number enter is divisible by the current number i . if the remainder is zero, it means i is a divisor of number.
- what is a prime number?
- how to check if a number is prime or not
- prime number is a number that cannot be evenly divided by any other number except 1 and itself. example( 2,3,5,7.9).
2.using boolean logic.
boolean isprime = true;
isprime = false;
3. i<=n/2
- Sequence of integer.
- if( n< o)
break;
chenk if a number is a negative - this code update our max and min value using a tenaray operator:
max = n> max? n : max;
min = n < min? n: min; - this also update a max value and a min value. as the above code.
if ( n > max)
max = n;
if( n < min)
min = n; - this make sure that n is not a negative number:
if 9 n >= 0){
// statements
if this statement is not executed the else will be executed. if the number from the use is a negative the if statement will not be executed the else will be executed.
}
else
statement
- Sum of digit of an integer.
example: 108= 1 + 0+8 = 9
- sum += number %10 this get the last digit of the number and add it to sum.
- number /=10; remove the last digit. this also divide the number by number and add the result back to number
example: number = number/10;
- The Fibonacci sequence
1 i < n -2
- String with space after each character
1.name.CharAt(i) this print the character at the current index i.
of any element or data store in the name variable. example: Darius: the first index is 0, so the first character is D this will print the character the character D in the string.
charAt(); this is a method in java that is used to get the character at a specific index of a string data.
- name.length()-1 means we are subtracting 1 from the length of the string to get the last character in the string
- name.length-1 returns the number of characters in the string.
1.Reverse a string
2. how do you reverse a string
- string reverse= “ “; this variable is initialized to store a value. this is similar to int sum = 0; which we can add the sum of number to the variable sum.
- to reverse a string you have to start from the last index of the string to the first index. example: int i = number.length-1; i >=0; i–;
a. number.length-1 get the last index of the character.
b. i <=0 get the first element of the string.
c. i– we decrease the value of i by one.
- Palindrome String.
- what is a palindrome string.
- palindrome string is a sequence of characters that reads the same forwards and backwards. Example: NOON, Level, radar
- str.CharAt(i) == str.charAt(j) this compare the character a index i and the character at index j.
- String str = “noon”;
boolean ispolindrome = true; this means that we set the string “noon” to be true. we assume that “noon” is a polylindrome string. - using the tenary operator:
ispalindrome? “palindrome : “notpalindrome”
if ispalindrome is true we will return palindrome if false we return notpalindrome.