C programming Use a dowhile loop to implement repeating unti
Solution
Tested on Ubuntu,Linux
/*****************Fibonacci.cpp******************/
# include <iostream>
using namespace std;
int main(){
//variable declaration
int N;//for Number of terms
int m1=1,m2=1,result=0,termsCount=0,modResult=0,flag=0;
char choice;
std::cout<<\"This is the Fibonacci Sequence mod 10 fact Checker\"<<std::endl;
do{
/*Prompt for input*/
std::cout<<\"How Many terms do you wish to check? \";
std::cin>>N;
/*If N>=2 then it will execute below code */
if(N>=2)
{
/*Resetting variable again to initial state*/
m1=1,m2=1,result=0,termsCount=0,modResult=0,flag=0;
/*Printing first Two terms*/
std::cout<<++termsCount<<\":\"<<m1<<std::endl;
std::cout<<++termsCount<<\":\"<<m2<<std::endl;
while(N>2){
/*Computing result mod 10*/
termsCount+=1;//increasing term count
result=(m1+m2)%10;//mod 10
m1=m2;
m2=result;
//mod result 5
modResult=result%5;
/*If result mod 2 is zero*/
if(result%2==0){
flag=1;
std::cout<<termsCount<<\": \"<<result<<\" - \"<<\"even\";
if(modResult==0){
std::cout<<\" - \"<<\"divisible by 5\";
}
if(result==0){
std::cout<<\"- is Zero\"<<std::endl;
}else{
std::cout<<std::endl;
}
}
/*If result mod 5 is zero and result is not 0*/
if(modResult==0 && result!=0){
flag=1;
std::cout<<termsCount<<\": \"<<result<<\" - \"<<\"divisible by 5\"<<std::endl;
}
/*Result is zero and flag not equal to 1 means above condition was not execute*/
if(result==0&& flag!=1){
flag=1;
std::cout<<\"- is Zero\"<<std::endl;
}
/*If all above condition was not execute*/
if(flag==0){
std::cout<<termsCount<<\": \"<<result<<std::endl;
}
/*Resetting flag to 0*/
flag=0;
/*decreasing to 1*/
N--;
}
}
else{
std::cout<<\"Please Enter Number of Terms >=2 \"<<std::endl;
}
std::cout<<\"Would you try again (Y|N)? \";
std::cin>>choice;
}while(choice==\'Y\' || choice == \'y\');
return 0;
}
/*****************output**************/
anshu@anshu:~/Desktop/chegg$ g++ Fibonacci.cpp
anshu@anshu:~/Desktop/chegg$ ./a.out
This is the Fibonacci Sequence mod 10 fact Checker
How Many terms do you wish to check? 17
1:1
2:1
3: 2 - even
4: 3
5: 5 - divisible by 5
6: 8 - even
7: 3
8: 1
9: 4 - even
10: 5 - divisible by 5
11: 9
12: 4 - even
13: 3
14: 7
15: 0 - even - divisible by 5- is Zero
16: 7
17: 7
Would you try again (Y|N)? y
How Many terms do you wish to check? 10
1:1
2:1
3: 2 - even
4: 3
5: 5 - divisible by 5
6: 8 - even
7: 3
8: 1
9: 4 - even
10: 5 - divisible by 5
Would you try again (Y|N)? n
Thanks a lot
If You have any Query feel free to ask


