| | |
If and while statement problem (Beginner)
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 32
Reputation:
Solved Threads: 0
//This program will ask the user for k, m and n.
//k should not be equal to 1.
//Output all numbers multiple by k between m and n. (ex. k=2,m=5,n=20 output should be 6 8 10...18)
*problem*
How to exit this program when the user assigned k to 1 ?
And why is it that when i assign an even-number to k it will output until n. And n should not appear on the screen. Actually this was already answered by our teacher but i want to know how.
//k should not be equal to 1.
//Output all numbers multiple by k between m and n. (ex. k=2,m=5,n=20 output should be 6 8 10...18)
*problem*
How to exit this program when the user assigned k to 1 ?
And why is it that when i assign an even-number to k it will output until n. And n should not appear on the screen. Actually this was already answered by our teacher but i want to know how.
#include <stdio.h>
int main()
{
int k, m, n;
printf("Enter a value for k: ");
scanf("%d",&k);
printf("Enter a value for m: ");
scanf("%d",&m);
printf("Enter a value for n: ");
scanf("%d",&n);
if (k <= 1 )
printf("k should not be equal to one \n");
if ( m < n)
{
while ( m < n )
{
m=m+1;
if ((m%k)==0)
{
printf("\n %d",m);
}
}
}
if (m>n)
printf("\n m should not be greater to n");
getchar();
getchar();
return 0;
} Last edited by DoEds; Jul 14th, 2009 at 10:46 am.
•
•
Join Date: Jun 2009
Posts: 32
Reputation:
Solved Threads: 0
Oh i figured out the problem when k is assign to 1.
if ( k <= 1)
printf("\n k should not be equal to 1");
if (k > 1 && m < n)
{
while (k > 1 && m < n )
{
m=m+1; Last edited by DoEds; Jul 14th, 2009 at 10:42 am.
Why would you want to wrap this if around the while:
As you can see both conditions are the same, so you can leave out this if 
Edit::
Like this:
This is possible because the instructions in the while loop's body will only get executed when the condition is true (in this case the condition is: k > 1 && m < n, so if this condition isn't true at the beginning of the while loop, your program will just skip over all the instructions inside the loop's body, and proceed executing the first statement after the while loop (if there's one))
if (k > 1 && m < n) { while(k > 1 && m < n) { } }

Edit::
Like this:
C Syntax (Toggle Plain Text)
while(k > 1 && m < n) { }
Last edited by tux4life; Jul 14th, 2009 at 11:01 am. Reason: fix broken code tag
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
*problem*
How to exit this program when the user assigned k to 1 ?
http://www.cprogramming.com/fod/exit.html
My blog on .NET- http://dotnet.tekyt.info
Possible, but if your main function contains your whole program, then I would rather suggest you to use:
return exit_code; instead of the exit() function
Last edited by tux4life; Jul 14th, 2009 at 11:18 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- Insert statement problem in sql server 2005 (MS SQL)
- insert statement problem (C#)
- WHERE statement problem. (PHP)
- Problem kicking my but!!! can anybody help? (C++)
- implement semaphore without the use of while or any if statement (C)
- Simple If statement questions (Java)
- If statement problem (Java)
- declaration syntax error? (C++)
- CardLayout problem restated (Java)
Other Threads in the C Forum
- Previous Thread: Problems with fseek
- Next Thread: wrong # of args in function call printf
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming standard strchr string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






