I have to make a program in which
take two numbers as input
print prime numbers between them
take sum of them
and print the prime numbers is ascending order.
Is it possible to make it without loops?
Printable View
I have to make a program in which
take two numbers as input
print prime numbers between them
take sum of them
and print the prime numbers is ascending order.
Is it possible to make it without loops?
In theory, yes, just write a statement for each possible case.
In practice, no.
In theory, yes, you could use recursion or goto's.
In practice, anyone doing this should be shot.
;)
how is it done by loop way sorry have no clue :(
Making it without loops is not possible. Why? Because you don't know what numbers user is gonna input. That's the beauty of programming, making an algorithm that can handle every situation.
To test a number you would use the next statement:
Or make a function:Code:if((n!=2 && n!=3 && n!=5 && n!=7) && (n%2==0 || n%3==0 || n%5==0 || n%7==0))
{
do something if it's NOT a prime
}
Code:int isprime(int n)
{
if((n!=2 && n!=3 && n!=5 && n!=7) && (n%2==0 || n%3==0 || n%5==0 || n%7==0)) return 0; //NOT a prime
return 1; //A prime
}
> Is it possible to make it without loops?
Gotta love the bone-headed assignments from clueless teachers.
What exactly is the point of these exercises?
It's not like you're going to reuse your new found skill (writing programs without loops) ever again.
Perhaps he attends a class in functional programming
errr.. depends how you define a loop.. you could just write the loop in assembly instead, but honestly I can't think of a way to do that.. think about how you as a human would try to solve a proble like that. Somebody gives you two numbers, you check each number to see if it's prime, and you continue doing so until you've done all the numbers. That's a loop; programming in ways that are different from how you think tends to be unproductive in my experience