Sei sulla pagina 1di 7

Cognizant Programming Questions by Sudhir

Lambhate T&P-CDGI

Cognizant Programming Questions

1) Write a program to reverse any number.


This code below is written in Java.

1. package javaapplication6;
2. import java.util.Scanner;
3. public class JavaApplication6 {
4. public static void main(String[] args)
5. {
6. int i, temp, sum=0, n;
7. Scanner sc = new Scanner(System.in);
8. n=sc.nextInt();
9. temp=n;
10. while(n>0)
11. {
12. int r = n%10;
13. sum = sum*10+r;
14. n = n/10;
15. }
16. System.out.println("Reverse of Number is:-" +sum);
17. if(temp==sum)
18. {
19. System.out.println("Palindrom");
20. }
21. else
22. {
23. System.out.println("Not Palindrom");
24. }
25. }
26. }

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI

2) Write a program to find out some of the digits of the given


number.
1. package javaapplication6;
2. import java.util.Scanner;
3. public class JavaApplication6 {
4. public static void main(String[] args)
5. {
6. int i, temp, sum=0, n;
7. Scanner sc = new Scanner(System.in);
8. n=sc.nextInt();
9. temp=n;
10. while(n>0)
11. {
12. int r = n%10;
13. sum = sum+r;
14. n = n/10;
15. }
16. System.out.println("Reverse of Number is:-" +sum);
17. }
18. }

3) Write a program to find out the power of a number.


1. package javaapplication6;
2. import java.util.Scanner;
3. public class JavaApplication6 {
4. public static void main(String[] args)
5. {
6. int result=1, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.println("the Exoponent is:- ");
9. n=sc.nextInt();
10. System.out.println("the base is:- ");
11. int base = sc.nextInt();
12.

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI
13. while (n != 0)
14. {
15. result *= base;
16. --n;
17. }
18. System.out.println("Power of Number is:-" +result);
19. }
20. }

4) Write a program to add two numbers without using the addition


operator.
1. package javaapplication6;
2. import java.util.Scanner;
3. public class JavaApplication6 {
4. static int Add(int x, int y)
5. {
6. while (y != 0)
7. {
8. int carry = x & y;
9. x = x ^ y;
10. y = carry << 1;
11. }
12. return x;
13. }
14. public static void main(String arg[])
15. {
16. Scanner sc = new Scanner(System.in);
17. System.out.println("First value is:- ");
18. int a=sc.nextInt();
19. System.out.println("Second value is:- ");
20. int b=sc.nextInt();
21. System.out.println("Sum of both digits:- "+Add(a, b));
22. }
23. }

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI

5) Write a program to subtract two numbers without using a


subtraction operator.
1. package javaapplication6;
2. import java.util.Scanner;
3. public class JavaApplication6 {
4. static int Add(int x, int y)
5. {
6. while (y != 0)
7. {
8. int borrow = ~ x & y;
9.
10. x = x ^ y;
11.
12. y = borrow << 1;
13. }
14. return x;
15. }
16.
17. public static void main(String arg[])
18. {
19. Scanner sc = new Scanner(System.in);
20. System.out.println("First value is:- ");
21. int a=sc.nextInt();
22. System.out.println("Second value is:- ");
23. int b=sc.nextInt();
24. System.out.println("Sum of both digits:- "+Add(a, b));
25. }
26. }

6) Write a program to show largest among three numbers using


binary minus operator.
1. package javaapplication9;
2. import java.util.Scanner;
3. public class JavaApplication9 {

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI
4. public static void main(String[] args)
5. {
6. Scanner s = new Scanner(System.in);
7. int a = s.nextInt();
8. int b = s.nextInt();
9. int c = s.nextInt();
10. if(a-b>0 && a-c>0)
11. System.out.println("Greatest is a :-"+a);
12. else
13. if(b-c>0)
14. System.out.println("\nGreatest is b :-"+b);
15. else
16. System.out.println("\nGreatest is b :-"+c);
17. }
18. }

7) Write a program to show the largest among three numbers


using conditional operator.
1. package javaapplication9;
2. import java.util.Scanner;
3. public class JavaApplication9 {
4. public static void main(String[] args)
5. {
6. Scanner s = new Scanner(System.in);
7. int a = s.nextInt();
8. int b = s.nextInt();
9. int c = s.nextInt();
10. int big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
11. System.out.println("\nGreatest elements is :-"+ big);}}

8) Write a program to show the generic root of any number.


1. package javaapplication9;
2. import java.util.Scanner;

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI
3. public class JavaApplication9 {
4. public static void main(String[] args)
5. {
6. Scanner s = new Scanner(System.in);
7. int a = s.nextInt();
8. int b=0,c;
9.
10. while(a>10){
11. b=0;
12. while(a>0){
13. c=a%10;
14. a=a/10;
15. b+=c;
16. }
17. if(b>10)
18. a=b;
19. else
20. break;
21. }
22. System.out.println("\nSum of the digits in single digit is:-"+b);}}

9) Write a c program to show the prime factor of a given number.


1. package javaapplication9;
2. import java.util.Scanner;
3. public class JavaApplication9 {
4. public static void main(String[] args)
5. {
6. Scanner s = new Scanner(System.in);
7. int a = s.nextInt();
8. int b=0,c,j,i,isPrime;
9.
10. for(i=2; i<=a; i++)
11. {
12. /* Check 'i' for factor of num */
13. if(a%i==0)

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI
Cognizant Programming Questions by Sudhir
Lambhate T&P-CDGI
14. {
15. /* Check 'i' for Prime */
16. isPrime = 1;
17. for(j=2; j<=i/2; j++)
18. {
19. if(i%j==0)
20. {
21. isPrime = 0;
22. break;
23. }
24. }
25. /* If 'i' is Prime number and factor of num */
26. if(isPrime==1)
27. {
28. System.out.println(i);
29. }
30. }
31. }
32. }
33. }
1.

Cognizant Programming Questions by Sudhir


Lambhate T&P-CDGI

Potrebbero piacerti anche