
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print the nearest prime number formed by adding prime numbers to N
As per the question, the task is to find the nearest prime number by adding the prime number starting from 2 if the number N is not Prime.
Input: N=6 Output: 11
Explanation
Since 6 is not prime add first prime to 6 i.e. 2 which will result to 8 now 8 is also not prime now add next prime after 2 which is 3 which will give 8+3 = 11. Hence 11 is a prime number output will be 11.
Algorithm
START Step 1- > declare num=15, i = num/2 Step 2 -> Loop For k=2 and k<=i and k++ Set I=k/2 Loop For j=2 and j<=l and j++ Set flag=0; If k%j=0 Set flag=1 Break End End IF flag=0 Set num=num+k; End IF Set a=num/2 Loop For m=2 and m<=a and m++ Set flag1=0; IF num%m=0 Set flag1=1 break End END If flag1=0 Print num End END STOP
Example
#include<stdio.h> int main(){ int num =15 ; int i,k,j,sum=0,flag=0,l,flag1=0,a,m; i=num/2; for(k=2;k<=i;k++) { l=k/2; for(j=2;j<=l;j++) { flag=0; if(k%j==0) { flag=1; break; } } if(flag==0) { num=num+k; } a=num/2; for(m=2;m<=a;m++) { flag1=0; if(num%m==0) { flag1=1; break; } } if(flag1==0){ printf("%d",num); return 0; } } }
Output
if we run the above program then it will generate the following output
17
- Related Articles
- Nearest prime less than given number n C++
- Nearest Prime to a number - JavaScript
- Print prime numbers from 1 to N in reverse order
- Finding nearest prime to a specified number in JavaScript
- Print all prime numbers less than or equal to N in C++
- Print prime numbers with prime sum of digits in an array
- Counting prime numbers from 2 upto the number n JavaScript
- Prime numbers upto n - JavaScript
- Java program to print a prime number
- Java program to print prime numbers below 100
- Print all Semi-Prime Numbers less than or equal to N in C++
- Sum of the first N Prime numbers
- The number 144 is divisible by the prime numbers 2 and ___
- The prime numbers 13 and 31 are formed by the same numbers. Find such pairs of numbers up to 100.
- What are prime numbers and co-prime numbers?

Advertisements