Saturday 27 August 2016

Adding Two Matrices

14. Write a java Program to Add Two Matrices? 
import java.util.Scanner;

public class AddMatrix {

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

int [][]a=new int[rows][cols];
int [][]b=new int[rows][cols];

System.out.println("enter First matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("enter Second matrix");
for (int i=0;i<rows;i++) {
for(int j=0;j<cols;j++){
b[i][j]=sc.nextInt();
}
}
System.out.println("After addition");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
System.out.print(a[i][j]+b[i][j]);
}
System.out.println();
}
}
}

Wednesday 3 August 2016

Number Patterns

Question No:9
i/p: 5 
1        1
12      21
123    321
1234  4321
1234554321


 
import java.util.Scanner;
public class Pat {
    public static void main(String[] args) {
        int n=5;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            for(int j=1;j<=2*(n-i);j++){
                System.out.print(" ");
            }
            for(int j=i;j>0;j--){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Number Patterns

Question No:1
i/p:3 

53135

 313
  1
 313
53135

import java.util.Scanner;
public class Pat {
    public static void main(String[] args) {
        int n=3;
        int sum=2*n+1;int sp=-1;int flag=0;
        for(int i=1;i<=2*n-1;i++){
            if(i<=n){
                sp++;
                sum=sum-2;
            }
            else{
                sp--;
                sum=sum+2;
            }
            for(int j=1;j<=sp;j++){
                System.out.print(" ");
            }
            int temp=sum;flag=0;

            for(int k=1;k<=sum;k++){

                if(flag==0){
                    System.out.print(temp);
                    temp=temp-2;
                    if(temp<1){
                        flag=1;
                        temp=temp+2;
                    }
                }
                else{
                    temp=temp+2;
                    System.out.print(temp);
                }
            }System.out.println();
        }
    }
}

Pascal Triangle

Question No:2 Pascal Triangle
i/p: 5

    1 

   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("enter n value");
int n=sc.nextInt();
int number=1;
for(int i=0;i<=n;i++){
for(int j=0;j<=n-i;j++){
System.out.print(" ");
}
number=1;
for(int k=0;k<=i;k++){
System.out.print(number+" ");
number=number*(i-k)/(k+1);
}
System.out.println();
}
}

Number Patterns

Question No:3 
i/p: 3

  11

 1221
123321
 1221
  11

public class Pattern1221 {

    public static void main(String[] args) {
        int n=3;
        int p=0,sp=n;
        for(int i=1;i<=2*n-1;i++){
            if(i<=n){
                sp=n-i;
                p++;
            }
            else{
                sp++;
                p--;
            }
            for(int j=1;j<=sp;j++){
                System.out.print(" ");
            }
            for(int k=1;k<=p;k++){
                System.out.print(k);
            }
            for(int l=p;l>=1;l--){
                System.out.print(l);
            }
            System.out.println();
        }
    }
}

Number Patterns

Question No:6
i/p: 5

54321

4321
321
21


import java.util.Scanner;

public class NumPatern {

    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
            System.out.println("enter n value");
            int n=sc.nextInt();
            for(int i=1;i<=n;i++)
            {
                for(int j=n-i+1;j>0;j--){
                     System.out.print(j);
                }
                System.out.println();
            }
    }
}

Number Patterns

Question No:5
i/p: 5

12345

1234
123
12
1

import java.util.Scanner;

public class NumPatern {

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

Number Patterns

Question No:4 
i/p: 5

1

12
123
1234
12345


import java.util.Scanner;

public class NumPatern {

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

Star Pattern

Question No:6
i/p: 5

    *

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


import java.util.Scanner;

public class StarPatern {

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

Star Pattren

Question No:5
i/p: 5

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


import java.util.Scanner;

public class StarPatern {

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

Star Pattern

Question No:3
i/p: 5

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


import java.util.Scanner;

public class StarPatern {

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

Star Pattern

Question No:4
i/p: 5

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



import java.util.Scanner;

public class StarPatern {

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

Star Pattern

Question No:2
i/p: 5

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



import java.util.Scanner;
 

public class StarPatern {

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

Star Pattern

Question No:1
i/p: 5

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


import java.util.Scanner;

public class StarPatern {

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

Tuesday 2 August 2016

Star Triangle Pattern

Question No:7
i/p: 5

    *

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


import java.util.Scanner;

public class TriangleStar {

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

        System.out.println("enter n value");
        int n=sc.nextInt();
        for(int i=1;i<=n;i++)
        {
            for(int sp=1;sp<=n-i;sp++){
                System.out.print(" ");
            }
            for(int j=1;j<=2*i-1;j++){
                if(j%2!=0)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
           
    }
}

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));
}
}
}

Featured post

All Logical Questions are Answered JumpInToIt