Académique Documents
Professionnel Documents
Culture Documents
import java.io.*;
class triangle
{
public static void display(String args[]) throws IOException
{
int n; //initializing n
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter Value of n =");
String v =br.readLine();
//inputing String s
n = Integer.parseInt(v);
//converting v to integer type
System.out.println(" M E N U ");
//displaying
System.out.println("1 for Triangle");
System.out.println("2 for Inverted Triangle");
System.out.println();
System.out.println("Enter your own choice");
String x = br.readLine();
//inputing string x
int ch = Integer.parseInt(x);/
/converting x into integer type
switch(ch) //running switch case
{
case 1:
int i,j;
//initializing i and j
for(i=1;i<=n;i++)
//running the for loop n number of times
{
for(j=1;j<=i;j++)
System.out.print(i);
//displaying i
System.out.println(" ");
}
break;
case 2:
for(i=n;i>=1;i--)
//running the for loop n number of times
{
for(j=i;j>=1;j--);
System.out.print(+i);
System.out.println(" ");
}
break;
default: //if the user does not give the above
condition then it will pass to default option and will print invalid option
System.out.println("INVALID OPTION");
}
}
}
Q3)WAP to input a sentence and print the number of characters found in the longe
st word of the given sentence.
class LongestWord
{
public void WordSize(String str) //accepting string str
{
char ch; //initializing char ch
str =str+" ";
int len = 0;
int x=str.length(); //calculating the length of th
e sentence
System.out.println("Enter the sentence::" +str);
System.out.println("\n");
for(int i=0;i<x;i++)
{
ch=str.charAt(i); //counting the number of ch
aracter
if(ch != ' ')
{
System.out.println(ch); //displaying ch
len=len+1;
}
if(ch==' ')
{
System.out.println(" : The Size is" +len + " characters")
;
len=0;
System.out.println("\n");
}
}
}
}
import java.io.*;
class overloading
{
public void menu() throws IOException
{
System.out.println(" M E N U ");
System.out.println("s. Square of Integer");
System.out.println("p. Product of integers");
System.out.println("e. Equality of Strings");
System.out.println();
System.out.println("Enter your choice");
}
public void num_calc(int num,char ch) // declaring the
function
{
System.out.println("Choice=" +ch);
System.out.println("Number+" +num); //displaying
int square = num*num; //finding the squar
e
int cube = num*num*num; // finding the cube
if(ch=='s')
System.out.println("Square of the Number is =" +square);
else
System.out.println("Cube of the Number is =" +cube);
}
public void num_calc (int a , int b , char ch) //calling anot
her function
{
System.out.println("Choice=" +ch);
System.out.println("Number1=" +a);
System.out.println("Number2=" +b);
int product = a*b; //
finding the product
int sum = a+b; //
finding the sum
if(ch=='p')
System.out.println("Product of the Number is =" +product);
//displaying the result
System.out.println("Sum of the Number is =" +sum);
}
public void num_calc(String s1,String s2)
{
System.out.println("First String:" +s1);
System.out.println("Second String:" +s2);
if(s1.equals(s2))
//cheking whether the two strings are equal or not
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
}
}
import java.io.*;
class BUZZ
{
public static void main(String args[])throws IOException
{
System.out.println(" M E N U ");
System.out.println(" ");
int n, number; //initializing n and number
InputStreamReader reader = new InputStreamReader(System.in);
//inputing
BufferedReader input = new BufferedReader(reader);
System.out.print("Enter Vlue of number =");
String x =input.readLine();
//inputing string x
n = Integer.parseInt(x);
//converting x into integer type
if(n%7 ==0)
//method
System.out.println("The number is BUZZ number");
//displaying
else
if (n/10==7)
System.out.println("The number is BUZZ number");
else
System.out.println("The number is NOT A BUZZ number");
}
}
Q4b)WAP to accept 2 number and print the GCD of them
import java.io.*;
class GCD
{
public static void main(String atrs[]) throws IOException
{
System.out.println(" M E N U ");
System.out.println(" ");
int a,b;
// initializ
ing a and b
BufferedReader input = new BufferedReader(reader);
System.out.println("NOTE::a nad b are two numbers. a is greater
than b");
System.out.print("Enter Value of a =");
String x = input.readLine();
// Inputing string x
a = Integer.parseInt(x);
//converting x into
integer type
System.out.print("Enter Value of b =");
String y = input.readLine();
// Inputing string y
b = Integer.parseInt(y);
//converting y into inte
ger type
System.out.println("First Number=" +a);
System.out.println("Second Number=" +b);
int dd, div;
// initializing dd and div
if(a>b)
{
dd=a; //method
div=b;
}
else
{
dd=b;
div=a;
}
int temp=1; //initializing tem
p
while(temp !=0);
temp =dd%div;
if(temp==0)
{
System.out.println("GCD is=" +div); //display
ing
}
else
{
dd=div;
div = temp;
}
}
}
import java.io.*;
class Result
{
public static void main (String args[]) throws IOException
{
int roll[] = new int [50;
//int roll can store 50 values
int sub1[] = new int [50];
//int sub 1 can store 50value
int sub2[] = new int[50];
//int sub 2 can store 50value
int sub3[] = new int[50];
//int sub 3can store 50value
int total[] = new int [50];
//int total can store 50value
double average[]=new double[50];
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
for(int i=0;i<=4;i++)
{
System.out.println("Enter Roll No. of the student");
String rn=br.readLine();
//inputing rn
roll[i]=Integer.parseInt(rn);
//converting rn to integer type
System.out.println("Enter Marks in Subject A");
String a=br.readLine();
//inputing a
sub1[i]=Integer.parseInt(a);
//converting a to integer type
System.out.println("Enter Marks in Subject B");
String b=br.readLine();
//inputing b
sub2[i]=Integer.parseInt(b);
//converting b to integer type
System.out.println("Enter Marks in Subject C");
String c=br.readLine();
//inputing c
sub3[i]=Integer.parseInt(c); //
converting c to integer type
total[i]= sub1[i]+ sub2[i]+sub3[i];
average[i]=(double)total[i]/3;
// finding the average
}
System.out.println(" R E S U L T ");
System.out.println("Roll No. Subject A Subject B Subject C A
verage");
System.out.println("----- --------- ------- --------- -
------");
for(int k=0;k<=4;k++)
{
int n;
n=k+1;
System.out.println(n +" "+sub1[k]+" "+sub2[k]+" "+su
b3[k]+" "+average[k]);
}
int max=total[0];
int high1=sub1[0];
int high2=sub2[0];
int high3=sub3[0];
int r=0;
int roll1=0,roll2=0,roll3=0;
for(int i=1;i<=4;i++)
{
if(total[i]>max)
{
max=total[i]; // method
r=roll[i];
}
if(sub1[i]>high1)
{
high1=sub1[i];
roll1=roll[i];
}
if(sub2[i]>high1)
{
high2=sub2[i];
roll2=roll[i];
}
if(sub3[i]>high1)
{
high3=sub3[i];
roll3=roll[i];
}
}
System.out.println("Total Highest Marks are="+max+ "obtained by
roll number"+r);
System.out.println("Highest Marks in Subject A are="+high1+"obt
ained by roll number"+roll1); // displaying the result
System.out.println("Highest Marks in Subject B are="+high2+"obt
ained by roll number"+roll2);
System.out.println("Highest Marks in Subject C are="+high1+"obt
ained by roll number"+roll3);
}
}
BOOK QUESTION
QI) Define a class called NumberProblems, which has the following functions
Int sumOfFactors(int n)-which returns sum of all the factors of the numb
er n except itself.
boolean isPerfect(int n)- which returns true if n is perfect and false oth
erwise.
void perfect Nos Below(int lim)-which first prints out all perfect numbers les
s than lim.Each perfect number should be printed on a single line along with its
factors. So for example the output from perfect Nos Below(10): 6=(1,2,3)
class NumberProblems
{
int sumOfFactors(int n) //calling function
{
int s=0,i; //intializing the variables
for(i=1; i<=n/2 ; i++) //running the for loop
{
if(n%1==0) //cheking wether it gives 0 as remainder
{
s=s+i;
}
}
return s; //returning the value
}
boolean isPerfect(int n) //declaring another function under the s
ame class
{
int f;
f=sumOfFactors(n); //calling the previous function
if(f==n) //cheking weth
er f is equal to n
{
return true; // if f=n it will return t
rue
}
else
{
return false; //if f is not equal to
n then it will return false
}
}
void perfectNosBelow (int lim) //declaring another function unde
r the same class
{
for(int i=1;i<=lim;i++)
{
if( isPerfect(i)) //calling the previ
ous function
{
System.out.print(i); //displaying i
}
}
}
void display(int n)
{
System.out.print( n+"=(" );
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
System.out.print(i +",");
}
System.out.print(")");
}
}
b)
class RecurringPatterns {
public void display(int n){
int i,j;
for(i=1;i<=n;i++) { //RUNNING THE FOR LOOP
for(j=1;j<=n-i;j++) {
System.out.print(" "); //DISPLAYING SPACE
}
for(j=1;j<=i;j++) {
System.out.print(j); //NESDTED LOOPING
}
for(j=i-1;j>=1;j--) {
System.out.print(j);
}
System.out.println();
}
for(i=n-1;i>=1;i--) {
for(j=1;j<=n-i;j++){
System.out.print(" "); }//DISPLAYING THE SPACE
for(j=1;j<=i;j++) {
System.out.print(j); }
for(j=i-1;j>=1;j--){
System.out.print(j); }
System.out.println();
}
}
}
c)
class RecurringPatterns{
void pats1(int n){
int i,j,k,l,m; //INITIALIZING j,k,lm
i=j=k=l=m=0;char ch='a';
for(i=0;i<n;i++){
for(j=1;j<=n-i;j++){ //running the for loop
System.out.print(ch++); //displayin ch++
}
for(k=1;k<=2*i;k++){
System.out.print(" ");
}
for(l=j-1;l>=1;l--){
System.out.print(--ch);
}
System.out.println("");
}
for(i=1;i<n;i++){
ch='a';
for(j=0;j<=i;j++){
System.out.print(ch);ch++;
}
for(k=2;k<((2*n)-(2*i));k++){ //running the for l
oop
System.out.print(" ");
}
for(l=j;l>=1;l--){
System.out.print(--ch);
}
System.out.println("");
}
}
}