Posts

Showing posts from January, 2025

Java String Manipulation 28th Jan Practice 2 - input a string and check whether it is a palindrome or not

Image
Palindrome is a number or a string which when reversed is the same as the original.  import java.util.*; class Main {     public static void main(String[] args) { Scanner in = new Scanner(System.in); String s,s1=""; char ch; System.out.println("Enter a String"); s = in.next(); int l = s.length(); s = s.toUpperCase(); for(int i=l-1; i>=0; i--){     ch = s.charAt(i);     s1 = s1+ch; } System.out.println(s1); if(s1.equals(s)){     System.out.println("Palindrome");      } else System.out.println("Not a palindrome");     } }

Java String Manipulation 28th Jan Practice 1 - to accept a character and check whether it is a letter or not. If letter, then convert it to uppercase or lowercase. Also check whether it is letter or a special character

Image
Java Program to accept a character and check whether it is a letter or not. If letter, then convert it to uppercase or lowercase. Also check whether it is letter or a special character import java.util.*; class Main {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         char a;         System.out.println("Enter a character");         a = in.next().charAt(0);         if(Character.isLetter(a)){             System.out.println("Character "+ a+ " is letter");             if(Character.isUpperCase(a)){                 System.out.println(Character.toLowerCase(a));             }             else             System.out.println(Character.toUpperCase(a)); ...

Java Recursion Program Practise 5 - to input a number and print the sum of digits of the number

Image
Java Program to input a number and print the sum of digits of the number using Recursive Function import java.util.*; class Main {    int digit(int n){        if(n==0)        return 0;        else        return (n%10+digit(n/10));    }     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int a;         System.out.println("Enter the number whose sum of digit is desired using Recursive function");         a = in.nextInt();         Main ob = new Main();         System.out.println(ob.digit(a));                       } }

Java Recursion Program Practise 4 - to input a number and display sum from 1 to that number

Image
Java Program to input a number and display sum from 1 to that number using Recursion import java.util.*; class Main {     int add(int n){         if(n>0){             return (n + add(n-1));         }         else         return 0;     }     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int a;         System.out.println("Enter the number of which the sum is desired from 1 to the number n");         a = in.nextInt();         Main ob = new Main();       System.out.println( ob.add(a));              } }

Java Recursion Program Practise 3 - Raising base to power of index

Image
Java Program to input two number and raising the input number to the power of the another input using Recursive Function.  import java.util.*; class Main {     int power(int n, int p){         if(p==0)         return 1;         else {             return (n* power(n,p-1));         }     }     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int a, b;         System.out.println("Enter the number and the power to be raised using Recursion Function");          a = in.nextInt();         b = in.nextInt();         Main ob = new Main();         System.out.println(ob.power(a,b));              } }

Java Recursion Program Practise 2 - Fibonacci Series

Image
Java program to find the given term of Fibonacci Series using Recursion Function import java.util.*; class Main {          int fib(int n){         if(n==1){         return 0;         }         if(n==2){         return 1;         }         if(n>2){             return(fib(n-2)+fib(n-1));         }         else         return -1;     }     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int a;         System.out.println("Enter the number of terms of Fibonacci series to be displayed using Recursion function");         a = in.nextInt();         Main ob = new Main();   ...

Java Recursion Program Practise 1 - Factorial

Image
Java Program to find Factorial of given number using Recursion import java.util.*; class Main {          int fact(int n){         if(n==0)         return 1;         else{             return(n*fact(n-1));         }     }     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int a;         System.out.println("Enter a number to get the factorial with the help of recursion");         a = in.nextInt();         Main ob = new Main();        System.out.println(ob.fact(a));                                } }

Translate