Monday, 22 July 2019

JAVA PROGRAMS

JAVA PROGRAM-

Find the smallest and largest number in m*n matrix and sort them in ascending order also print its row and column value.

SOLUTION:


import java.util.*;
class matrix
{
public static void main(String args[])
{  int p=0,smallest=4999,larger=0,maxr=0,maxc=0,minr=0,minc=0;     ////global varibles//////
Scanner sc=new Scanner(System.in);       ////input through 'util' package////
System.out.println("enter m*n");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];    ///creating m*n matrix///
int b[]=new int[1000];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();        ///input elements in matrix////
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]<smallest)      ////for checking the number is smaller or not/////
{
smallest=a[i][j];              ////storing the smaller number in new array////
maxr=i;                      ////storing the row of smaller number////
maxc=j;                     ////same as storing the column of smaller number////
}
if(a[i][j]>larger)                   ////for checking the number is larger or not/////
{
larger=a[i][j];                         ////storing the larger number in new array///
minr=i;                                   ////storing the row of larger number////
minc=j;                                    ////same as storing the column of column number////
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[p]=a[i][j];                    /////storing all the elements in single dimensional array///
p++;
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<p-i-1;j++)
{
if(b[j]>b[j+1])                      ////sort the elements in ascending order////
{
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[p]+"\t");
p++;
}
System.out.println();
}
System.out.println("largest"+"="+larger);             /////for printing///
System.out.println("Row : "+minr);
System.out.println("Column : "+minc);
System.out.println("smallest"+"="+smallest);
System.out.println("Row : "+maxr);
System.out.println("Column : "+maxc);
}
}
--------------------end--------------------------


Tuesday, 16 April 2019

PRIME_STRING PROGRAM THROUGH JAVA

PRIME_STRING PROGRAM THROUGH JAVA

import java.util.*;
class primeString
{
public static void main(String args[])
{   int p=0,s=0,h=0;
   String x="",m="";
   char iq='\u0000';
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String n=sc.nextLine();
String a[]=new String[1000];
int y[]=new int[1000];
char c1[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
n=n+" ";
for(int i=0;i<n.length();i++)
{
char ch=n.charAt(i);
if(ch!=' ')
{
x=x+ch;
}
else
{
a[p]=x;
p++;
x="";
}
}
for(int i=0;i<p;i++)
{
int l=a[i].length();
for(int j=0;j<l;j++)
{
for(int k=0;k<26;k++)
{
char ch=a[i].charAt(j);
if(ch==c1[k])
{
s=s+k+1;
}
}
}
y[h]=s;
h++;
s=0;
}
for(int t=0;t<h;t++)
{ int q=0;
for(int i=1;i<=y[t];i++)
{
if(y[t]%i==0)
q++;
}
if(q==2)
{
iq=a[t].charAt(0);
for(int j=a[t].length()-1;j>0;j--)
{
char ch1=a[t].charAt(j);
if(ch1!=' ')
{
m=m+ch1;
}
}
System.out.print(iq+m);
m="";
iq='\u0000';
}
else
{
for(int j=0;j<a[t].length();j++)
{
char ch1=a[t].charAt(j);
if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u')
{
System.out.print("*");
}
else
System.out.print(ch1);
}
}
System.out.print(" ");
}
}
}

OUTPUT :

INPUT: Enter Sentence
  this is an apple.

OUTPUT: th*s *s *n *ppl*.
--------------------END-----------------

Friday, 12 April 2019

AMSTRONG PROGRAM THROUGH JAVA

import java.util.*;
class amstrong{
public static void main(String aefe[])
{
int s=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number:");
int a=sc.nextInt();
int a1=a;
while(a>0)
{
int k=a%10;
s=s+k*k*k;
a=a/10;
}


if(a1==s)
{
System.out.println("amstrong is:"+a1);
}
else{
System.out.println("not amstrong:"+a1);
}
}}

Wednesday, 3 April 2019

HOW TO CREATE KBC GAME IN JAVA

HOW TO CREATE KBC GAME IN JAVA 

--------------------------------------------------------------------------------------------------------------------------
import java.util.*;
class kbc
{
public static void main(String args[])
{   int t=1,tp=0;
    char an='0',an1='0',an2='0',an3='0',an4='0';
while(t>=1)
{
String A[]={"Where was the first Constituent Assembly held ?","Which planet looks reddish in the night sky ?","Which bank is the banker of the banks ?","'Gobar gas' contains mainly which gas ?","Which is the biggest lake in india ?"};
char n[]={'B','D','C','A','B'};
int p=0,q=0;
String na="";
Scanner sc=new Scanner(System.in);
System.out.println("               WELCOME               ");
System.out.println("        KON BANEGA CROREPATI 2017        ");
System.out.println("if u want to start the game press Y for YES if not then press N for NO");
char a=sc.next().charAt(0);
if(a=='Y')
{
System.out.println("........THE FIRST QUSTION ON YOUR SCREEN........");
System.out.println(A[0]);
System.out.print("A)LUCKNOW          B)DELHI");
System.out.println("          C)KANPUR          D)BANARAS");
System.out.println("Enter your option");
}
an=sc.next().charAt(0);
if(n[0]==an)
{
System.out.println("<<<<Congrats your answer is correct>>>>");
System.out.println("You have won Rs.1000/-");
p=1000;
System.out.println("press Y for next Question");
char y=sc.next().charAt(0);
System.out.println("........THE Second QUSTION ON YOUR SCREEN........");
System.out.println(A[1]);
System.out.print("A)JUPITER          B)SUN");
System.out.println("          C)VENUS          D)MARS");
System.out.println("Enter your option");
an1=sc.next().charAt(0);
}
if(n[1]==an1)
{     
System.out.println("<<<<Congrats your answer is correct>>>>");
System.out.println("You have won Rs.2000/-");
p=p+1000;
System.out.println("press Y for next Question");
char Y1=sc.next().charAt(0);
System.out.println("........THE Third QUSTION ON YOUR SCREEN........");
System.out.println(A[2]);
System.out.print("A)SBI          B)BOB");
System.out.println("          C)RBI          D)CBI");
System.out.println("Enter your option");
an2=sc.next().charAt(0);
}
if(n[2]==an2)
{     
System.out.println("<<<<Congrats your answer is correct>>>>");
System.out.println("You have won Rs.3000/-");
p=p+1000;
System.out.println("press Y for next Question");
char Y2=sc.next().charAt(0);
System.out.println("........THE forth QUSTION ON YOUR SCREEN........");
System.out.println(A[3]);
System.out.print("A)METHANE         B)NITROGEN");
System.out.println("          C)OXYGEN          D)HYDROGEN");
System.out.println("Enter your option");
an3=sc.next().charAt(0);
}
if(n[3]==an3)
{     
System.out.println("<<<<Congrats your answer is correct>>>>");
System.out.println("You have won Rs.4000/-");
p=p+1000;
System.out.println("press Y for next Question");
char Y2=sc.next().charAt(0);
System.out.println("........THE forth QUSTION ON YOUR SCREEN........");
System.out.println(A[4]);
System.out.print("A)GANGA         B)CHILKA LAKE");
System.out.println("         C)NILE         D)TAPU");
System.out.println("Enter your option");
an4=sc.next().charAt(0);
}
if(n[4]==an4)
{     
System.out.println("<<<<Congrats your answer is correct>>>>");
System.out.println("You have won Rs.5000/-");
p=p+1000;

else
{
tp=p;
System.out.println("<<<<oppsss your answer is wrong>>>>");
System.out.println("total amount you win = "+tp);
}
System.out.println("if you want to play again then enter Y and if not then press N");
char w=sc.next().charAt(0);
if(w=='Y')
{
t=1;
}
else
{
t=0;
}
}
}
}

--------------------------------------end--------------------------------------

PRIME NUMBER PROGRAM THROUGH JAVA

Code :

import java.util.*;
class prime
{
public static void main(String hee[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the prime number");
int a=sc.nextInt();
//int c=0;
if(a%2==0)
{

System.out.println("prime no:"+a);
}
else
{
System.out.println("not prime:"+a);
}
}
}
------------------end-----------