Posts

Showing posts with the label if on loop

Print a table of ASCII code from 33 to 259

# include <iostream> using namespace std; void main () { int number=33; // here the number is set as 33 char ascii=number;// gte the ascii code of the int number while (number<=256) // loop is countinue till the givem limit is approached { cout<<ascii<<"      "<<number<<endl; number++; // number is increamnet ascii++; // here ascii is increament } }

Check that enter number is even , odd or prime ............unless the user press (n)

#include <iostream> using namespace std; void main () { char test='y'; int i=1; int ans=0; int counter=0; for (int j=1; j<=2;) // for repeat the Q again and again if (test=='y' || test=='Y') { cout<<"Please enter the number here:"; int number; cin>>number; if (number<0) // check a positive number cout<<"You can only enter a positive number"<<endl; else  { if (number==1) cout<<"The number is prime."<<endl; else { while (i<=number) { if (number%i==0)  counter++;   i++; } } if (counter==2)  cout<<"The number is prime"<<endl; if (number%2==0) // for even number cout<<"The number is even..."<<endl; else  cout<<"The number is odd..."...

To check that enter number is Armstrong or not. (Armstrong no if 3^3 + 7^3 + 1^3 = 371 then 371 is a Armstrong no)

#include <iostream> using namespace std; void main () { cout<<"Please enter the number here: "; int number; cin>>number; int number1=number; int d=1; int cube; int sum=0; while (number >=1 ) { d=number%10; // for get the digit of number cube=d*d*d; // for getting the cube sum=cube+sum; // for get the sum number=number/10; // for reduce the numeber } if (sum==number1) cout<<"The number is Armstrong"<<endl; else cout<<"The number is not Armstrong"<<endl; }

Print following series 1 + 2x + 3x^2 + 4x^3...............(n+1)x^n

#include <iostream> using namespace std; void main () { cout<<"Please enter the Base (X) here:"; int base; cin>>base; cout<<"Please enter the power (n) here :"; int power; cin>>power; int power2; power2=power+1; // for manage the loop iteration int ans=1; int ans2=1; int sum=0; int i=1; while (i<=power2) // outer loop for series { for (int j=1;j<=i;j++) // inner loop for make power { if (j<=1)  // for manage the series and write only 1 ans=1; else if (j<=2) // for second factor then series and for not print power on second digit ans=base; else ans=(ans*base); // for power } if (i==1) // for not multiply coffient with base in first digit ans2=ans; else ans2=i*ans; // for multiply the cofficient with base sum=sum+ans2; ans=1; ans2=1; i++; } cout<<"The sum of the following series is:...

Print and calculate the sum of following series x + x^2 + x^3 + x^4.................x^n

#include <iostream> using namespace std; void main () { cout<<"Please enter the Base (X) here:"; int base; cin>>base; cout<<"Please enter the power (n) here: "; int power; cin>>power; cout<<"\n"; // for line space only int i=1; //outer loop int ans=1; int sum=0; while (i<=power) { cout<<base<<"^"<<i<<" "; if (i!=power)  // for control the '+' sign cout<<" + "; else ; // black statement for (int j=1; j<=i;j++) { ans=ans*base; //base multiply it self by number od power e.g. 2^3= 2*2*2 = 8 } sum=sum+ans; ans=1; // for remove the old value i++; // increament of the outer loop } cout<<"\n\nThe sum of the above series is:"<<sum<<endl; }

Print and sum of following series 1! + 2! +3!.............upper limit

#include <iostream> using namespace std; void main () {        cout<< "Please enter the upper limit here:" ;        int upperlimit;        cin>>upperlimit;        int i=1; // initialization for outer loop        int fact=1; //initialization for factorial variable        int sum=0; // 'O' is the identity of addition        while (i<=upperlimit)        {               cout<<i<< "! " ;                           ///////////////////           ...

Check on loop | Ask user to run / terminate the loop | Loop

# include <iostream> using namespace std; void main() { int i=1; char checker='y'; // for first run while (i>0) // an infintie loop {                          if (checker=='y' || checker=='Y') //  condition  check        {                  cout<<"Ali\n"; // text which is repeated                  i++;               cout<<"Press y to continue and n to terminate =";        cin>>checker; // get value to terminate or continue the loop         }        else              break; // if y ot Y is not printed then loop ...