| | |
how do u find prime numbers in an array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 13
Reputation:
Solved Threads: 0
first write a program to catch all the prime numbers in to an array in a given or a chosen range....
and compare the elements of prime numbers with the each element of the array u want...
if one of ur array element is equal to a element of prime number array,,that means it's a prime number....
e.g.
A[4]={1,5,3,4,17}; //range is 1 to 20
primArray[10]={2,3,5,7,11,13,17,19}
now compare array 'A' with the each element of 'primArray'.
if u find hard to write a program to catch prime numbers of a given range to an array email me....
and compare the elements of prime numbers with the each element of the array u want...
if one of ur array element is equal to a element of prime number array,,that means it's a prime number....
e.g.
A[4]={1,5,3,4,17}; //range is 1 to 20
primArray[10]={2,3,5,7,11,13,17,19}
now compare array 'A' with the each element of 'primArray'.
if u find hard to write a program to catch prime numbers of a given range to an array email me....
Speak English please. And once you do that, you can show some effort.
http://www.daniweb.com/techtalkforum...cement8-2.html
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.daniweb.com/techtalkforum...cement8-2.html
http://www.catb.org/~esr/faqs/smart-questions.html
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
A prime number is a number that can only be evenly divided by itself and 1. Let number x equal a given element in the array. Now you can for loop from 2 to x-1 and use the modulus operator. If the resulting value is equal to 0 then it is certainly not a prime.
Good luck, LamaBot
Good luck, LamaBot
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
/* This program is for checking a single integer*/ #include<stdio.h> #include<stdlib.h> main() { int a,i; printf("\nEnter a positive integer\n"); scanf("%d",&a); if(a<=0) { printf("\nInvalid input\n"); exit(1); } for(i=2;i<=a-1;i++) if(a%i==0)break; if(i==a)printf("\n%d is prime\n",a); else printf("\n%d is not a prime\n",a); } /* This is for elements in an array*/ #include<stdio.h> #include<stdlib.h> #define MAX 5 main() { int a[MAX],i,m; printf("\nEnter elements into array\n"); for(m=0;m<=MAX-1;m++) { scanf("%d",&a[m]); if(a[m]>0) { for(i=2;i<=a[m]-1;i++) if(a[m]%i==0)break; if(i==a[m])printf("\n%d is prime\n",a[m]); else printf("\n%d is not a prime\n",a[m]); } else printf("\n%d is not positive\n"); } }
Last edited by WaltP; Feb 21st, 2007 at 2:58 am. Reason: Added Code Tags -- please use them yourself. Instructions in the stickys.
![]() |
Similar Threads
- Finding Prime numbers without using Boolean (C++)
- C++ prime numbers (C++)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the C Forum
- Previous Thread: Convert to Absolute Values
- Next Thread: read each line of file
Views: 5103 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






