Section B : 2016 Solved Theory
Question : 7
Disarium Number Using recursive technique.
Sample Input : 135
135 = 1¹ + 3² + 5³ = 1 + 9 + 25 = 135
Sample Output : 135 is a Disarium Number.
import java.util.*;
public class Disarium
{
int num,size;
Disarium(int nn)
{
num=nn;
size=0;
}
void countDigit()
{
int t;
t=num;
for(;t!=0;)
{
t=t/10;
size++;
}
}
int sumOfDigits(int n,int p)
{
int r;
r=(int)(Math.pow(n%10,p));
p--;
n=n/10;
if(p==0)
{
return (r);
}
else
{
return (r+sumOfDigits(n,p));
}
}
void check()
{
int d;
d=sumOfDigits(num,size);
if(d==num)
{
System.out.println(num+" is Desarium Number.");
}
else
{
System.out.println(num+" is not a Desarium Number.");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
int no;
System.out.println("Enter a number to check if it is Desarium Number : ");
no=in.nextInt();
Disarium ob=new Disarium(no);
ob.countDigit();
ob.check();
}
}
public class Disarium
{
int num,size;
Disarium(int nn)
{
num=nn;
size=0;
}
void countDigit()
{
int t;
t=num;
for(;t!=0;)
{
t=t/10;
size++;
}
}
int sumOfDigits(int n,int p)
{
int r;
r=(int)(Math.pow(n%10,p));
p--;
n=n/10;
if(p==0)
{
return (r);
}
else
{
return (r+sumOfDigits(n,p));
}
}
void check()
{
int d;
d=sumOfDigits(num,size);
if(d==num)
{
System.out.println(num+" is Desarium Number.");
}
else
{
System.out.println(num+" is not a Desarium Number.");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
int no;
System.out.println("Enter a number to check if it is Desarium Number : ");
no=in.nextInt();
Disarium ob=new Disarium(no);
ob.countDigit();
ob.check();
}
}
Question :8
Shift Matrix row : 1st to 2nd, last to 1st and so on.
Sample Input :
1 2 3
4 5 6
7 8 9
Sample Output :
7 8 9
1 2 3
4 5 6
import java.util.*;
public class Shift
{
int mat[][]=new int[5][5];
int m,n;
Shift(int mm,int nn)
{
m=mm;
n=nn;
}
void input()
{
Scanner in=new Scanner(System.in);
int i,j;
System.out.println("Enter elements in matrix of size "+m+" X "+n+" : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat[i][j]=in.nextInt();
}
}
System.out.println("Matrix Formed :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
void cyclic(Shift P)
{
int i,j,t;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
P.mat[(i==(m-1))?0:(i+1)][j]=mat[i][j];
}
}
}
void display()
{
int i,j;
System.out.println("Shifted Matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
int r,c;
System.out.println("Enter number of rows : ");
r=in.nextInt();
System.out.println("Enter number of columns : ");
c=in.nextInt();
Shift ob=new Shift(r,c);
ob.input();
Shift ob1=new Shift(r,c);
ob.cyclic(ob1);
ob1.display();
}
}
public class Shift
{
int mat[][]=new int[5][5];
int m,n;
Shift(int mm,int nn)
{
m=mm;
n=nn;
}
void input()
{
Scanner in=new Scanner(System.in);
int i,j;
System.out.println("Enter elements in matrix of size "+m+" X "+n+" : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat[i][j]=in.nextInt();
}
}
System.out.println("Matrix Formed :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
void cyclic(Shift P)
{
int i,j,t;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
P.mat[(i==(m-1))?0:(i+1)][j]=mat[i][j];
}
}
}
void display()
{
int i,j;
System.out.println("Shifted Matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
int r,c;
System.out.println("Enter number of rows : ");
r=in.nextInt();
System.out.println("Enter number of columns : ");
c=in.nextInt();
Shift ob=new Shift(r,c);
ob.input();
Shift ob1=new Shift(r,c);
ob.cyclic(ob1);
ob1.display();
}
}
Question :9
Change all the consonants to upper case and arrange it at the starting of the word followed by vowel.
Sample Input : shubham
Sample Output : SHBHMum
import java.util.*;
public class ConsChange
{
String word;
int len;
ConsChange()
{
word="";
len=0;
}
void readWord()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a word in lower case : ");
word=in.nextLine();
len=word.length();
}
void shiftCons()
{
char c;
int i;
String cons="",vol="";
for(i=0;i<len;i++)
{
c=word.charAt(i);
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
{
vol=vol+c;
}
else
{
cons=cons+c;
}
}
System.out.println("Shifted Word : "+(cons+vol));
}
void changeWord()
{
char c;
int i;
String cons="",vol="";
for(i=0;i<len;i++)
{
c=word.charAt(i);
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
{
vol=vol+c;
}
else
{
cons=cons+Character.toUpperCase(c);
}
}
System.out.println("Changed Word : "+(cons+vol));
}
void show()
{
System.out.println("Original Word : "+word);
shiftCons();
changeWord();
}
public static void main()
{
ConsChange ob=new ConsChange();
ob.readWord();
ob.show();
}
}
public class ConsChange
{
String word;
int len;
ConsChange()
{
word="";
len=0;
}
void readWord()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a word in lower case : ");
word=in.nextLine();
len=word.length();
}
void shiftCons()
{
char c;
int i;
String cons="",vol="";
for(i=0;i<len;i++)
{
c=word.charAt(i);
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
{
vol=vol+c;
}
else
{
cons=cons+c;
}
}
System.out.println("Shifted Word : "+(cons+vol));
}
void changeWord()
{
char c;
int i;
String cons="",vol="";
for(i=0;i<len;i++)
{
c=word.charAt(i);
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
{
vol=vol+c;
}
else
{
cons=cons+Character.toUpperCase(c);
}
}
System.out.println("Changed Word : "+(cons+vol));
}
void show()
{
System.out.println("Original Word : "+word);
shiftCons();
changeWord();
}
public static void main()
{
ConsChange ob=new ConsChange();
ob.readWord();
ob.show();
}
}
This is really helpful
ReplyDeleteIt helped me a lot......thanks
ReplyDeleteReally helpful
ReplyDelete