943,706 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 660
  • C RSS
Jul 14th, 2009
0

If and while statement problem (Beginner)

Expand Post »
//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.

#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.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009
Jul 14th, 2009
0

Re: If and while statement problem (Beginner)

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.
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009
Jul 14th, 2009
0

Re: If and while statement problem (Beginner)

Why would you want to wrap this if around the while:
if (k > 1 && m < n)
{
  while(k > 1 && m < n)
  {
  
  }
}
As you can see both conditions are the same, so you can leave out this if

Edit::
Like this:
  1. while(k > 1 && m < n)
  2. {
  3.  
  4. }
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))
Last edited by tux4life; Jul 14th, 2009 at 11:01 am. Reason: fix broken code tag
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 14th, 2009
0

Re: If and while statement problem (Beginner)

Click to Expand / Collapse  Quote originally posted by DoEds ...
*problem*
How to exit this program when the user assigned k to 1 ?
Use exit() function

http://www.cprogramming.com/fod/exit.html
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jul 14th, 2009
0

Re: If and while statement problem (Beginner)

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 19th, 2009
0

Re: If and while statement problem (Beginner)

Thanks a lot man...
I've figured out this problem already...
I put a another condition in the mudulo statement
 if ((m%k)==0 && n != m)
              {
                   printf("\n %d",m);
                   
              }
Last edited by DoEds; Jul 19th, 2009 at 12:56 am.
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Problems with fseek
Next Thread in C Forum Timeline: wrong # of args in function call printf





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC