Program No 1:
Input any word in lower case and display its output in uppercase.
Program using the main function
import java.io.*;
class CaseChange
{
String s1,s2="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeString() throws Exception
{
System.out.print("\nEnter the word:");
s1=br.readLine();
for(int i=0;i< s1.length();i++)
{
if(Character.isUpperCase(s1.charAt(i)))
s2=s2+Character.toLowerCase(s1.charAt(i));
else
s2=s2+s1.charAt(i);
}
System.out.println(s2);
}
public static void main(String args[])throws Exception
{
CaseChange ob=new CaseChange();
ob.takeString();
}
}
Program exclusively for BlueJ Editor
class CaseChange
{
public void takeString(String s1)
{
String s2="";
for(int i=0;i< s1.length();i++)
{
if(Character.isUpperCase(s1.charAt(i)))
s2=s2+Character.toLowerCase(s1.charAt(i));
else
s2=s2+s1.charAt(i);
}
System.out.println(s2);
}
}
Program No 2:
Total number of alphabets present in the word
.
For example, if
Lower case :
snowy
|
||||
Upper case:
SNOWY
|
||||
Total number
of alphabets : 5.
Program using main function
import
java.io.*;
class CountLetters
{
String s1;
int i,len,count=0;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
public void takeString() throws Exception
{
System.out.println("Enter the
string:");
s1=br.readLine();
len=s1.length();
for(int i=0;i< len;i++)
{
if(Character.isLetter(s1.charAt(i)))
count++;
}
System.out.println("No of
alphabets:"+count);
}
public static void main(String
args[])throws Exception
{
CountLetters ob=new CountLetters();
ob.takeString();
}
}
Program exclusively for BlueJ Editor
lass CountLetters
{
public void takeString(String s1)
{
int count=0;
int len=s1.length();
for(int i=0;i< len;i++)
{
if(Character.isLetter(s1.charAt(i)))
count++;
}
System.out.println("No of
alphabets:"+count);
}
}
Program No 3
Write
a program to store elements in an array of size 4
(a)
each
row and display
(b)
each
column and display
import java.io.*;
class Matrix
{
int arr[][]=new int[4][4];
int i,j,sum;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeValues() throws Exception
{
for(i=0;i< 4;i++)
{
for(j=0;j< 4;j++)
{
System.out.print("\nValue:");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
}
public void showResult()
{
System.out.println("\nElements in the array:\n");
for(i=0;i< 4;i++)
{
for(j=0;j< 4;j++)
{
System.out.print("
"+arr[i][j]);
}
System.out.println();
}
System.out.println("\nRow wise
sum of the elements:\n");
for(i=0;i< 4;i++)
{
sum=0;
for(j=0;j< 4;j++)
{
sum+=arr[i][j];
}
System.out.println("Row
Number:"+ (i+1)+"="+sum);
sum=0;
}
System.out.println("\nColumn
wise sum of the elements:\n");
for(i=0;i< 4;i++)
{
sum=0;
for(j=0;j< 4;j++)
{
sum+=arr[j][i];
}
System.out.println("Row
Number:"+ (i+1)+"="+sum);
sum=0;
}
}
public static void main(String
args[])throws Exception
{
Matrix ob=new Matrix();
ob.takeValues();
ob.showResult();
}
}
Sample Input Output
Value:6
Value:1
Value:2
Value:3
Value:4
Value:33
Value:4
Value:5
Value:6
Value:5
Value:6
Value:6
Value:5
Elements in the array:
3 4 5 6
1 2 3 4
33 4 5 6
5 6 6 5
Row wise sum of the elements:
Row Number:1=18
Row Number:2=10
Row Number:3=48
Row Number:4=22
Column wise sum of the elements:
Row Number:1=42
Row Number:2=16
Row Number:3=19
Row Number:4=21
Progran No 4
Read N numbers in a single dimensional array and
calculate the highest
and the least
number along with
their corresponding
positions.
For example, if
INPUT : 8, 4, 5,
1, 7
OUTPUT,
Highest : 8 Position
: 1
Lowest : 1 Position : 4
import java.io.*;
class Array1
{
int arr[];
int i,min,max,minp,maxp,n;
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
public void takeValues()
throws Exception
{
System.out.print("\nHow many elements to store:");
n=Integer.parseInt(br.readLine());
arr=new int[n];
System.out.println("\nEntering Values:");
for(i=0;i< n;i++)
{
System.out.print("\nValue:");
arr[i]=Integer.parseInt(br.readLine());
}
}
public void
showResult()
{
max=arr[0];
min=arr[0];
minp=0;
maxp=0;
for(i=1;i< n;i++)
{
if(arr[i]>max)
{
max=arr[i];
maxp=i+1;
}
else
if(arr[i]< min)
{
min=arr[i];
minp=i+1;
}
}
System.out.println("Entered Values:");
for(i=0;i<
n;i++)
System.out.print(" "+arr[i]);
System.out.println("Maximum Value= "+max);
System.out.println("Maximum Value position= "+maxp);
System.out.println("Minimum Value= "+min);
System.out.println("Minimum Value position= "+minp);
}
public static void
main(String args[])throws Exception
{
Array1 ob=new Array1 ();
ob.takeValues();
ob.showResult();
}
}
Sample Input and Output
How many elements to store:12
Entering Values:
Value:44
Value:5
Value:66
Value:5
Value:4
Value:555
Value:4
Value:4
Value:3
Value:33
Value:23
Value:4
Entered Values:
44 5 66 5 4 555 4 4 3 33 23
4
Maximum Value= 555
Maximum Value position= 6
Minimum Value= 3
Minimum Value position= 9
|
[15]
|
No comments:
Post a Comment