Code Example – Check for Prime Number Objective C

This is a simple Objective C program written to check whether a number is a Prime number.


// main.m

// PrimeNumbers


// Created by Ravi Shankar on 10/02/12.

// Copyright (c) 2012 All rights reserved.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])


@autoreleasepool {


int number;

BOOL isPrime=YES;

NSLog (@”Enter a number”);


for (int i=2; i < number –1; i++)


if (number % i == 0)


isPrime = NO;




if (isPrime)


NSLog (@”%i is a Prime Number”,number);




   NSLog (@”%i is not a Prime Number”, number);




return 0;


Output (as displayed in Xcode console window)

2012-02-10 15:53:52.072 PrimeNumbers[1164:707] Enter a number


2012-02-10 15:53:58.665 PrimeNumbers[1164:707] 23 is a Prime Number

2012-02-10 15:54:41.469 PrimeNumbers[1176:707] Enter a number


2012-02-10 15:54:44.589 PrimeNumbers[1176:707] 24 is not a Prime Number

This program checks whether the number is divisible by any of the n-1 number and if divisible (% operator returns reminder as 0) then the flag is set to false otherwise it is set to true. Then based on the flag value the message is printed to the console window.


One response to “Code Example – Check for Prime Number Objective C”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.