Multiplication Table With While Loop
Dec 12, 2014 at 1:38pm UTC
I have created Multiplication Table using while loop. Now I am trying to make multiplication table that show input only when a an "odd" number is entered. Can somebody hep me with that?
Here is how I created a simple multiplication table. (Runs in Borland C++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,i,tab;
cout<<"Enter the number:" ;
cin>>n;
i=1;
while (i<=10)
{
tab=n*i;
cout<<n<<"X" <<i<<"=" <<tab<<endl;
i++;
}
cout<<endl;
getch();
}
Last edited on Dec 12, 2014 at 1:39pm UTC
Dec 12, 2014 at 1:45pm UTC
Use an if statement!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include<iostream>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,i,tab;
cout<<"Enter the number:" ;
cin>>n;
if (n%2!=0)
{
i=1;
while (i<=10)
{
tab=n*i;
cout<<n<<"X" <<i<<"=" <<tab<<endl;
i++;
}
}
else
cout<<"even number entered!! error!" ;
cout<<endl;
getch();
}
Dec 12, 2014 at 1:50pm UTC
conio.h, void main, clrscr... whatever.
Maybe a for loop could be more appropriate in place of that while.
Dec 12, 2014 at 2:04pm UTC
@nerdpwn, thanks for your help. I will not forget this now. :)
BTW I did it with this way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include<iostream>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,i,tab;
cout<<"Enter the number:" ;
do {
cin>>n;
i=1;
if (n%2==0)
{cout<<n<<
" is even. Enter an odd number." ;
}
else {
while (i<=10)
{
tab=n*i;
cout<<n<<"X" <<i<<"=" <<tab<<endl;
i++;
}
}
cout<<endl;
}while (true );
getch();
}
@EssGeEich, I am studying Fundamentals of Programming and my assignment was to use while loop only.
Topic archived. No new replies allowed.