Check Armstrong number in c using recursive function

  • Armstrong number is a number which order of n if
  • xyz=x n + y n + z n;
  • To check three digit Armstrong number in c programming
  • 153=13 * 53*33
  • In the same way we can also check four digit Armstrong number
  • 1634=14+64+34+44 is Armstrong  number.
  • Now let us check a four digit number is armstrong or not by using recursive function in c.
  • Armstrong number in c using recursion function.
  • Four digit armstrong number in c. check below program for four digit or n digit armstrong number in c programming.



Program #1: Write a c program to check four / 4 digit number or N digit number is armstrong number or not by using recursive function

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int powerfun(int, int);
  5.  
  6. int main()
  7. {
  8.    int n, sum = 0, temp, remainder, digits = 0;
  9.  
  10.    printf("Input number to check Armstrong or not");
  11.    scanf("%d", &n);
  12.  
  13.    temp = n;
  14.    // check the number of digits in given number
  15.    while (temp != 0) {
  16.       digits++;
  17.       temp = temp/10;
  18.    }
  19.  
  20.    temp = n;
  21.  
  22.    while (temp != 0) {
  23.       remainder = temp%10;
  24.       sum = sum + powerfun(remainder, digits);
  25.       temp = temp/10;
  26.    }
  27.  
  28.    if (n == sum)
  29.       printf("%d is an Armstrong number.\n", n);
  30.    else
  31.       printf("%d is not an Armstrong number.\n", n);
  32.  
  33.    getch();
  34. }
  35.  
  36. int powerfun(int num, int r) {
  37.    int c, res = 1;
  38.  
  39.    for (c = 1; c <= r; c++) 
  40.       res = res*num;
  41.  
  42.    return res;   
  43. }

Output:


armstrong number recusrion function c

C program to check number is armstrong number or not

  • Armstrong number is a number which order of n if
  • xyz=x n + y n + z n;
  • To check three digit Armstrong number in c programming
  • 153=13 * 53*33
  • In the same way we can also check four digit Armstrong number
  • 1634=14+64+34+44 is Armstrong  number.
  • In c Programming to check a number is armstrong or not we need to split that number and need to find nth power of each number and sum all the results.
  • If total sum is equal to the same number then we can say it is Armstrong number.
  • Let us see an Example program in c to check a number is Armstrong number or not without using recursion.
  • First we will check a three digit number is armstrong number or not 

Program #1 : write a c program to check a three digit number is armstrong number or not without using recursion.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main(){
  4. int number, originalNumber, rem, result = 0;
  5.  
  6.     printf("Enter a three digit number to check armstrong number or not: ");
  7.     scanf("%d", &number);
  8.  
  9.     originalNumber = number;
  10.  
  11.     while (originalNumber != 0)
  12.     {
  13.         rem = originalNumber%10;
  14.         result += rem*rem*rem;
  15.         originalNumber /= 10;
  16.     }
  17.  
  18.     if(result == number)
  19.         printf("%d is an Armstrong number.",number);
  20.     else
  21.         printf("%d is not an Armstrong number.",number);
  22.  
  23.     getch();
  24.     
  25. }

 Output:


Armstrong number in c program

Factorial program in c without recursion

  • Factorial of n= n*n-1*.....1
  • To find factorial of a number in c programming language we need to use for loop and iterate from n to 1 
  • in side loop we need to write a logic to multiply the result.
  •  factorial *= i;    
  • Finally in  factorial  we will have the result of  1 *2 *.....n
  • Let us see an example c program on finding factorial of a number without using recursion. 



 Program #1: Write a c program to print factorial of a number without using recursion.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n, i;
  7.     unsigned long long factorial = 1;
  8.  
  9.     printf("Enter a number to find factorial: ");
  10.     scanf("%d",&n);
  11.  
  12.     // show error if the user enters a negative integer
  13.     if (n < 0)
  14.         printf("Error! Please enter any positive integer number");
  15.  
  16.     else
  17.     {
  18.         for(i=1; i<=n; ++i)
  19.         {
  20.             factorial *= i;              // factorial = factorial*i;
  21.         }
  22.         printf("Factorial of Number %d = %llu", n, factorial);
  23.     }
  24.  
  25.     getch();
  26. }

Output:

factorial number in c program without recursion

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%

Select Menu