Sunday 31 July 2016

Square Pattern in java

Question No:8 print Square
i/p: 5

* * * * *
*       *
*       *
*       *
* * * * *

import java.util.Scanner;

public class Square {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++){
                if(i==1||j==n||i==n||j==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
           
           
        }
    }
}

Saturday 30 July 2016

ButterFly Pattern In Java

Question No:9 Butter Fly
i/p: 5
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *

import java.util.Scanner;
public class ButterFly {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter n value");
        int n=sc.nextInt();
        int sp=2*n-1,st=0;
        for(int i=1;i<=2*n-1;i++){
            if(i<=n){
                sp=sp-2;
                st++;
            }
            else{
                sp=sp+2;
                st--;
            }
            for(int j=1;j<=st;j++){
                System.out.print("*");
            }
            for(int k=1;k<=sp;k++){
                System.out.print(" ");
            }
            for(int l=1;l<=st;l++){
                if(l!=n)
                System.out.print("*");
            }
            System.out.println();
        }
    }
}


Wednesday 27 July 2016

All Logical Questions are Answered JumpInToIt
 Q.7 print the non-repeated elements from a Random Array i/p;{10,20,4,5,6,10,5} o/p:{20,4,6}
public class NonRepeat {

    public static void main(String[] args) {
        int arr[]={
10,20,4,5,6,10,5};
        for(int i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    arr[i]=arr[j]=-1;
                }
            }
        }
        for(int a:arr){
            if(a!=-1)
            System.out.print(a+" ");
        }
    }
}
Q.6 swap two numbers without using third variable and any arithmatic operators.(in c Programming)
#include<stdio.h>
 
int main() {
   int num1, num2;
 
   printf("\nEnter First Number : ");
   scanf("%d", &num1);
 
   printf("\nEnter Second Number : ");
   scanf("%d", &num2);
 
   num1 = num1 ^ num2;
   num2 = num1 ^ num2;
   num1 = num1 ^ num2;
 
   printf("\n Numbers after Exchange : ");
   printf("\n Num1 = %d and Num2 = %d", num1, num2);
 
   return(0);
}
Q.5 Devide two Numbers without Using *,/,% operators print quotient,Reminder 
public class DevideNum {
    public static void main(String[] args) {
        int a=54,b=5,c=0;
        while(b<=a){
            a=a-b;
            c++;
        }
        System.out.println("Reminder"+a);
        System.out.println("Quotient"+c);
    }
}

Saturday 23 July 2016

Q.4 Print the Character Count in Entered String
i/p:APPLE o/p:A-1 P-2 L-1 E-1
import java.util.Scanner;

public class CharCount {

public static void main(String[] args) {
int count=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.next();
char ch[]=s.toCharArray();
for(int i=0;i<ch.length;i++){
count=1;
for(int j=i+1;j<ch.length;j++){
if(ch[i]==ch[j]){
count++;
ch[j]='@';
}
}
if(ch[i]!='@'){
System.out.println(ch[i]+" "+count);
}

}
}
}
Q.3 Change letters of the entered string from Uppercase to Lowercase and vice versa 
import java.util.Scanner;

public class ReverseCase {

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.next(); 
char ch[]=s.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i]<90){
ch[i]=(char) (ch[i]+32);
}
else{
ch[i]=(char) (ch[i]-32);
}
}
System.out.println(ch);

}
}
Q.2 Reverse the given Stirng by word
import java.util.Scanner;

public class ReverseByWord {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Sentence");
String s=sc.nextLine();
System.out.println(s);
String s1="",rev="";
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
s1=s.charAt(i)+s1;
}
else{
rev=rev+" "+s1;
s1="";
}
}rev=rev+" "+s1;
System.out.println(rev);
}

}
Q.11 Find whether entered String is Palindrome or not.

import java.util.Scanner;

public class StringPal{

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter String");

String s=sc.next();String s1="";
for(int i=s.length()-1;i>=0;i--){
s1=s1+s.charAt(i);
}
System.out.println(s1);
if(s1.equals(s)){
System.out.println("palindrome");
}
else{
System.out.println("not palindrome");
}
}
}
Q.10 Find whether given number is palindrome or not?
public class Palindrome {

public static void main(String[] args){
int n=636;
int sum=0,r,temp;
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum){
System.out.println(temp+" is a Palindrome");
}
else
System.out.println(temp+" is not a Palindrome");
}
}
Q.13 Find whether the given number is Amstrong Number or not?
import java.util.Scanner;

public class Armstrong {

public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter a Number to check it is a Armstrong Number or Not");
int n=in.nextInt();
in.close();
int a=n,reminder=0,sum=0;
while(a>0){
reminder=a%10;
a=a/10;
sum=sum+reminder*reminder*reminder;
}
if(sum==n){
System.out.println("Given Number "+n+" is a ArmstrongNumber");
}
else
System.out.println("Given Number "+n+" is not an ArmstrongNumber");
}
}
Q.12 Print Fibonacci Series using Recursion.
import java.util.Scanner;

public class fiborec
{
static int fibo(int n){
if(n==0)
return 0;
else if(n==1)
        return 1;
else
return (fibo(n-1)+fibo(n-2));
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter n value");
int n=sc.nextInt();
for(int c=0;c<=n;c++){
System.out.println(fibo(c));
}
}
}
Q.9 Find the PrimeNumbers in between given Range,Print count and their sum?
import java.util.Scanner;
public class PrimeNumbers {

public static void main(String[] args){
Scanner in =new Scanner(System.in);
System.out.println("Enter Starting Number");
int start=in.nextInt();
System.out.println("Enter Ending Number");
int end=in.nextInt();
int count=0;
int sum=0;
for(int i=start;i<=end;i++){
boolean isPrime=true;

for(int j=2;j<=i/2;j++){
if(i%j==0){
isPrime=false;
break;
}
}
if(isPrime&i>0)
{
System.out.print(i+" ");
count++;
sum=sum+i;
}
}in.close();
System.out.println();
System.out.println("Count = "+count);
System.out.println("Sum = "+sum);
}

}
Q.8: Factorial of Given number using Recursion.
class  Factorial
{
long fact(int n)
{
if(n==0||n==1)
return 1;
else
return n*fact(n-1);
}

public static void main(String[] args) 
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
Factorial f=new Factorial();
long fact=f.fact(n);
System.out.println("Factorial of given Num is: "+fact);

}
}

Friday 22 July 2016

Q.1 Find Three Largest Numbers from an Unsorted Array ,using single for loop,without sorting.? i/p: Array [10,18,73,2,84,9,20,66,22] o/p:first:84, second:73, third:66
public class ThreeLargest { public static void main(String[] args) { int f=0,s=0,t=0; int[] x={10,18,73,2,84,9,20,66,22}; for(int i=0;i<x.length;i++){ if(x[i]>f){ t=s;s=f;f=x[i]; }else if(x[i]>s){a t=s;s=x[i]; }else if(x[i]>t){ t=x[i]; } } System.out.println("first :"+f+" second :"+s+ " third :"+t); } }

Featured post

All Logical Questions are Answered JumpInToIt