How can i write a code in c# to take a number from the user
and then find all the prime numbers till the number entered
by the user.

Answer Posted / shuklaamit565

using System;
class Test2
{
public static void Main()
{
int prime,num=100;

for(int i = 3; i <num; i++)
{
prime = 1;
for(int n = 2; n <= i - 1;
n++)
{
if(i % n == 0)
{
prime = 2;
}
}
if(prime==1)
{
Console.WriteLine( "
is prime :"+i);
}
}
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

1852


Can we have inheritance without polymorphism?

767


How Do you Code Composition and Aggregation in C++ ?

24637


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

1077


write a program that takes input in digits and display the result in words from 1 to 1000

2167






What are oops functions?

764


Is react oop?

793


write a program to find 2^n+1 ?

1755


What is the purpose of polymorphism?

844


when to use 'mutable' keyword and when to use 'const cast' in c++

1862


What is encapsulation in ict?

786


What is use of overloading?

826


What are the three main types of variables?

807


What is class encapsulation?

796


What is static in oop?

805