while loop c++ example program,

while loop c++ example program

Program 1:


Write a program t calculate the sum of first ten odd numbers. Also print the odd numbers.

#include<iostream.h>
main ()
{
int s, n;
s=0;
n=1;
while (n<=10)
{
s=s+n;
cout<<n<<endl;
n=n+2;
}
cout<<"Sum="<<s;
}

Program 2:

Write a program to print the multiplication table of a number entered from the keyboard.

#include<iostream.h>
main()
{
int tab, res, c;
cout <<"Enter the value of table";
cin >> tab;
c=1;
while ( c<=10)
{
res =tab *c;
cout <<tab<< "*" << c "=" <<res <<endl;
c++;
}
}

Program 3:

Write a program to convert an integer number into the equivalent binary number.

#include <iostream.h>
main ()
{
int n, r;
cout << "Enter the integer";
cin >> n;
while (n>=1)
{
r = n % 2;
n = n/2;
cout << r;
}
}

Note:

To convert the decimal integer into its equivalent octal number, divider r & n by 8.
Similarly , to convert it into equivalent hexadecimal number , divider r and n by 16.
Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.