SchoolProgram
SchoolProgram
class Main{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Output:
Program 5: Divide 2 numbers
class Main{
static float divide(float x, float y){
return x/y;
}
public static void main(String[] args) {
System.out.println(divide(9,6));
}
}
Output:
Program 8: Factorial
class Main{
public static void main(String[] args) {
int fact=1;
for(int i=1; i<=5; i++){
fact=fact*i;
}
System.out.println(fact);
}
}
Output:
Program 9: Fibonacci Series
class Main{
public static void main(String[] args) {
int n1=0, n2=1, n3,count=10;
}
if(temp==sum){
System.out.println("Palindrome Number");
}else{
System.out.println("Not a Palindrome number");
}
}
}
Output:
class Main {
public static void main(String[] args) {
int i, j, row=3;
for(i=0;i<row;i++){
for(j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output:
class Main {
public static void main(String[] args) {
int i, j, row=3;
for(i=1;i<=row;i++){
for(j=1;j<=i;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
Output
Program 18: Peterson number (A number is said to be Peterson if the sum of factorials of
each digit is equal to the sum of the number itself.)
Output
Program 20: Odd Numbers
public class Main {
public static void main(String[] args) {
int num = 10;
for(int i = 1; i<=num; i++){
if(i%2!=0)
System.out.print(i+ " ");
}
}
}
Output
Program 23: Count the total number of vowels and consonants in a String
public class Main {
public static void main(String[] args) {
int vCount = 0, cCount = 0;
String str = "I am the best programmer";
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' ||
str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
}
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}
Output
Program 25: Replace Uppercase letters with lowercase and vice versa
public class Main {
public static void main(String[] args) {
String str1="Great Power";
StringBuffer newStr=new StringBuffer(str1);
for(int i = 0; i < str1.length(); i++) {
if(Character.isLowerCase(str1.charAt(i))) {
newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));
}
else if(Character.isUpperCase(str1.charAt(i))) {
newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));
}
}
System.out.println(newStr);
}
}
Output
string = string.toLowerCase();
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.out.println("Given string is palindrome");
else
System.out.println("Given string is not a palindrome");
}
}
Output