If and while statement problem (Beginner)

Thread Solved

Join Date: Jun 2009
Posts: 32
Reputation: DoEds is an unknown quantity at this point 
Solved Threads: 0
DoEds DoEds is offline Offline
Light Poster

If and while statement problem (Beginner)

 
0
  #1
Jul 14th, 2009
//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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 32
Reputation: DoEds is an unknown quantity at this point 
Solved Threads: 0
DoEds DoEds is offline Offline
Light Poster

Re: If and while statement problem (Beginner)

 
0
  #2
Jul 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: If and while statement problem (Beginner)

 
0
  #3
Jul 14th, 2009
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: If and while statement problem (Beginner)

 
0
  #4
Jul 14th, 2009
Originally Posted by DoEds View Post
*problem*
How to exit this program when the user assigned k to 1 ?
Use exit() function

http://www.cprogramming.com/fod/exit.html
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: If and while statement problem (Beginner)

 
0
  #5
Jul 14th, 2009
Originally Posted by tuse View Post
Use exit() function

http://www.cprogramming.com/fod/exit.html
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."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 32
Reputation: DoEds is an unknown quantity at this point 
Solved Threads: 0
DoEds DoEds is offline Offline
Light Poster

Re: If and while statement problem (Beginner)

 
0
  #6
Jul 19th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC