Wednesday, 17 June 2020

Multiply of two numbers without using operator ( * ) in java

 Multiply of two numbers without using operator in java


In the below program we are going to see how to multiply of two numbers without using operator i.e; finding the product of two numbers without using  * operator in java programming language.

In the below program we are going to read two numbers using command line argument. So while running the program we have to pass two values.

Here we took three variable by name a, b, product.  variables a,b will hold the values and  variable product will hold the product of two numbers.


class multiply{   
   public static void main (String args[]){
        int a,  b,  product=0;
        a=Integer.parseInt(args[0]);
        b=Integer.parseInt(args[1]);
        int i=0;
        while(i<b){
            product= product + a;
            i++;
        }   
       
        System.out.println("First number  =  " + a);  
        System.out.println("Second number  =  " + b);  
        System.out.println(" Multiply of two numbers  without using operator =  " + product);  
   }
  
  }

Sunday, 14 June 2020

Reversing a string in java 2023

 Reversing a string in java

class StringReverseDemo {
    public static void main(String[] args) {
        String str = "Hello";     
                     
        // convert string into char array and store in tempArray.
       
            char[] temp = str.toCharArray();

        char[] charRevArray = new char[str.length()];

        // copying the values of temp array to charRevArray in reverse order.

        for (int j = 0; j < str.length(); j++) {
            charRevArray[j] = temp [str.length() - 1 - j];
        }
       
        // convert char array to String.

        String reversePalindrome = new String(charRevArray);

        System.out.println("original strinr = "+str);
        System.out.println("Reverse = "+charRevArray);
    }
}

Tuesday, 9 June 2020

Program to read string in C language | gets()


Program to read string in C language:

#include<stdio.h>
void main(){
   char course[20];
   printf("enter your course name \n");
   gets(course);
   printf("your course name is %s",course);
}

output 1:
            enter your course name
            C
            your course name is C
output 2:
            enter your course name
            C language
            your course name is C language
output 3:
            enter your course name
            C++
            your course name is C++


we use gets() function to read a string in c language. 
here we declared a character array by name course ( of size 20) to store the string.

Monday, 8 June 2020

Program to add two matrices using c language

Program to add two matrices using c language.

In this we took two matrices by name matrix1 and matrix2 which hold the values of two matrices. And we took another  matrix by name res_matrix to store the result after addition of matrix1 and matrix2. r1,c1 represents the size of first matrix and r2,c2 represents the size of second matrix.

If the size of two matrices are same ( i.e; r1 , r2 must be same and c1,c2 must be same ) then only we can add two matrices otherwise not possible to add.so first read sizes of two matrix and then check if both the sizes of matrix are equal then read the values of two matrices and find sum. 

 #include <stdio.h>
void main(){
int matrix1[10][10], matrix2[10][10], res_matrix[10][10], i, j, r1, r2, c1, c2;

// reading the size of first matrix
printf(" enter the size of first matrix <10");
scanf("%d%d", &r1, &c1) ;

// reading the size of second matrix
printf(" enter the size of second matrix <10");
scanf("%d%d", &r2, &c2) ;

//checking the sizes of two matrices for addition
if( r1==r2 && c1==c2){
 
    // reading the elements of first matrix
    printf(" enter elements of first matrix ");
    for( i=0; i<r1 ;i++){
        for(j=0;j<c1;j++){
            scanf( "%d", &matrix1[i][j]);
        }
    }

    // reading the elements of second matrix
    printf(" enter the elements of second matrix ");
    for( i=0; i<r2; i++){
        for(j=0; j<c2; j++){
            scanf( "%d", &matrix2[i][j]);
        }
    }

    // adding two  matrices`
    for( i=0; i<r2; i++){
        for (j=0; j<c2; j++){
            res_matrix[i][j]= matrix1[i][j] + matrix2[i][j];
        }
    }

    // printing the resultant matrix  
    printf(" the resultant matrix is  \n");
    for (i=0; i<r1; i++){
        for( j=0; j<c1; j++){
            printf(" %d ",  res_matrix[i][j]);
        }
        printf(" \n ");
    }
}
else printf(" it is not possible to add two matrix as sizes are different ");
}







Saturday, 6 June 2020

Program to read values from command line and find max in java


Program to read values from command line argument and find maximum in java.

In the below java program  we read some values from command line argument and the maximum among them.
class Demomaximum {   
   public static void main (String [] args)       {    
       int max=0;
                  
                   int a[]=new int [args.length];
                   for(int i=0;   i < a.length;   i++){
                         a[i] = Integer.parseInt(args[i]);
                   }
                  
                        //  finding maximum value
                   max=a[0];
                   for(int i=0  ;  i  <a.length; i++){
                                                if(a[i]>max){
                                                    max=a[i];
                                                }
                   }
                   System.out.println("max = "+max);
                }
                }

compile:
C:\>  javac Demomaximum.java
Run:
C:\>  java    Demomaximum    10  15   5   25   15   40   12  30
  Max= 40