Skip to main content

Simple Java Programs (Part 1)

 Q. Define a class to declare a character array of size ten, accept the character into the array and perform the following: 

• Count the number of uppercase letters in the array and print.

 • Count the number of vowels in the array and print.

import java.util.*;
class Q32S{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
char ch[]=new char[10];
int upper=0,vowel=0;
char c=' ';
System.out.println("Enter characters: ");
for(int i=0;i<ch.length-1;i++){
ch[i]=s.next().charAt(0);
if(Character.isUpperCase(ch[i]))
upper=upper+1;
c=ch[i];
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
vowel=vowel+1;
}
System.out.println("No. of UPPERCASE : "+upper);
System.out.println("No. of vowels : "+vowel);
}
}
view raw Q3S2 hosted with ❤ by GitHub

Q. Define a class to declare an array of size 20 of double datatype, accept the elements into the array and perform the following:

 • Calculate and print the sum of all the elements.

 • Calculate and print the highest value of the array.

import java.util.Scanner;
class Q42S{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
double num[]=new double[20];
double sum=0.0, max=num[0];
System.out.println("Enter elements: ");
for(int i=0;i<20;i++){
num[i]=s.nextDouble();
sum+=num[i];
if(num[i]>max)
max=num[i];
}
System.out.println("Sum = "+sum);
System.out.println("Largest numbe = "+max);
}
}
view raw Q4S2 hosted with ❤ by GitHub

Q. Define a class to accept two strings, convert them into uppercase, check and display whether two strings are equal or not, if the two strings are not equal, print the string with the highest length or print the message both the strings are of equal length.

import java.util.Scanner;
class Q52S{
public static void main(String []args){
Scanner s=new Scanner(System.in);
String str1, str2;
int len1,len2;
System.out.println("Enter first string: ");
str1=s.nextLine();
System.out.println("Enter second string: ");
str2=s.nextLine();
str1=str1.toUpperCase();
str2=str2.toUpperCase();
len1=str1.length();
len2=str2.length();
if(len1<len2)
System.out.println(str2);
else if(len1>len2)
System.out.println(str1);
else
System.out.println("Both strings are of equal length");
}
}
view raw Q5S2 hosted with ❤ by GitHub

Q. Define a class to accept a string, convert it into lowercase and check whether the string is a palindrome or not.

 A palindrome is a word which reads the same backward as forward.

 Example:

 madam, racecar etc.

import java.util.Scanner;
class Q52S{
public static void main(String []args){
Scanner s=new Scanner(System.in);
String str1, str2;
int len1,len2;
System.out.println("Enter first string: ");
str1=s.nextLine();
System.out.println("Enter second string: ");
str2=s.nextLine();
str1=str1.toUpperCase();
str2=str2.toUpperCase();
len1=str1.length();
len2=str2.length();
if(len1<len2)
System.out.println(str2);
else if(len1>len2)
System.out.println(str1);
else
System.out.println("Both strings are of equal length");
}
}
view raw Q5S2 hosted with ❤ by GitHub

Comments

Popular posts from this blog

Shifting Agriculture

  What is Shifting agriculture? Shifting Agriculture also known as 'Slash-and-Burn' method is a very common practice of cultivation prevalence in countries such as India. In this method, farmers make a small clearing in the forest where the cultivation of crops is done solely dependent on monsoon rain for irrigation and natural fertility of soil. No artificial input of either irrigation or fertilizer is given by the farmer. This method is very harmful to the environment. Shifting agriculture  Key features of Shifting agriculture:  Following are the key features of shifting agriculture: It is practiced on small patches of land with the help of primitive tools. It is also called jhumming , agriculture where the farmers make a small clearing in the forest, cultivate it for some years and then abandon it when the fertility of the soil diminishes. They then shift to another fresh clearing in the forest. This is also called slash-and-burn agriculture. The patch of land is usu...

Binary Search in Java - Explained

Binary Search in Java - Explained You are looking for an explanation of how binary search works in Java ? If so, then search no more! What is Binary search? In Java programming language, binary search is an advanced searching algorithm used to search through a single or multiple arrays for a search element. How does the binary search algorithm works? In binary search, the array has to be sorted first. In this method, when the user searches for a value, the number of elements to be searched is reduced, because it locates the middle number and then either moves to the upper half or the lower half to continue the search. Program for Binary Search Suppose I have to search for a number and return its position (index) in the array. int arr[] = {5,10,15,20,25,30,35,40,45,50} For example if I search for say, 35 The program has been given below for binary search: Points to Note: Binary search only works with an array already sorted This is effective for very large arrays, howeve...

Abou Ben Adhem

  Abou Ben Adhem                                           by-   Leigh Hunt Abou Ben Adhem (may his tribe increase) Awoke one night from a deep dream of peace, And saw, within the moonlight in his room, Making it rich, and like a lily in bloom, An Angel writing in a book of gold:- Exceeding peace had made Ben Adhem bold, And to the presence in the room he said, "What writest thou?" - The vision raised it's head, And with a look made of of all sweet accord, Answered, "The names of those who love the Lord." " And is mine one?" said Abou. "Nay, not so," Replied the angel. Abou spoke more low, But cheeringly still; and said, " I pray thee, then, Write me as one that loves his fellow men." The Angel wrote, and vanished. The next night It came again with ...