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); | |
} | |
} |
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); | |
} | |
} |
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"); | |
} | |
} |
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"); | |
} | |
} |
Comments
Post a Comment