Matrix multiplication in c program with explanation

  • Matrix multiplication in c.
  • Take three two dimensional arrays 
  • int first_matrix[10][10], second_matrix[10][10], multiply_result[10][10];
  • Two for take input from user for matrices one for capture multiplication of two matrices result.
  • Ask user to enter number of columns of rows of first matrix.  m ,n
  • Read from keyboard using scanf() function in java. and store all the elements (rows*columns) in first matrix using two for loops.
  • Ask user to enter number of columns of rows of second matrix. p , q
  • Read from keyboard using scanf() function in java. and store all the elements (rows*columns) in first matrix using two for loops.
  • Now take two more loops for rows of first matrix and columns of second matrix.
  • Third for loop for multiplication iteratek
  • for (c = 0; c < m; c++) {
  •       for (d = 0; d < q; d++) {
  •         for (k = 0; k < p; k++) {
  •           sum = sum + first_matrix[c][k]*second_matrix[k][d];
  •         }
  •  
  •         multiply_result[c][d] = sum;
  •         sum = 0;
  •       }
  •     } 
  • c program for multiplication of two matrices using arrays


Program #1: Write a c program to multiply two matrices. c program for matrix multiplication using arrays
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.   int m, n, p, q, c, d, k, sum = 0;
  7.   int first_matrix[10][10], second_matrix[10][10], multiply_result[10][10];
  8.  
  9.   printf("Enter the number of rows and columns of first matrix\n");
  10.   scanf("%d%d", &m, &n);
  11.   printf("Enter the elements of first matrix\n");
  12.    for (c = 0; c < m; c++)
  13.    for (d = 0; d < n; d++)
  14.       scanf("%d", &first_matrix[c][d]);
  15.  
  16.   printf("Enter the number of rows and columns of second matrix\n");
  17.   scanf("%d%d", &p, &q);
  18.  
  19.   if (n != p)
  20.     printf("Matrices with entered orders can't be multiplied with each other.\n");
  21.   else
  22.   {
  23.     printf("Enter the elements of second matrix\n");
  24.  
  25.     for (c = 0; c < p; c++)
  26.       for (d = 0; d < q; d++)
  27.         scanf("%d", &second_matrix[c][d]);
  28.  
  29.     for (c = 0; c < m; c++) {
  30.       for (d = 0; d < q; d++) {
  31.         for (k = 0; k < p; k++) {
  32.           sum = sum + first_matrix[c][k]*second_matrix[k][d];
  33.         }
  34.  
  35.         multiply_result[c][d] = sum;
  36.         sum = 0;
  37.       }
  38.     }
  39.  
  40.     printf("Product of entered matrices:-\n");
  41.  
  42.     for (c = 0; c < m; c++) {
  43.       for (d = 0; d < q; d++)
  44.         printf("%d\t", multiply_result[c][d]);
  45.  
  46.       printf("\n");
  47.     }
  48.   }
  49.  
  50.  getch();
  51. }

Output:


multiply matrices c program

Write a c program to generate fibonacci series using recursion

  • Fibonacci series starts from 0 ,1 and the next number should be sum of previous two numbers.
  • 0+1=1, so the next number in Fibonacci series in c program is 1. 0 1 1 
  • 0 1 1 2 3 5 8.. and so on.
  • We have already seen this c program without using recursion.
  • Now we will see how to generate fibonacci series by using recursion.
  • A function called by itself : recursion. 
  • Fibonacci series c programming using function


Program #1 : Write a C program to print / generate fibonacci series up to n numbers.  Take input from user. By using recursion


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // C program to generate  fibonacci series by using recursion 
  5. //www.instanceofjava.com
  6.  
  7. //Function declaration
  8. long long Printfibonacci(int num);
  9.  
  10.  
  11. int main()
  12. {
  13.     int num;
  14.     long long fibonacci;
  15.      
  16.     //Reads number from user to find nth fibonacci term
  17.     printf("Enter any number to generaye fibonacci series up to ");
  18.     scanf("%d", &num);
  19.      
  20.     printf("%d %d ",0,1);  
  21.     Printfibonacci(num-2);//n-2 numbers. i.e 1 and 2 are printed already
  22.      
  23.     getch();
  24. }
  25.  
  26.  
  27. /**
  28.  
  29.  * Recursive function  
  30. */
  31. long long Printfibonacci(int num) 
  32. {
  33.     static int n1=0,n2=1,n3;  
  34.     if(num>0){  
  35.          n3 = n1 + n2;  
  36.          n1 = n2;  
  37.          n2 = n3;  
  38.          printf("%d ",n3);  
  39.          Printfibonacci(num-1); //Recursively calls Printfibonacci()
  40.     }   
  41. }

Output:


fibonacci recursive c prorgam

Write a c program to generate fibonacci series without recursion

  • Fibonacci series starts from 0 ,1 and the next number should be sum of previous two numbers.
  • 0+1=1, so the next number in Fibonacci series in c program is 1. 0 1 1 
  • 0 1 1 2 3 5 8.. and so on.
  • Now let us see and c language example program on fibonacci series.


Fibonacci series in C language: 

  • First print 0, 1 by default.
  • and start from 2 and iterate upto n-1 using for loop.
  • inside loop assign n3=n1+n2;  
  • and ptint sum. printf(" %d",n3);  
  • Assign n2 value to n1. n1=n2
  • Assign sum to n2:  variable n2=n3;  


Program #1 : write a C program to print / generate fibonacci series up to n numbers.  Take input from user without recursion

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()  
  5. {   
  6. int n1=0,n2=1,n3,i,number;   
  7. printf("Enter the number of elements to be printed in Fibonacci series:");   
  8. scanf("%d",&number);   
  9. printf("\n%d %d",n1,n2);//printing 0 and 1  
  10.    
  11. for(i=2;i<number;++i)//Starts from 2 to given number-1
  12. {  
  13.   n3=n1+n2;  
  14.   printf(" %d",n3);  
  15.   n1=n2;  
  16.   n2=n3;   
  17. }  
  18. getch();  
  
Output:

fibonacci series in c

Profit and loss questions and answers java

  • Just focusing on quantitative aptitude questions to help freshers who are facing these questions in interviews and written test.
  • No need to allocate different time slot to prepare quantitative aptitude questions. We will help you to cover those formulas and provide problems with solutions as a java programs.
  • Hope it will help you.
Profit and loss:

  • Cost Price: The price, at which an product is purchased (CP).
  • Marked price: actual price or label price which shown on product.(MRP)
  • Selling price: The product sold price.(SP)
  • Profit: If  selling_price is greater than cost_price then its a profit.(selling_price-cost_price)
  • Loss: if  selling_price is less than cost_price then its a Loss.(cost_price-selling_price)
  • Discount:  marked_price-selling_price.
  • discount_percentage=((marked_price-selling_price)/marked_price)*100;
  • profit=selling_price-cost_price;
  • if(selling_price>cost_price)
  • profit_percentage=((selling_price-cost_price)/cost_price)*100;     
  • if(selling_price<cost_price)
  • loss_percentage=((cost_price-selling_price)/cost_price)*100;

 Problem 1#: If a product is bought for Rs.100. and sold for Rs.150 what is the profit percentage

  1. package com.javaQuantitaveQuestions;
  2.  
  3. /**
  4. * Quantitative aptitude questions
  5. * @author www.instanceofjava.com*/
  6. public class ProfitNLoss {
  7.  
  8. public static void main(String[] args) {
  9.  
  10.      double profit,profit_percentage=0;
  11.         
  12.         
  13.         double cost_price=100;
  14.         
  15.         double selling_price=150;
  16.         
  17.         if(selling_price>cost_price){
  18.         profit_percentage=((selling_price-cost_price)/cost_price)*100;
  19.         profit=selling_price-cost_price;
  20.         
  21.         System.out.println("Profit:="+profit+"\n profit Percentage:="+profit_percentage+"%");
  22. }
  23.  
  24. }

Output:


  1. Profit:=50.0
  2. profit Percentage:=50.0%
Problem 2#: If a product is bought for Rs.100. and sold for Rs.70 what is the loss and loss percentage

  1. package com.javaQuantitaveQuestions;
  2. /**
  3. * Quantitative aptitude questions
  4. * @author www.instanceofjava.com
  5. */
  6. public class ProfitNLoss {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.      double loss=0,loss_percentage=0;
  11.         
  12.         double cost_price=100;
  13.         
  14.         double selling_price=70;
  15.         
  16.         
  17.         if(selling_price<cost_price){
  18.             
  19.         loss=cost_price-selling_price;
  20.         loss_percentage=((cost_price-selling_price)/cost_price)*100;
  21.         System.out.println("Loss:="+loss+"\nLoss Percentage:="+loss_percentage+"%");
  22.         
  23.         }
  24.     }
  25.  
  26. }

Output:


  1. Profit:=50.0
  2. profit Percentage:=50.0%


Problem 3#: If a product is bought for Rs.100. and sold for Rs.70 what is the Discount and Discount percentage

  1. package com.javaQuantitaveQuestions;
  2.  
  3. /**
  4. * Quantitative aptitude questions
  5. * @author www.instanceofjava.com
  6. */
  7. public class ProfitNLoss {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.      double discount=0,discount_percentage=0;
  12.         
  13.         double marked_price=100;
  14.         
  15.         double selling_price=70;
  16.         
  17.         
  18.         discount=(marked_price-selling_price);
  19.         
  20.  discount_percentage=(discount/marked_price)*100;
  21. System.out.println("Discount:="+discount+"\ndiscount
  22. Percentage:="+Discount_percentage+"%");
  23.         
  24.     }
  25.  
  26. }

Output:


  1. Discount:=30.0
  2. Discount Percentage:=30.0%

How to subtract X days from a date using Java calendar and with java 8

  • To subtract "X" days from a date in java of calendar class object.
  • calendar.add(Calendar.DAY_OF_MONTH, -X);
  • add method of calendar has a option to subtract days from given date.
  • Lets see and example program on how to subtract days from a date in java 



 Program #1: Java Example program to subtract 2 days from current date using calendar class.

  1. package com.javadate;
  2.  
  3. import java.util.Calendar;
  4. /**
  5. * how to subtract date in java
  6. * @author www.instanceofjava.com
  7. */
  8. public class SubtractDateJava {
  9.  
  10.     public static void main(String[] args) {
  11.         
  12.         Calendar calendar = Calendar.getInstance(); // this would default to now
  13.         
  14.         System.out.println(calendar.getTime());
  15.         //subtract  two days from todays date.
  16.         calendar.add(Calendar.DAY_OF_MONTH, -2);
  17.         
  18.         System.out.println(calendar.getTime());
  19.         
  20.     }
  21.  
  22. }

Output:

  1. Sun May 07 22:17:22 IST 2017
  2. Fri May 05 22:17:22 IST 2017

Java 8 subtract dates

  • We can subtract days from given date in java without using calendar class also.
  • Now in Java 8 we have LocalDateTime class. and it provides a method to subtract days from given date.
  • LocalDateTime.now().minusDays(30);

Program #2: Java Example program to subtract 2 days from current date without using calendar class. (Use Java 8)


  1. package com.javadate;
  2.  
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneOffset;
  5. import java.util.Date;
  6. /**
  7. * how to subtract date in java using example program
  8. * @author www.instanceofjava.com
  9. */
  10. public class SubtractDateJava {
  11.  
  12.     public static void main(String[] args) {
  13.         
  14.         
  15.         System.out.println(LocalDateTime.now());
  16.         LocalDateTime dateBefore20Days = LocalDateTime.now().minusDays(20);
  17.         
  18.         System.out.println(dateBefore20Days);
  19.         
  20.         Date date = Date.from(dateBefore20Days.toInstant(ZoneOffset.UTC));
  21.         System.out.println(date);
  22.     }
  23.  
  24. }

Output:

  1. 2017-05-07T22:30:22.308
  2. 2017-04-17T22:30:22.308
  3. Tue Apr 18 04:00:22 IST 2017

Program #3: Java Example program to subtract 2 days from current date without using calendar class. (Use Java 8) Using Eclipse IDE.
 
subtract days from date java

Top 100 java interview questions

  • Now its time to practice some java programs.
  • Here we provided some java interview programs and basic programs with solutions to practice.
  • Java programming exercises for beginners and experienced programmers.
  • We have also provided some java multiple choice questions and answers.
  • Lets see what are the top 100 java interview questions we have listed




java programs to practice

 1.Restricting a class from creating more than three objects.
  •  Restring a class from creating multiple objects or restricting class from creating not more than three objects this type of interview questions will come for experience interview or written tests.

 2.Print message without using System.out.println() method.


3.What happens when we try to print null?


4.Constructor Chaining in java

  • Constructor chaining in java with example programs to practice for freshers [Solution]

5.Fibonacci series with recursion


6. Print pascals triangle.

  •  Java example program to print pascals triangle [Solution]

7.Get top two maximum numbers in an array
  • How to get top two maximum numbers in java [Solution]


8.Merge sort algorithm in java
  • Merge sort algorithm with an example program to practice [Solution]

9.Java Interview Programming questions on this keyword.
  • Here we have provided some java programs and you need to find out the output of the program.
  • Practice and get the output check the programs on this keyword [Solution]

10. Abstract class interview Programming questions


11.Multiple choice interview programs and questions to practice.

  • Multiple choice java interview programs on this keyword [Check here]
  • Core java multiple choice questions with answers on method overloading [Check here]
  • Multiple choice programs to practice on static keyword [Check here]
  • Constructors java interview programs for freshers [Check here]
  • Mentioned questions are top java interview questions and programs

13. Oops concepts Programming questions to practice : Java beginner practice problems

  • Constructor in interface ? [Solution]
  • Can we create private constructor in java ? [Solution]
  • Super keyword in java inheritance ? [Solution]
  • Can we override static methods in java? [Solution] 
  • Can we overload static methods in java? [Solution]  
  • Can we call sub class methods using super class object ? [Solution]

14.Java programming exercises with solutions on java Strings

15.java programming practice questions and answers on collections


16. Exception handling interview questions and programs.

Select Menu