Skip to content

C Program to print prime numbers up to the inputted number

by yashan on September 9th, 2011

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

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS