C++ Program Based on Loops

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Problem 1

/* Programs based on loops 
 1. Fibbonacci series 
  */
 //  0   1   1   2   3   5   8   13  21 ..........n

#include<iostream>
using namespace std;
int main()
{
      int a=0,b=1,c,n;
      system("cls");
      cout<<"Enter the limit";
      cin>>n;
      if(n==1)
       cout<<a;
     else if(n==2)
       cout<<a<<"  "<<b;
     else if(n>2)   // n=10
     {
          cout<<a<<"  "<<b;    //   0   1   1    2    3  5   8 
          int i=1;
          while(i<=n-2)  //4<=8     a=8  b=5
          {
            c=a+b;   //  c=8
            cout<<"   "<<c;  
            a=b;
            b=c;
            i++;
          }
     }  
return 0;
}

Program 2

/* Programs based on loops 
  Prime Number
 */
 #include<iostream>
using namespace std;
int main()
{
     int n,i=2;
     bool flag=true;
     system("cls");
     cout<<"Enter a number";
     cin>>n;
     while(i<n)      //   3  to   n=9
     {
          if(n%i==0)   
          {
               flag=false;
               break;
          }
          i++;
     }
     if(flag==true)
      cout<<"\nNo is Prime";
      else
      cout<<"\nNo is not Prime";
     return 0;
}

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *