Dec

18

/*Write a program in Java to accept a sentence and convert all its letters into upper case. Count and print the number of letters of the English alphabet that have been used in the sentence (i.e. if a letter occurs 1 or more times it is counted as 1).
For example, if the sentence entered is:
“FOR ME PARENTS ARE GOOD ON EARTH”
Then the output should be: 13 letters.
*/

— Thank you Tarushi, CMS Gomti Nagar, Lucknow for asked this questions

import java.util.*;
public class Prog2
{
public void main(String str){
str=str.toUpperCase(); //to Convert sentence stored in str into Capital Letters
int len=str.length(); // to get the length of the sentence
int count=0; // count variable will be used to count the unique chars in sentence.
String temp=””; // temp string we are using to hold strings have only alphabets step by step in below loop
for(int i=0;i<len;i++)
{
char ch=str.charAt(i); //storing single char of each step in ch variable
if(ch<=’A’ && ch<=’Z’) // condition to check whether ch has an alphabet
{
if(temp.indexOf(ch)==-1) //condition to check; did char in ch exists in temp if not increase count.
count++;
temp=temp+ch; // put every char in temp fetch in each iteration
}
}
System.out.println(str); // to print sentence in capital letters
System.out.println(count+” letters”); // to print unique occurence of letters in accepted sentence.
}
}

Dec

17

/*Write a program in Java to accept a number. Using the digits of this number create the largest possible number.
For example, if the sentence entered is: 311409 then the largest possible number using its digits will be 943110.
*/

— Thank you Tarushi, CMS Gomti Nagar, Lucknow for asked this questions

import java.util.*;
public class Prog1
{
public void main(int num){
int len=0; // variable to store the of the number of digits in the given number.
int temp=num; // variable hold the value of num temporarily
while(temp!=0) // Loop used to find the length of the number
{
temp=temp/10;
len++;
}
int i=0;
int arr[]=new int[len]; // int arr is defined with len find from above loop
while(num!=0) // loop used to store each digit from given number to arr
{
arr[i]=num%10;
num=num/10;
i++;
}
Arrays.sort(arr); // it will sort the digits stored in ascending order
for(i = len – 1; i >= 0; i–) // Loop used to form greatest number using digits stored in arr in ascending order therefore this loop starts from len-1 till 0
{
num = num * 10 + arr[i];
}
System.out.println(num); //printing the greatest number
}
}