Write a program in the C programming language to print all the prime numbers up to the inputted number. This program is being made by using the nested for loop statements and if statements. Below is the code of this C program –

/* C program to print prime numbers */ #include<stdio.h> void main() { int n,i,j,ct=0; printf("Enter any number \n"); scanf("%d",&n); printf(" All prime numbers are -\n"); for(i=2;i<=n;i++) { ct=0; for(j=2;j<i;j++) { if(i%j==0) { ct=1; break; } } if(ct==0) { printf("%d \t",i); } } getch(); }

**Description** – The outer for loop is used to create the numbers up to the limit and the nested loop is used to check if the numbers are prime or not and if any of the number is prime then it prints the number.

**Input – **15

**Output **– All prime numbers are – 2 3 5 7 11 13

it s vry useful…..the subject s vry clear nw………

it very use ful for my sem examination thank you

What do you mean when you defined “ct = 0” ? Thank for your reply

What do you mean by ‘ct’??

What u mean ct=0

Ct variable acts as a flag variable. When ct=1 of that particular value of i, then that value is not a prime number which will not be printed. Hope you got it.

You can optimize you code to the following,

function getPrimesTill(n){

var i, j, len, limit, result = [];

for(i=2;i<n;i++){

limit = i/2;

len = result.length;

isPrime = true;

for(j=0;len && result[j]<=limit;j++){

if(i%result[j] == 0){

isPrime = false;

break;

}

}

if(isPrime) result.push(i);

}

return result;

}

I feel that this code can be optimized to a very good level when it comes to Efficient Programming.

#include

int main()

{

int count=2,num;

printf(“Enter a Number:\t”);

scanf(“%d”,&num);

for(count=2;count<=num/2;count++)

{

if(num%count==0)

{

printf("%d is not a Prime Numer\n",num);

break;

}

}

if(count==num)

{

printf("%d is a Prime Number\n",num);

}

}

Source: http://www.codingalpha.com/prime-number-c-program/

“C Program to print prime numbers up to the inputted number”

not

“Find if a number is prime or not”.

It is very useful reference to learn the logic writing …thank you