//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;
}

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;

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:

while(k > 1 && m < n)
{

}

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))

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 [I]exit_code[/I]; instead of the exit() function :)

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);
                   
              }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.