Adsense 728 * 15

Search

Custom Search

C Program to Print Fibonacci Series using While, Do While & For Loop

Write a C program to print fibonacci series. In this tutorial, We'll write a c program to print fibonacci series using while, do while and for loop.

C Program to Print Fibonacci Series using While, Do While & For Loop

What is Fibonacci series?


In Fibonacci series, The first two numbers are 0 and 1, and each subsequent numbers are the sum of previous two numbers. For example- 0  1  1  2  3  5  8  13 ............


So first two numbers are 0 and 1, next number 1 is the sum of 0 + 1. Similarly, 2 is the sum of 1 + 1 and so on.


Interview Questions For Practice

C Program to Print Fibonacci Series using Recursion


C Program to Print Fibonacci Series using For Loop


#include <stdio.h>

int main()
{
    int i, n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);


    for(i = 2; i < n; i++){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
  }

 return 0;
}


Output :

How many elements you want to print in a series  6

Fibonacci Series is

0   1   1   2   3   5

C Program to print factorial of a number

C Program to reverse a number

C Program to Print Fibonacci Series using While Loop

We have written a code to print Fibonacci series using for loop. In this example, We are going to write a C program to print Fibonacci series using while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //While loop
    while (i < n){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      
      i++;
  }

  return 0;
}

C Program to Print Fibonacci Series using Do While Loop


In this example, we write to code to print Fibonacci series using do while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //Do while loop logic

    do {
    
    /* Each subsequent number is the sum  
        of previous two numbers
     */
      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      i++;
       
    } while (i < n);

 return 0;
}

In this tutorial, I have explained how to print Fibonacci series using multiple code examples. If you have any doubt you can ask them through your comments.

C++ Program to Print Factorial of a Number

Write a C++ Program to print factorial of a number. How to print factorial of a number using C++ code.

Given an input number, we have to write a code to calculate a factorial of a number.

C++ Program to Print Factorial of a Number


What is Factorial?


Factorial of a non-negative number n, is the product of all integers less than or equal to n.

For example - Factorial of 5 is 120.

5! = 5 * 4 * 3 * 2 * 1 = 120

C Program to Print Factorial of a Number

Java Program to Print Factorial of a Number


C++ Program to Print Factorial of a Number


#include<iostream.h>

int main() {

  int num, fact=1;  

  cout << "Enter a number: "; 
  cin  >> num;

  //If number is greater than zero

  if(num > 0) {

    for(int i=1; i <= num; i++){
       fact = fact*i;                  
    }

   cout << "Factorial of a number is: "<< fact;
 
  } else if (num == 0 ){

    cout << "Factorial of 0 is 1";

  } else {

   cout << " Factorial of a negative number doesn't exist";

  } 

  return 0; 

}

C++ Program to Print Factorial of a Number using Function


#include <iostream>

using namespace std;

int factorial(int num) {

     int fact = 1;
 
     //Factorial of a zero is zero
     if (num == 0) {
       return 1;
     } 
 
     for (int i = 1; i <= num; i++) {
       fact = fact*i;
     }
 
    return fact;
}

int main(void) {

    int num;
 
    cout << "Enter a number \n";
    cin  >> num;
 
    if (num >=  0) {
       //Function is called factorial(num)
       cout << "Factorial of a number is " << factorial(num);
  
    } else {
      cout << "Factorial of a negative number doesn't exist";
    }
  
    return 0;
}


C++ Program to Print Factorial of a Number Video Tutorial




Print factorial of a number using recursion


C Program to Print Factorial of a Number


Write a C  program to Print  factorial of a number. In this program, We are going to write a C program which takes an input number and print it's factorial. 

To solve this problem, we are using iterative approach. If you are not familiar with iterative and recursive approach then you can check this awesome tutorial on recursion vs iteration.

Difference between iteration and recursion

Java program to print factorial of a number

C Program to Print Factorial of a Number



What is Factorial?

For any input number n, it's factorial is

factorial = 1 * 2 * 3 * 4 .......n;

Suppose, An input number is 4 then it's factorial is 4 * 3 * 2* 1 = 24

C Program to Print Factorial of a Number using Loop


In this program, we take a input number and using for loop our program calculate and print the factorial of a number.

#include<stdio.h>

int main() 
{

   int number, fact=1, i;  

   printf("Enter a number \n "); 
   scanf("%d",&number);
 
   //If a number is greater than zero
   if(number > 0) {

      for(i = 1; i <= number; i++){
         fact = fact*i;                  
      }

      printf("The factorial of a number is: %d",fact);
 
    } else if (number == 0 ){

        printf("Factorial of 0 is 1");

    } else {

       printf("Factorial of negative number doesn't exist";
 
   }

   return 0;
 }

C Program to Print Factorial of a Number using Function

#include <stdio.h>

  //This method calculate and return factorial
  int factorial(int num) {

     int fact = 1;
 
     //Factorial of a zero is zero
      if (num == 0) {
        return 1;
     } 
 
     for (int i = 1; i <= num; i++) {
        fact = fact*i;
     }
 
     return fact;
  }

  int main(void) {

     int num;
 
     printf ("Enter a number \n");
     scanf  ("%d", &num);
 
     if (num >=  0) {
        //Function is called factorial(num)
        printf ("Factorial of a number is %d", factorial(num));
  
     } else {
        printf ("Factorial of a negative number doesn't exist");
     }
  
     return 0;
  }



Output -

Enter a number 5

Factorial of a number is 120

Print factorial of a number using recursion



C Program to Reverse a Number

Write a C program to reverse a number. In this program, A number is input by a user and our code will reverse this number. For reversing a number, we'll use an iterative approach.

C Program to Reverse a Number


Program to Reverse a number using Recursion


How to Reverse a Number in C



Let's check the logic.

1. Take a input from user.

2.
 while(num!=0) {

          temp = num%10;   // Store the result in temp variable

          rev = (rev*10)+temp;

          num = num/10; 
        
      }
 

Explanation -  


 Suppose, An input number is 243. 

 While loop condition is true, as num is 243 

 rev = 0 * 10 + 3; 
 num = 24; 

 Now num is 24 still while loop condition is true, 

 rev = 3*10 + 4; 
 num = 2; 

 Now num is 2, again while loop is executed, 

 rev = 34 * 10 + 2 
 num = 0; 

 Now number is zero while loop will break


Program to Reverse a String



C Program to Reverse a Number


#include <stdio.h>

int main(void) {
 
    int num, temp, rev=0;

    printf("Enter a number");
    scanf("%d", &num);

 
    while(num != 0){

       temp = num % 10;
       rev  = (rev*10)+temp;
       num  = num/10;

    }

    printf("Reverse of a input number is  %d", rev);


    return 0;
}



Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books



C++ Program to Reverse a Number

Write a C++ program to reverse a number. In this tutorial, we are going to write a program which takes an input number from a user and reverse this  number.

C++ Program to Reverse a Number


C program to reverse a number

C++ Program to Reverse a Number


 #include<iostream>
  using namespace std;

  int main()
  {
     int num, temp, rev=0;

     cout << "Enter a number \n";
     cin  >> num;

 
     while(num != 0){

       temp = num%10;
       rev  = (rev*10) + temp;
       num  = num/10;

     }

     cout<<"\n Reverse of a input number is "<< rev;

     return 0;

 }




Output -

Enter a number   243

Reverse of a input number is   342


Explanation -  


 We have input a number 243. 

STEP 1 -  While loop condition is true, as num is 243. Initially the value of rev is equal to 0.

 rev = 0 * 10 + 3; 
 num = 24; 

 STEP 2 -Now num is 24 and rev is 3. In next iteration while loop condition is true, 

 rev = 3*10 + 4; 
 num = 2; 

 STEP 3- Now num is 2 and rev = 34. So again, while loop condition is true, 

 rev = 34 * 10 + 2 
 num = 0; 

 Now number is zero while loop will break the value of rev is 342.


C++, C Program to Check whether a Number is Prime or Not

Write a C, C++ program to check whether a number is prime or not. In this program, we take a number as input from a user and your program prints whether an input number is prime or not.

What is a Prime Number?


A prime number is a number which is greater than 1 , and divisible by 1 and itself

For example -  7 is a prime number it is divisible by 1 and 7 only.

NOTE -  2 is the only even prime number. 


There are various methods to check whether a number is prime or not. You can check this tutorial for explanation. 

Program to check whether a number is prime or not

Algorithm to  check whether number is prime or not

We run a loop from 2 to num/2 and check whether the number is divisible. If the number is not divisible by any number then it's a prime number.


Program to Print all prime numbers between 1 to 100.

Print even numbers from 1 to 100.

Sorting algorithms and their time complexity.

C++ Program to Check whether a Number is Prime or Not


#include<iostream.h>
using namespace std;

int main(){

    int num, i, flag=0;
    

    cout << "Enter a number\n";
    cin  >> num;

    /* Check whether number is divisible 
     by number other than 1 and itself 
    */
    for(i = 2; i <= num/2; i++){

        if(num % i == 0){
            
            val = 1;
            break;

        }
    }

   if(flag == 0)
        cout << "An input number is a prime number";
   else
      cout<< " An input number is not a prime number";

   return 0;
}


C Program to Check whether a Number is Prime or Not


#include <stdio.h>

int main(void) {
 
 int num,i,val=0;
    
    printf ("Enter a number\n");
    scanf ("%d" , &num);

    /* Check whether number is divisible 
       by number other than 1 and itself 
    */
    for(i = 2; i <= num/2; i++){

        if(num % i == 0){
            
            val=1;
            break;

        }
    }

   if(val==0)
        printf (" An input number is a prime number");
   else
      printf (" An input number is not a prime number");
      
 return 0;
}


Output :

Enter a number :  5

An input number is a prime number


Enter a number : 6

An input number is not a prime number

C, C++ Program to Swap Two Numbers without using temporary Variable

Write a C, C++ program to swap two numbers without using temporary variable. In this tutorial, You are going to learn how to write a code to swap two numbers without using temporary variable. 

In my previous posts, i have wrote a program to 

Swap two numbers using third variable

Swap two numbers using call by reference method


Logic of Swapping two Numbers without using temporary Variable


i)  Let's assume two variables a and b with a value of 7 and 5;
     a = 7 and b = 5

ii)   Let's swap the numbers. 

       a) a =  a + b ;   // a = 12 ( 7 + 5)

       b)  Now the value of a variable is 12.
        
            b = a - b;  // b = 7 (12 - 5) 

        c) a = a - b;  // a = 5  (12 - 7) 



 After swapping the value of a and b variable is 5 and 7.

C, C++ Interview Questions

C Program to Swap two Numbers without using temporary Variable



#include<stdio.h>

int main()
{
   int a, b; 
   
   printf("Enter two numbers a and b ");
   scanf("%d %d",&a,&b);
   
  // Logic of swapping

   a = a + b;
   b = a - b;
   a = a - b;

   printf(" After swapping, a = %d and b = %d",a, b);

   return 0;
}



Output:

Enter two numbers a and b :  7  5

After swapping a = 5 and b = 7


C++ Program to Swap Two Numbers without using Temporary Variable

#include <iostream>
using namespace std;

int main() {
 
   int a, b; 
   
   /* Taking user input. */

   cout << "Enter two numbers a and b ";
   cin  >> a >> b;
   
   /* Swapping logic */

   a = a + b;
   a = a - b;
   a = a - b;

   cout<<"After swapping a and b is "<<" "<< a <<" "<< b;

    return 0;
}




C Program to Implement a Stack Using an Array

Write a C program to implement a stack using an array. In this tutorial, We'll implement a stack data structure using an array.

What is a Stack Data Structure?

Stack is a data structure in which insertion and deletion can be done from one end only. That's why it is also called LIFO(Last In First Out).

In stack, Insertion and deletion operation is known as push (Insertion) and pop (Deletion). While inserting and deleting an element in a stack we need to check some conditions. For insertion, we need to check whether a memory is available, if memory is not available then it is known as stack overflow. Similarly, while deleting (pop operation) an element, if no element is present in a stack, then it is known as stack underflow.

Reverse a String using Stack

C Program to Implement a Stack using an Array


#include <stdio.h>

#define MAX 50
int top=-1;

void push(int stackop[],int item){
     
    if(top==MAX-1){
        printf("Stack Overflow");
        return;
    }else{
        // Insert the element in a stack
        stackop[++top]=item;
        printf("Value inserted in stack is %d\n",item);
    }
}

void pop(int stackop[]){

    int val;

    if(top==-1){
        printf("Stack underflow");
        return;
    }else{
        val = stackop[top];
        --top;
    }

    printf( "Value deleted from stack is %d\n",val);
}

main()
{
    int stackop[MAX];

    // Inserting value in stack

    push(stackop,5);
    push(stackop,7);
    push(stackop,2);
    // Deleting value from stack

    pop(stackop);
    pop(stackop);

} 

C Program to Implement a Stack using an Array

C, C++ Program to Check whether an Input Number is a Perfect Square or not

Write a C, C++ program to check whether an input number is a perfect square or not. In this program, We will not use an in-built function such as sqrt() to solve this question.



C, C++ programming questions for practice

Find cube of a number

Perfect Square

A number is a perfect square if it is the square of an integer.

For example - 25 is a perfect square. You can write it as 5 * 5 which is the square of an integer. But 17 is not a perfect square.


C Program to Check an Input Number is Perfect Square or not


#include <stdio.h>

int main(void) {

   int num, flag = 0;
 
   printf ("Enter a number \n");
   scanf ("%d", &num);
 
   for (int i = 1; i <= num; i++) {
  
                 
      if ( i * i == num) {
   
         flag = 1;
         break;
      }
  
      if ( i * i > num) {
   
        break;
   
      } 
   }
 
   if (flag) {
  
      printf (" %d is a perfect square", num);
  
    } else {
  
      printf (" %d is not a perfect square", num);
    }

   return 0;
}



Output :

Enter a number :  4

4 is a perfect square


Enter a number : 7

7 is not a perfect square

C++ Program to Check wether an Input Number is a Perfect Square or not


#include <iostream>
using namespace std;

int main() {
 
   int num, flag = 0;
 
   cout << "Enter a number \n";
   cin  >> num;
 
   for (int i = 1; i <= num; i++) {
  
     /* If it is equal to an input number
      * then it's a perfect square
      */
     if ( i * i == num) {
   
         flag = 1;
         break;
     }
  
     /**
      * If it's greater than input number 
      * so no need to check further
      * break the loop
      */ 
     if ( i * i > num) {
   
        break;
   
      } 
   }
 
   if (flag) {
  
     cout << num << " is a perfect square";
  
   } else {
  
     cout << num << " is not a perfect square";
 }
 
 return 0;
}



C, C++ Program to Print Square of a Number

Write a C, C++ program to print square of a number. We have to write a code in which our program will take an input number and print it's square.

Similar programming questions for practice.

Calculate cube of a number

Find sum of first n odd numbers

Program to check perfect square of a number

C Program to Print Square of a Number 

#include<stdio.h>


int main() {


   int num, sqr;
   
   printf("Enter a number \n");
   scanf("%d", &num);
 
   // calculate square of a number

   sqr = num * num;
 
   printf("Square of an input number is %d", sqr);
 
   return 0;
}


C++ Program to Print Square of a Number


 

#include <iostream.h>

using namespace std;

 int main() {
 
   int num,sqr;
   
   cout << "Enter a number \n";
   cin  >> num;
   
   // Calculate square of a number

   sqr = num * num;

   cout << " Square of a number is " << sqr;

  return 0;
}

Output:

Enter number  :  6

Square of a number is :  36