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.

Answers were Sorted based on User's Feedback



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

Answer / kumar gaurav

void main()
{
int num=0, i=0;
printf("Enter the number to ckeck");
scanf("%d",&num);
for (i=2;i<num;i++)
{
if (num%i==0)
{
printf("Number is not prime");
break;
}
}
if (i==num)
{
printf("Number is prime");
}
getch();
}

Is This Answer Correct ?    10 Yes 1 No

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

Answer / 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

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

Answer / guest

for(i=0;i<n;i++)
{
if(i==1||i==2||i==3||i==5||i==7)
{
printf("No is prime");
}
else if(i%2==0&&i%3==0&&i%5==0&&i%7==0)
{
printf("No is prime");
}
else
printf("No is not prime");
}

Is This Answer Correct ?    5 Yes 9 No

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

Answer / anjali

while(num>0)
{
if(num%2==1)
printf("num is prime value");
}

Is This Answer Correct ?    1 Yes 10 No

Post New Answer

More OOPS Interview Questions

What is the purpose of polymorphism?

0 Answers  


What is the correct syntax for inheritance? 1) class aclass : public superclass 2) class aclass inherit superclass 3) class aclass <-superclass

6 Answers   Wipro,


How to call a non virtual function in the derived class by using base class pointer

3 Answers   HCL,


oops concept is used for?

3 Answers   Synergy,


#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

0 Answers  






what is virtual function?

3 Answers  


What Is a Polymorphism? How many types of polymorphism and whats that use in application?

2 Answers  


Program to print 0 to 9 in cross order

3 Answers  


if i have same function with same number of argument but defined in different files. Now i am adding these two files in a third file and calling this function . which will get called and wht decide the precedence?

1 Answers  


What is oops in programming?

0 Answers  


When is a memory allocated to a class?

11 Answers  


Whats is abstraction in oops?

0 Answers  


Categories