JAVA
PROGRAMS
1. ODD EVEN
// Program to display even numbers
package navya_raghavxiib;
public class OddEven {
public static void main(String[ ]args)
{ for (int n=1;n<=50;n=n+1) {
if(n%2==0) {
System.out.println(n+ " IS EVEN NUMBER");}
else {
System.out.println(n+ " IS ODD NUMBER");}
}
}
}
}
2. FACTOR FACTORIAL
//Program to display the factors and factorial of a number
package navya_raghavxiib;
import java.util.Scanner;
public class Factor
{
public static void main(String[]args)
{
Scanner Input=new Scanner (System.in);
System.out.println("Enter a no");
int no=Input.nextInt();
for(int i=1;i<=no;i=i+1)
{
if (no%i==0)
{
System.out.println(i);
}
}
int factorial=1
for (int j=1;j<=no,j=j+1)
{
factorial=factorial*j;
}
{system.out.println("Factorial of"+no+"is"+factorial);
}}
3. NUMBERS IN RANGE
// Program to display all the numbers in a range
package navya_raghavxiib;
import java.util.Scanner;
public class PrintInRange {
public static void main(String[] args) {
Scanner Input=new Scanner (System.in);
System.out.println("Enter 1st number");
int m=Input.nextInt();
System.out.println("Enter 2nd number");
int p=Input.nextInt();
for (int i=m;i<=p;i=i+1)
{ System.out.println(i); }
}
}
4. ALL NUMBERS UPTO 1
//Program to display the number upto 1
package navya_raghavxiib;
import java.util.Scanner;
public class NumberUpto1 {
public static void main (String[] args) {
Scanner S1=new Scanner(System.in);
System.out.println("Enter the number");
int no=S1.nextInt();
for (int i=no;i>=1;i=i-1)
{
System.out.println(i);
}
}
}
5. SUM OF NUMBERS IN RANGE
//Program to display the sum of all the numbers
package navya_raghavxiib;
import java.util.Scanner;
public class Sum1 {
public static void main(String[] args)
{
Scanner S1=new Scanner(System.in);
System.out.println("Enter the limit");
int limit=S1.nextInt();
int sum=0;
for(int i=1;i<=limit;i=i+1)
{
no=S1.nextInt();
sum=sum+no;
}
System.out.println("Sum of all numbers is" + sum);
}
}
6. TWO-DIGIT NUMBER
//Program to enter integers and find whether it is two digit or not
package navya_raghavxiib;
import java.util.Scanner;
public class TwoDigits {
public static void main(String[] args) {
Scanner S1=new Scanner(System.in);
System.out.println("ENTER THE LIMIT");
int limit=S1.nextInt();
int no,count=0,i=0;
do {
System.out.println("Enter the Number");
no=S1.nextInt();
if(no>=10 && no<=99)
{
count=count+1;
}
i=i+1;
}
while(i<limit);
System.out.println("TOTAL NUMBER OF 2DIGIT
NUMBER"+count); }
}
7. PALINDROME
//Pogram to enter a number and check if its palindrome
import java.util.Scanner;
public class Palindrome {
public static void main (String[] args) {
Scanner S1=new Scanner (System.in);
System.out.println("Enter a number");
int no=S1.nextInt();
int rev=0,d=0;
int no1=no;
while (no>0) {
d=no%10;
rev=rev+10*d;
no=no/10;
}
if (rev==0)
{ System.out.println("NUMBER IS PALINDROME"); }
else { System.out.println("NUMBER IS NOT PALINDROME"); }
}
}
8. FIBONACCI SERIES
//program to enter a limit and print the fiboanacci series
import java.util.Scanner;
public class fibonacci {
public static void main(String[] args) {
Scanner S1=new Scanner(System.in);
System.out.println("ENTER THE LIMIT");
int limit=S1.nextInt();
int first=0,second=1,nextno;
System.out.println(first+","+second+",");
nextno=first+second;
while (nextno<=limit) {
System.out.println(nextno+",");
first=second;
second=nextno;
nextno=first+second;
}
}
}
9. SUM OF DIGITS IN THE NUMBER
//Program to find sum of all digits of the number
import java.util.Scanner;
public class sum {
public static void main(String[] args){
Scanner S1=new Scanner(System.in);
System.out.println("Enter the number");
int no=S1.nextInt();
int d=0,no1=no,sum=0;
while (no>0)
{
d=no%10;
sum=sum+d;
no=no/10;
}
System.out.println("SUM OF DIGITS IS "+sum);
}
}
10. STRING
//Program for strings
package navya_raghavxiib;
import java.util.Scanner;
public class String1 {
public static void main(String[] args) {
Scanner S1=new Scanner(System.in);
System.out.println("ENTER A STRING");
String Str1=S1.next();
//Find total number of characters in a string or length of string
//Find length of String
System.out.println("LENGTH OF STRING"+ Str.length());
//Convert the string into uppercase or lowercase
//Convert the string into uppercase or lowercase
System.out.println("UPPERCASE
STRING"+Str.toUpperCase());
System.out.println("LOWERCASE
STRING"+Str.toLowerCase());
//Find any substring in string
//Find any substring in string
System.out.println("ENTER A NEW STRING");
String newString=S1.next();
if (Str.contains(newString)==true) {
System.out.println("SUBSTRING EXIST IN THE MAIN
STRING"); }
else System.out.println("SUBSTRING DOESNOT EXIST IN
MAIN STRING");
//Find the index or position of a character in string
//find the index of character
System.out.println("INDEX OF a"+ Str.indexOf('a'));
//Find a character at a given index in string
//Find the character at given index
System.out.println("Character at Index"+Str.charAt(6));
//Enter 2nd string and join the same with string
//Enter 2nd string and join with the first
System.out.println("ENTER 2ND STRING");
String Str2=S1.nexr();
System.out.println("CONCATING STRING" + Str.concat(Str2));
//Replace any character with new value in string
//Replace a substring in the string
System.out.print("Replaced string" + Str.replace('ar','er'));
//Find whether the string is empty or not
//To check wether the string is empty or not
if (Str.isEmpty()==true) {
System.out.println("STRING IS EMPTY");
}
else {
System.out.println("STRING IS NOT EMPTY");
}
// To check whether the string starts or ends with ‘a’ or not
//To check whether the string starts or ends with 'a' or not
if(Str.startsWith('a')==True && Str.endsWith('a''))==True
{
System.out.println("String starts of ends with a");
}
}
}