0% found this document useful (0 votes)
289 views

Apex Basic Program For Practice

The document contains code snippets for several Java programs that demonstrate different programming concepts like Fibonacci series, prime number checking, reversing numbers, printing patterns, finding the largest/second smallest element in an array, counting vowels/consonants in a string, reversing a string, finding duplicate words, and determining if a string is a palindrome.

Uploaded by

KRUNAL RAJURKAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
289 views

Apex Basic Program For Practice

The document contains code snippets for several Java programs that demonstrate different programming concepts like Fibonacci series, prime number checking, reversing numbers, printing patterns, finding the largest/second smallest element in an array, counting vowels/consonants in a string, reversing a string, finding duplicate words, and determining if a string is a palindrome.

Uploaded by

KRUNAL RAJURKAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

***Fibonacci Series***

class FibonacciExample1{
public static void Febo()
{
int n1=0,n2=1,n3,i,count=10;
System.debug(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}}

***Prime Number Program using Method in Java***


public class PrimeExample2{
static void checkPrime(int n){
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1){
System.debug(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.debug(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.debug(n+" is prime number"); }
}//end of else
}

***ReverseNumber***

public class ReverseNumberExample1


{
public static void main()
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.debug("The reverse of the given number is: " + reverse);
}
}

***Hello World***
class Simple{
public static void main(){
System.debug("Hello World");
}
}

Pattern program

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

public class pattern  

    public static void main()
  {   int i ,j;  
        int n = 5;  
            for(i = n; i>0 ; i-- )  
            {  
                 for(j = 1; j<=i ; j++)  
                    {  
                        System.debug(j);  
                    }  
                         System.debug("");  
            }  
    }  

*****A
B B
C C C
D D D D
E E E E E

class pattern
{
public static void main5()
{
int n = 4;
for(int i = 0 ; i <= n ; i++)
{
for(int j = 0 ; j <= i ; j++)
{
System.debug(" "+(char)(65 + i));
}
System.debug("");
}
}
}

********
*******
******
*****
****
***
**
*

public class pattern

public static void main()


{
int size ;
size =6;
for (int i = size; i != 0; i--)
{
for (int j = 0; j<size-i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < (2 * i - 1); k++)
{
System.debug("*");
}
System.debug();
}
}

  
*******
1
2 3
4 5 6
7 8 9 10

class pattern

public static void main()


{
int i ,j;
int n = 6;
for(i = n; i>0 ; i-- )
{
for(j = 0; j<i ; j++)
{
System.debug("*");
}
System.debug("");
}
}

$$$$$$$$

Program to print the largest element in an array

public class LargestElement_array {


public static void main() {

//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
System.debug("Largest element present in given array: " + max);
}

$$$$$$Java Program to find Second Smallest Number in an Array

public class SecondSmallestInArrayExample{


public static int getSecondSmallest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[1];//2nd element because index starts from 0
}
public static void main(){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.debug("Second smallest: "+getSecondSmallest(a,6));
System.debug("Second smallest: "+getSecondSmallest(b,7));
}}
$$$$$$Java Program to count the total number of vowels and consonants in a string

public class CountVowelConsonant {


public static void main() {

//Counter variable to store the count of vowels and consonant


int vCount = 0, cCount = 0;

//Declare a string
String str = "This is a really simple sentence";

//Converting entire string to lower case to reduce the comparisons


str = str.toLowerCase();

for(int i = 0; i < str.length(); i++) {


//Checks whether a character is a vowel
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
|| str.charAt(i) == 'o' || str.charAt(i) == 'u') {
//Increments the vowel counter
vCount++;
}
//Checks whether a character is a consonant
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
//Increments the consonant counter
cCount++;
}
}
System.debug("Number of vowels: " + vCount);
System.debug("Number of consonants: " + cCount);
}
}

#######Java Program to find Reverse of the string.

public class Reverse


{
public static void main() {
String string = "Dream big";
//Stores the reverse of given string
String reversedStr = "";

//Iterate through the string from last and add each character to variable
reversedStr
for(int i = string.length()-1; i >= 0; i--){
reversedStr = reversedStr + string.charAt(i);
}

System.debug("Original string: " + string);


//Displays the reverse of given string
System.debug("Reverse of given string: " + reversedStr);
}
}

@@@@@@Java program to find the duplicate words in a string

public class DuplicateWord {


public static void main() {
String string = "Big black bug bit a big black dog on his big black nose";
int count;

//Converts the string into lowercase


string = string.toLowerCase();

//Split the string into words using built-in function


String words[] = string.split(" ");

System.out.println("Duplicate words in a given string : ");


for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
//Set words[j] to 0 to avoid printing visited word
words[j] = "0";
}
}

//Displays the duplicate word if count is greater than 1


if(count > 1 && words[i] != "0")
System.debug(words[i]);
}
}
}

&&&&&Java Program to determine whether a given string is palindrome

public class PalindromeString


{
public static void main() {
String string = "Kayak";
boolean flag = true;

//Converts the given string into lowercase


string = string.toLowerCase();

//Iterate the string forward and backward, compare one character at a time
//till middle of the string is reached
for(int i = 0; i < string.length()/2; i++){
if(string.charAt(i) != string.charAt(string.length()-i-1)){
flag = false;
break;
}
}
if(flag)
System.debug("Given string is palindrome");
else
System.debug("Given string is not a palindrome");
}
}

You might also like