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;
}