• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games

Program to Print Fibonacci Series in C++

Leave a Comment

What is Fibonacci Series?

In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

1,;1,;2,;3,;5,;8,;13,;21,;34,;55,;89,;144,; ldots;

or (often, in modern usage):

0,;1,;1,;2,;3,;5,;8,;13,;21,;34,;55,;89,;144,; ldots; (sequence A000045 in OEIS).
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_n = F_{n-1} + F_{n-2},!,

with seed values

F_1 = 1,; F_2 = 1

or

F_0 = 0,; F_1 = 1.

Program to Print Fibonacci Series in C++

PROGRAM:

#include<iostream>
using namespace std;
main()
{
   int n, c, first = 0, second = 1, next;
   cout << "Enter the number of terms of Fibonacci series you want" << endl;
   cin >> n;
   cout << "First " << n << " terms of Fibonacci series are :- " << endl;
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout << next << endl;
   }
   return 0;
}

 

OUTPUT:

Filed Under: cpp codes

Write a C++ program to Make Calculator

Leave a Comment

Here is the Simple C++ Program to make Calculator

PROGRAM:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter operand1 op operand2
    int  nOperand1;
    int  nOperand2;
    char cOperator;
    cout << "Enter 'value1 op value2'n"
         << "where op is +, -, *, / or %:" << endl;
    cin >> nOperand1 >> cOperator >> nOperand2;
    // echo what the operator entered
    cout << nOperand1 << " "
         << cOperator << " "
         << nOperand2 << " = ";
    // now calculate the result; remember that the
    // user might enter something unexpected
    switch (cOperator)
    {
        case '+':
            cout << nOperand1 + nOperand2;
            break;
        case '-':
            cout << nOperand1 - nOperand2;
            break;
        case '*':
        case 'x':
        case 'X':
            cout << nOperand1 * nOperand2;
            break;
        case '/':
            cout << nOperand1 / nOperand2;
            break;
        case '%':
            cout << nOperand1 % nOperand2;
            break;
        default:
            // didn't understand the operator
            cout << " is not understood";
    }
    cout << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}

 

OUTPUT:

Filed Under: cpp codes

C++ program to convert decimal to binary

1 Comment

Hello all, today we are going to write a C++ program to convert decimal to binary number, let’s first discuss how we can convert a decimal number to binary number and later we will write the program for the same.

How Program works?

Lets take an example of 11.

Now divide it by 2 and note down the remainder.

  • So. 11 / 2 , Remainder = 1.
  • Quotient: 5 / 2 , Remainder = 1.
  • Quotient: 2/2 , Remainder = 0.
  • Quotient: 1/2, Remainder = 1.
  • Now Quotient = 0. Stop.

Binary number is the Remainders in reverse order i.e. Binary of 11 = 1011

C++ Program to convert Decimal to Binary:

#include<iostream>
using namespace std;
void binary(int num)
{
    int rem;
    if (num <= 1)
    {
        cout << num;
        return;
    }
    rem = num % 2;
    binary(num / 2);
    cout << rem;
}
int main()
{
    int dec, bin;
    cout << "Enter the number : ";
    cin >> dec;
    if (dec < 0)
        cout << dec << " is not a positive integer." << endl;
    else
    {
        cout << "The binary form of " << dec << " is ";
        binary(dec);
        cout << endl;
    }
    return 0;
}

 

OUTPUT:

 

Hope you find it easy, reply in comment if you face any problem in executing the same.

Filed Under: conversion

C++ program to find HCF and LCM of two numbers

2 Comments

HCF ( Highest Common Factor): The LCM of 2 numbers is Highest common factor of those numbers. For e.g. we have 20 and 30, so HCF will be 10, as 10 is the highest common factor of 20 as well as 30. You must know how to calculate HCF of numbers.

 

LCM ( Lowest Common Multiple): LCM is the lowest common Multiple of those numbers. For e.g. LCM of 5, 7 is 35, as 35 is the least number divisible by both the numbers.

How Code Works:

  1. Take 2 numbers as ‘a’ and ‘b’, and find multiplication of them in ‘c’ i.e. c = a * b.
  2. Now while ‘a’ is not equals to ‘b’ we find the greater and subtract the lower from greater until both become equal.
  3. So finally we will have ‘a’ as our HCF and LCM will be ‘c/a’.

 

Dry run the Code:

  • Let’s take 4 and 6, i.e. a=4 and b=6.
  • Now c=a*b i.e. c=4*6 = 24.
  • While 4 is not equals to 6
  1. 6>4 i.e. b = 6-4 = 2
  2. Now a>b i.e. 4>2 so a = 4-2 =2.
  3. While is over now as ‘a’ = ‘b’
  • HCF = ‘a’ = 2
  • LCM = c / a i.e. 24/2 = 12.

Now let’s see the code. We can also find the HCF and LCM of Three numbers

C++ Program to find HCF and LCM of two numbers

[code lang=”cpp”]# include <iostream>;
using namespace std;

int main()
{
int a,b,c;
cout<< "Enter two nos :"<<endl;
cout<<endl;
cout<< "Enter first no. : ";
cin>>a;
cout<< "Enter sec. no. : ";
cin<<b;
c=a*b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<< "HCF = " << a<<endl;
cout<< "LCM = " << c/a<<endl;
return 0;
}[/code]

 

OUTPUT:

Please comment in case any errors.

Filed Under: cpp codes

C++ Program to check Prime number

Leave a Comment

Today we are going to write a C++ program to check Prime number i.e. if a number enter by user is a prime number or not.

We all know what a Prime number is let’s recall quickly:

Prime Number: A number which is divisible by 1 and itself is called Prime number i.e. it do not have any factors. Let’s see how to check Prime numbers in a program.

Logic behind finding prime number:

  • Start finding the factors of the number from 1.
  • Count the total number of factors.
  • If number of factors are 2 then it is a Prime number otherwise not.

Program to check Prime Number:

#include<iostream>
using namespace std;
int main()
{
    int number,count=0;
    cout<<"Enter a number: ";
    cin>>number;
    for(int a=1;a<=number;a++)
    {
        if(number%a==0)
            count++;
    }
    if(count==2)
        cout<<"t"<<number<<" IS A PRIME NUMBER n";
    else
        cout<<"t"<<number<<" IS NOT A PRIME NUMBER n";
    return 0;
}

 

OUTPUT:

Filed Under: cpp codes

C++ Program to swap two numbers without using temporary variable

3 Comments

Today we are going to write a C++ program to swap two numbers without using any temporary variable, let’s start. There are many ways to swap two numbers without using a Temporary variable.

Here I’ve discussed two methods:

  1. Using ‘+’ and ‘-‘ operators.
  2. Using Bitwise XOR ‘^’.
There is another way to swap using call by reference method.

 

So here’s the First Program:

C++ Program to Swap two numbers using ‘+’ ‘-‘ operators.

#include<iostream>
using namespace std;
int main()
{
   int a, b;
   cout<<"Enter two integers to swap\n";
   cout<<"Enter a= ";
   cin>>a;
   cout<<"Enter b= ";
   cin>>b;
   a = a + b;   \\ ex. a=5,b=6  so,  here a = 5+6 = 11
   b = a - b;   \\  b= a-b i.e. b = 11-6 = 5
   a = a - b;   \\  a= a-b i.e. a = 11-5 = 6
   cout<<"\nAfter Swapping\n";
   cout<<"a = "<<a<<"\nb = "<<b;
   return 0;
}

 

OUTPUT:

 

Now let’s discuss the second method i.e. using the Bitwise XOR ‘^’.

 

C++ Program to Swap two numbers using Bitwise XOR ‘^’

#include<iostream>
using namespace std;
int main()
{
   int a, b;
   cout<<"Enter two integers to swap\n";
   cout<<"Enter a= ";
   cin>>a;
   cout<<"Enter b= ";
   cin>>b;
   a = a ^ b;
   b = a ^ b;
   a = a ^ b;
   cout<<"\nAfter Swapping\n";
   cout<<"a = "<<a<<"\nb = "<<b;
   return 0;
}

 

OUTPUT:

There many other ways in which we can do this like using call by reference etc. I’ve discussed the simplest one hope you like it, please comment if you have any other methods or any improvement for the above codes. Thank you.

Filed Under: cpp codes

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 63
  • Go to page 64
  • Go to page 65

Primary Sidebar

Recent Posts

  • Total Quality Management Questions and Answers – Agile Manufacturing
  • How to Become a Cyber Security Engineer?
  • How to Become a Software Architect?
  • 7 Best Languages to Learn IoT Development in 2020
  • REDIS Tutorial: Beginners
  • Privacy Policy
  • About
  • Contact US

© 2020 ProProgramming Privacy Policy About Contact Us