Posts

Showing posts with the label If

Print and calculate the following series 1/1 + 2/3 + 3/5 + 4/7.........+19/27

#include<iostream> using namespace std; void main() { float deno = 1; float sum = 0; for(float num = 1; num<= 19 ; num=num+1 ) { cout<<num<< "/" <<deno<<" + " ; deno = deno + 2; sum = sum + (num/deno); } cout<< "Sum of series = " << sum <<endl; }

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..."...

Print and calculate the following series 1/2 + 2/3 + 3/4................+ 49/50

#include <iostream> using namespace std; void main () { float deno=2; float neu=1; float sum=0; float ans; for (int i=1;i<50;i++) { ans=neu/deno; sum=sum+ans; // add in the sum variable for display the sum of all the number cout<<neu<<"/"<<deno<<"\t"; neu=neu+1; deno=deno+1; } cout<<"\n\nThe sum of the series: "<<sum<<endl; }

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<< "! " ;                           ///////////////////           ...

If statement | How If statement works

Write a program that input two numbers and it decide either they are equal or not. # include <iostream> using namespace std; void main() {        int a;        int b;        //get value for a        cout<< "Please enter the Value of A :" ;        cin>>a;        //get the value for b        cout<< "Please enter the value of B :" ;        cin>>b;                              if (a == b)              cout<< "These two values are equal to each other " <<endl;   ...

If statement | Condition | Check | Checking condition

I f statement is a checking condition in C++ language and many others also. By this we can impose a check point and control the flow of program during the execution of the program. The if statement is firstly check the given criteria and then execute the statement or block of statements. The if statement is very important in any language. It is called by many names as if statement, if condition, if and check. Usage:           If condition (only if not nested if) is used in that place where we have  check one condition and then execute some statements. If the condition become true or satisfy the statements or block of statements written very next after if condition, execute and if the condition become false or unsatisfying then the program ignore the statements or block of statements written very next after if condition and control transfer to other statement.          ...