Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
good afternoon to all..

Question:

program that need to prints 1 to 100 numbers.the number which are all multiples of 3 will need to print as 3.like wise the number which are all multiples of 5 will need to print as 5.As in the same way the numbers which are all multiples of both 3 and 5 will need to print as 35.


Can anybody provide the code snippet?

Thanks in advance
Posted
Updated 30-Aug-12 0:54am
v2
Comments
nika2008 30-Aug-12 6:43am    
i think u got 2 good solution and need to accet them
jai_mca 30-Aug-12 7:28am    
y u deleted ur code nika?

Yes.
But we won't.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
jai_mca 30-Aug-12 6:41am    
hello Mr.Original..

i am not a student.k?

i just attended one interview today.they let me to do the above program.i know how to print 1 t 100.but the condition given was some what not working..

for clarification i asked the doubt..k?
OriginalGriff 30-Aug-12 6:48am    
Yeah right - I am sure you are...it's such a difficult problem after all...

What part are you stuck on?
jai_mca 30-Aug-12 6:53am    
wait i will insert my code
jai_mca 30-Aug-12 7:14am    
This is my code:

int i;
int a = 3, b = 5,c=35;
for (i = 0; i <= 100; i++)
{
i = i + 1;
if (i % 3 == 0)
{
Console.WriteLine(+a);
}
else if (i % 5 == 0)
{
Console.WriteLine(+b);
}
else if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine(+c);

}

else
{
Console.WriteLine(+i);
}




}
Santhosh Kumar Jayaraman 30-Aug-12 7:19am    
have u tried my solutioon?
Besides the fact, that this sound like an entry level programming question, it is a high-school level mathematics question also. You need prime factorization. Don't try to be a programmer if you can't solve this somehow. Here is a recursive and generic approach: http://handcraftsman.wordpress.com/2010/09/02/prime-factorization-in-csharp/[^], and this is an other one: http://www.blackwasp.co.uk/PrimeFactors.aspx[^].

some mathematical basics: http://www.mathsisfun.com/prime-factorization.html[^], http://en.wikipedia.org/wiki/Prime_factor[^]
 
Share this answer
 
v2
Comments
jai_mca 30-Aug-12 7:31am    
hello...if u r too smart means insert ur own idea.don't put the link..
i hav the logic.but it does not shows the output properly.
Zoltán Zörgő 30-Aug-12 8:45am    
I am smart enough to search. You should to this too.
try this

C#
class Program
    {
        public static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                int j=i;
                if ((i % 3 == 0) && (i % 5 != 0))
                    j = 3;
                else if ((i % 5 == 0) && (i % 3 != 0))
                    j = 5;
                else if ((i % 5 == 0) && (i % 3 == 0))
                    j = 35;

                Console.WriteLine(j);
            }
            Console.ReadLine();
        }
    }
 
Share this answer
 
Comments
ridoy 30-Aug-12 7:20am    
good answer..+5 from me...not sure why someone give it just 1!
Santhosh Kumar Jayaraman 30-Aug-12 7:20am    
that happens always:)
Santhosh Kumar Jayaraman 30-Aug-12 7:59am    
you know what , two people have downvoted it and both of their scores are more than 10k.
jai_mca 30-Aug-12 7:26am    
3 and 5 constraint k sathosh.But if A number is multiples of both 3 and 5 means,it need to print as 35.this will not working in ur code
Santhosh Kumar Jayaraman 30-Aug-12 7:27am    
How you are saying that?Have you tried it first? cant you see last else if loop? Dont assume yourself by seeing code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900