nitin466
BAN USER@chriscow: you are right , thanks for suggestion abt recursive function and for output 0^0, the solution can be rectified by order of changing the condition statement :
Also i think result should be initialized with value of a rather than 1(which result in giving output a^(b-1))
statement :
public class Indices {
public static int prod(int a, int b) {
if(b==0)
return 1;
else if (a==0)
return 0;
int result = a;
for (int i = 1; i<b; i++) result *= a;
return result ;
}
public static void main(String[] args){
System.out.println(prod(a,b));
}
}
The solution suggested above:
(Sum of natural number from 1 to 100) - (sum of all the elements present in the given array) will give the missing number and infact smart way too :)
I can also think of another solution just let me know if this is efficient enough or not :
Sort the array in increasing order(if not already given in sorted fashion). Then the difference of any two consecutive number should be 1, wherever its not that is our culprit .
Java code for the same:
public class FindMissing {
public static void main(String[] args) {
int array[] = { here sorted input array will come};
for(int i = 0; i<array.length-1; i++){
if(array[i+1]-array[i] !=1)
System.out.println("The missing number is: " + (array[i] + 1));
}
}
}
Assuming b is integer and for b negative we can make some minor change in code to get the result.
public class Indices {
public static int prod(int a, int b) {
if(a==0)
return 0;
else if (b==0)
return 1;
else
return a*prod(a, b-1);
}
public static void main(String[] args){
System.out.println(prod(a, b))
}
}
}
public class PrintNum2 {
public static void main(String[] args){
Print:
for(int l =0; l<10; l++){
for(int k =0; k<10; k++){
for(int j =0; j<10; j++){
for(int i =0; i<10; i++){
int num = (l*1000+k*100+j*10+i);
if(((l==k)&&(k==0)&&(i!=j)&&(i!=k)&&(num>45))||((i!=j)&&(i!=k)&&(i!=l)&& (j!=k)&& (j!=l)&& (k!=l)))
{
System.out.println(num);
if(num >= 4578)
break Print;
}
}
}
}
}
}
}
Using Array:
//conversion of string
//Use SIMPLE LOGIC for Converting this string str="aaabbccc" into str="3a2b3c".
public class Compression{
public static void main(String[] args){
char a[] = {'a','a','a','b','b','b','b','c','c','d','d','d','d','d','d','e','e'} ;
int sum = 1;
for(int i =0; i<(a.length-1); i++){
if(a[i+1]==a[i])
{
sum = sum +1;
if(i==a.length-1)
System.out.print(sum+""+a[i]);
}
else
{
System.out.print(sum+""+a[i]);
sum = 1;
}
}
}
}
- nitin466 July 11, 2016