this is our homework...
// make a c program that will ask any number from the user.
//Output all numbers multiples of 3 from number to 30.

// make a c program that will ask any number from the user. 
//Output all numbers multiples of 3 from nuber to 30.

#include<stdio.h>


int main()
{
    int num = 0;
    
    printf("Enter a number: \n");
          scanf("%d",&num);
    while ( num <= 30)
    {
          printf("%d \n", num);
         
          num = num + 3;
    }
    {
          if ( ( num % 3 )== 1 )
          {
           printf("%d \n",num);
           num = (num  + 2) + 2;
           }
           while ( ( num % 3) == 0)
           { 
           printf("d \n", num);
           num = (num + 1) + 3; 
           }
           while ( ( num % 3 )== 2 )
           {
           printf("%d \n",num);
           num = num  + 1;
           }
    }     
    getchar();
    getchar();
}            
          
          
//1) Ask for integer from user.
//2) If num is less than 30 display multiples of 3.
//3) if else exit.

What should i do with this one?.,
I really need your help guys.

Recommended Answers

All 11 Replies

DoEds,

Put if statements inside the while loop.

Step 1: Get an integer from the user (store it in a variable)
Step 2: Check whether the integer is higher or lower than zero:

IF number < zero:
    Print out the multiples ([B]Step 3[/B])

Step 3 (Get all multiples of 3 for all numbers less or equal to 30):

  • Create a variable (here I'll call it tmp) and initialize it to the first multiple of three.
  • You create a while loop and let it run as long as tmp is less or equal to 30.
  • Each time you print the value of tmp.
  • At the end of the loop you each time add three to tmp.

dude, i can suggest a different approach for ur problem....
since 30 is divisible by 3 so start ur loop from 30 and subtract 3 each time and print until ur number becomes less than the input number...

this approach will print number between ur input and 30 but in decreasing order..

try it.

dude, i can suggest a different approach for ur problem....
since 30 is divisible by 3 so start ur loop from 30 and subtract 3 each time and print until ur number becomes less than the input number...

this approach will print number between ur input and 30 but in decreasing order..

try it.

Why starting with 30 if you can easily start with 3 and each time add 3 ?
Then the output will be in a more logical order, right?

but starting from 3 will add unnecessary numbers in our answer...u have to check whether ur number is not less than ur input number and should not greater than 30...and also u have to reject sum ur calculated numbers

in my each calculated number will be our answer..and we do only compare with input number...

but starting from 3 will add unnecessary numbers in our answer...u have to check whether ur number is not less than ur input number and should not greater than 30...and also u have to reject sum ur calculated numbers

in my each calculated number will be our answer..and we do only compare with input number...

You don't understand what I'm saying, right?

from which value ur loop is going to start?

from which value ur loop is going to start?

Well, I would be glad if you had read my post:

Why starting with 30 if you can easily start with 3 and each time add 3 ?
Then the output will be in a more logical order, right?

:)

Oh i got it....Thx!

#include<stdio.h>

int main()
{
    int num=0;
    printf("Enter number: ",num);
    scanf("%d",&num);
    
    if (num <= 30)
    {
            while(num <= 30)
            {
            num=num+1;
                      if ((num%3)==0)
                      {
                      printf("%d \n",num);
                      }
            }
    }
            
    else
    { 
    printf("The number exceeded 30 \n");
    }
getchar();
getchar();
return 0;
}

but starting from 3 will add unnecessary numbers in our answer...u have to check whether ur number is not less than ur input number and should not greater than 30...and also u have to reject sum ur calculated numbers

in my each calculated number will be our answer..and we do only compare with input number...

Buddy, if he start from 30 in reverse order, then also he need to check if the value is between 3 and 30 or not. This will only increase the confusion..

Buddy, if he start from 30 in reverse order, then also he need to check if the value is between 3 and 30 or not. This will only increase the confusion..

Depends on the way how you implement it, if you each time decrease with 3, then you only have to check whether it's higher or equal to 3.

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.