Posts

Showing posts with the label While loop

Practice Programs for Java

  Write a program and simple display   “Helo world……My name is Ali Haider” (by System.out.print statement ) Write a program that get your roll number and then display it on the screen. (by System.out.print statement & int n=input.nextInt();)   Write a program that get input from user in two variables and program show sum, subtraction, multiply and divide of these two values. (by variables)   Write a program for ABC bank system; user ask to enter the amount to deposit in saving account, number of year for save the amount and interest rate in percentage. Program should display the the net amount after given number of year. (simple arithmtic operator)   Write a program that ask from user to enter two numbers in to different variable and then the program decide and show that which value is greater.(by if statement)   Write a program that swap the value; such that user enter two integre values in two different variables and then s...

While loop

While loop:                    The while loop is a conditional loop, which repeat the code written in the body of the loop. The while is used to repeat the code until the given conditional remain true. There are many uses of loops such as repeat a piece of codes, repeat any logic for number of times and print the series of number as our own desire. The loop is also used to save our time and space, such that if we repeat a same code for 100 times it takes more time to write and more space to store the coding. The loop is easy to debug and control the flow of program. Syntax:                 The general syntax for write the while loops. While (condition) { Body of loop; increment if needed; } Example:               ...

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