Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Corejava Important Program (1)

The document contains multiple Java programs that perform various tasks such as calculating the sum of even and odd numbers, counting characters, reversing numbers, checking for palindromes, and generating Fibonacci series. Each program includes its source code and output results, showcasing fundamental programming concepts. Overall, it serves as a collection of basic algorithms and operations in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Corejava Important Program (1)

The document contains multiple Java programs that perform various tasks such as calculating the sum of even and odd numbers, counting characters, reversing numbers, checking for palindromes, and generating Fibonacci series. Each program includes its source code and output results, showcasing fundamental programming concepts. Overall, it serves as a collection of basic algorithms and operations in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PROGRAM

SUM OF EVEN NUMBER


package org.program;
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
a = a + i;
}
}
System.out.println(a);
}
}

OUTPUT:
30
SUM OF ODD NUMBER
package org.program;
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
a = a + i;
}
}
System.out.println(a);
}
}

OUTPUT:
25
COUNT OF EVEN NUMBER
package org.program;
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
a = a + 1;
}
}
System.out.println(a);
}
}

OUTPUT:
5

COUNT OF ODD NUMBER


package org.program;
public class Corejava {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
a = a + 1;
}
}
System.out.println(a);
}
}

OUTPUT:
5

REVERSE NUMBER

package org.program;
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=(j*10)+i;
num=num/10;
}
System.out.println(j);
}
}

OUTPUT:
4321
PALINDRONE

package org.program;
public class Corejava {
public static void main(String[] args) {
int num = 121,i=0,j=0;
int reverse=num;
while(num>0) {
i=num%10;
j=(j*10)+i;
num=num/10;
}
if(reverse==j) {
System.out.println("Palindrone");
}
else
{
System.out.println("Not palindrone");
}
}
}

OUTPUT:
Palindrone

SUM OF DIGITS OF A NUMBER

package org.program;
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=j+i;
num=num/10;
}
System.out.println(j);
}
}

OUTPUT:
10
COUNT OF DIGITS
package org.program;
public class Corejava {
public static void main(String[] args) {
int num = 1234,i=0,j=0;
while(num>0) {
i=num%10;
j=j+1;
num=num/10;
}
System.out.println(j);
}
}
OUTPUT:
4

AMSTRONG NUMBER
package org.program;
public class Corejava {
public static void main(String[] args) {
int num = 153,i=0,j=0;
int reverse=num;
while(num>0) {
i=num%10;
j=j+(i*i*i);
num=num/10;
}
System.out.println(j);
if (reverse==j) {
System.out.println("Amstrong");
}
else {
System.out.println("Not Amstrong");
}
}
}

OUTPUT:
153
Amstrong
SWAPPING OF 2 NUMBERS USING 3 NUMBER

package org.program;
public class Corejava {
public static void main(String[] args) {
int a =10,b=23,c;
c=a;
a=b;
b=c;
System.out.println("a value is "+a+"\n"+"b
value is "+b);
}
}

OUTPUT:
a value is 23
b value is 10
SWAPPING OF 2 NUMBERS WITHOUT 3
NUMBER

package org.program;
public class Corejava {
public static void main(String[] args) {
int a=10, b=20;
System.out.println("before swapping: "+a +"\n"+
"before swapping: "+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("after swapping: "+a +"\n"+
"after swapping: "+b);
}
}

OUTPUT:
before swapping: 10
before swapping: 20
after swapping: 20
after swapping: 10

FIBONACCI SERIES

package org.program;
public class Corejava {
public static void main(String[] args) {
int a=0,b=1,c;
for (int i = 0; i <=10; i++) {
c=a+b;
a=b;
b=c;
System.out.println(c);
}
}
}
OUTPUT:
1
2
3
5
8
13
21
34
55
89
144

FACTORIAL

package org.program;
public class Corejava {
public static void main(String[] args) {
int a=1;
for (int i = 1; i <=5; i++) {
a=a*i;
}
System.out.println(a);
}
}

OUTPUT:
120

COUNT EACH CHARACTER OF STRING OR


OCCURANCE COUNT

package org.program;
import java.util.HashMap;
import java.util.Map;
public class Corejava {
public static void main(String[] args) {
String s ="yaashith";
Map<Character,Integer> m = new HashMap<>();
char[] ch = s.toCharArray();
for (char c : ch) {
if(m.containsKey(c))
{
int x = m.get(c);
m.put(c, x+1);
}
else {
m.put(c, 1);
}
}
System.out.println(m);
}
}

OUTPUT:
{a=2, s=1, t=1, h=2, y=1, i=1}

REMOVE SPACE AND PRINT STRING

package org.program;
public class Corejava {
public static void main(String[] args) {
String s ="yaashith kumar";
String x = s.replace(" ", "");
System.out.println(x);
}
}

OUTPUT:
Yaashithkumar

COUNT OF EACH WORD

package org.program;
import java.util.HashMap;
import java.util.Map;
public class Corejava {
public static void main(String[] args) {
String s ="yaashith kumar yaashith";
String [] x = s.split(" ");
HashMap<String, Integer> m = new HashMap<>();
for (String c : x) {
if (m.containsKey(c)) {
int i =m.get(c);
m.put(c, i+1);
}
else
{
m.put(c,1);
}
}
System.out.println(m);
}
}

OUTPUT:
{yaashith=2, kumar=1}

TRIANGLE PROGRAM

package org.program;;
public class Corejava {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

OUTPUT:
*
**
***
****
*****
REVERSE TRIANGLE

package org.program;;
public class Corejava {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >=i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}

OUTPUT:
****
***
**
*

VOWEL AND CONSTANT

package org.program;;
public class Corejava {
public static void main(String[] args) {
String a ="Yaashith";
int v=0, n=0;
for (int i = 0; i < a.length(); i++) {
char ch =a.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
v++;
}
else {
n++;
}
}
System.out.println("Vowel is "+v);
System.out.println("Constant is "+n);
}
}

OUTPUT:
Vowel is 3
Constant is 5
SORTING AN ARRAY
package org.program;
import java.util.Arrays;
public class Corejava {
public static void main(String[] args) {
int array[] = {10,6,3,2};
Arrays.sort(array);
System.out.println("sorting elements:");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
OUTPUT:
sorting elements:
2
3
6
10

MAXIMUM NUMBER IN AN ARRAY

package org.program;
import java.util.Arrays;
public class Corejava {
public static void main(String[] args) {
int array[] = {10,324,45,9808};
int max=Arrays.stream(array).max().getAsInt();
System.out.println(max);
}
}
OUTPUT:
9808
PRIME NUMBER

package org.program;
public class Corejava {
public static void main(String[] args) {
int num=100;
boolean flag =false;
for (int i = 2; i <=num/2; i++) {
if (num%i==0) {
System.out.println("its not a prime number");
flag=true;
break;
}
}
if(!flag) {
System.out.println("its a prime number");
}
}
}

OUTPUT:
its not a prime number

SECOND LARGEST NUMBER IN AN ARRAY

package org.program;
import java.util.Arrays;
public class Corejava {
public static void main(String[] args) {
int temp,size;
int array[]= {10,20,30,40,50};
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if(array[i]>array[j]) {
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
System.out.println(array[array.length-2]);
}
}

OUTPUT:
40

COUNT THE STRING OF VOWEL AND


CONSTANT, SYMBOL AND SPECIAL
CHARECTER
package org.program;
public class Hierarchial {
public static void main(String[] args) {
String s = "preethi120598@gmail.com";
int count=0,constant=0,num=0,special=0;
for (int j = 0; j < s.length(); j++) {
char ch = s.charAt(j);
if (ch>='a' && ch<='z') {
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' ||
ch=='u') {
count++;
}
else {
constant++;
}
}
else if (ch>='0' && ch<='9') {
num++;
}
else
{
special++;
}
}
System.out.println(count);
System.out.println(constant);
System.out.println(num);
System.out.println(special);
}
}

FLAMES

package org.program;
import java.util.Scanner;
public class Corejava {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the girl name:");
String girl = s.next();
System.out.println("Enter the boy name");
String boy = s.next();
String girl1 = new String(girl);
String boy1 = new String(boy);
int length = 0;
for (int i = 0; i < girl.length(); i++) {
for (int j = 0; j < boy.length(); j++) {
if (girl.charAt(i) == boy.charAt(j)) {
String strgirl = removeCharAt1(girl, i);
String strboy = removeCharAt1(boy, j);
girl = strgirl;
boy = strboy;
i--;
j--;
break;
}
}
}
int lengthgirl = girl.length();
int lengthboy = boy.length();
length = lengthgirl + lengthboy;
System.out.println(length);
String str = "FLAMES";
int lengthone = str.length();
int lengthfinal = lengthone - 2;
for (int i = 0; i <= lengthfinal; i++) {
int firstlength = length % lengthone;
if (firstlength == 0) {
String str1 = removeCharAt(str, lengthone - 1);
str = str1;
int lengthsecond = str1.length();
lengthone = lengthsecond;
} else {
String str1 = removeCharAt(str, firstlength -
1);
str = str1;
int lengthsecond = str1.length();
lengthone = lengthsecond;
}
}
System.out.println(str);
switch (str) {
case "F":
System.out.println(girl1 + "is Friend to " +
boy1);
break;
case "L":
System.out.println(girl1 + " is Love to " +
boy1);
break;
case "A":
System.out.println(girl1 + " is Affection to "
+ boy1);
break;
case "M":
System.out.println(girl1 + " is Marriage to " +
boy1);
break;
case "E":
System.out.println(girl1 + " is Enemies to " +
boy1);
break;
case "S":
System.out.println(girl1 + " is Sister to " +
boy1);
break;
}
}

public static String removeCharAt(String str,


int i) {
return str.substring(i + 1) + str.substring(0,
i);
}

public static String removeCharAt1(String str,


int i) {
return str.substring(0, i) + str.substring(i +
1);
}
}

ASSIGN CHARACTER VALUE TO INTEGER

package org.program;
public class Dog {
static int i;
static char ch;
public static void main(String[] args) {
for (i = 1,ch='a'; i <=26 && ch<='z'; i++,ch++)
{
System.out.println(i +"="+ch);
}

}
}

You might also like