HCL
Company
Programming
Database
Write a program in JAVA which print 0 if 1 is provided as input and print 1 if 0 is provided.
Condition apply :
(i.) you can't use predefined function(like logical not).
(ii.) You can't use if-else condition.
Read Solution (Total 11)
-
- import java.util.*;
import java.util.scanner;
class Program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
Stirng s=i>0?"0":"1":
System.out.println(s);
}
} - 10 years agoHelpfull: Yes(26) No(2)
- Lets take n as the input no.
{
system.out.println(1-n)
when nis 1 output is 0 and vice versa - 10 years agoHelpfull: Yes(26) No(0)
- import java.util.*;
class a{
public static void main(Sting args[]){
try{
Scanner sc=new Scanner(System.in);
int a=Integer.parseInt(sc.next());
switch(a){
case 0:a=1;System.out.println(" o changes to :"+a);
System
break;
case 1:a=0;System.out.println(" 1 changes to :"+a);
break;
default:System.out.println("please enter a valid no:");
break;
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
} - 10 years agoHelpfull: Yes(5) No(2)
- void main()
{
int ch,a;
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 0:a=1;
printf("%d",a);
break;
case 1:a=0;
printf("%d",a);
break;
default:printf("enter 0 or 1 only");
break;
}
} - 10 years agoHelpfull: Yes(4) No(1)
- using switch cases will work
- 10 years agoHelpfull: Yes(2) No(6)
- class ABC
{
- 10 years agoHelpfull: Yes(0) No(2)
- import java.util.Scanner;
public class Conversion
{
public static void main(String... s)
{ int i;
Scanner output =new Scanner(System.in);
System.out.println("enter the value of i either 1 or 0");
i=output.next();
switch(i)
{ Case 1: System.out.println("0");
break;
Case 0: System.out.println("1");
break;
default: System.out.println("wrong value");
}
}
} - 10 years agoHelpfull: Yes(0) No(0)
- /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tricky.java.program;
import java.util.Scanner;
/**
*
* @author vikash
*/
public class TrickyJavaProgram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt();
int b=n;
int a[]= new int[2];
a[0]=1;
a[1]=0;
System.out.println(String.valueOf(a[b]));
}
}
- 8 years agoHelpfull: Yes(0) No(0)
- public class Check {
public static void main(String[] args) {
int i=1;
for(;i==1;)
{
i++;
System.out.println("0");
}
for(;i==0;)
{
i++;
System.out.println("1");
}
}
}
for loop's second segment holds matter of checking and validating expression.. using that idea i ave implemented the program, assigning the other number based on checking and printing the assigned value gets the right answer. - 6 years agoHelpfull: Yes(0) No(0)
- import java.util.*;
class solution{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println(1-a);
}
} - 6 years agoHelpfull: Yes(0) No(0)
- #include
int main() {
int n,s;
printf("enter only 0 0r 1 in n value");
scanf("%d",&n);
s=(n==0?1:0);
printf("n%d",s);
} - 4 years agoHelpfull: Yes(0) No(0)
HCL Other Question