#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],count,c,n,i,j,sum=0;
clrscr();
printf("Enter the no. of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
count=0;
printf("\nprime numbers are:\n");
for(i=0;i<n;i++)
{
c=0;
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
c=1;
break;
}
}
if(c==0)
{
printf("%d\t",a[i]);
sum=sum+a[i];
count++;
}
}
printf("\nThe number of prime numbers is %d",count);
printf("\nThe sum of prime no. is %d",sum);
getch();
}
#include<conio.h>
void main()
{
int a[50],count,c,n,i,j,sum=0;
clrscr();
printf("Enter the no. of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
count=0;
printf("\nprime numbers are:\n");
for(i=0;i<n;i++)
{
c=0;
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
c=1;
break;
}
}
if(c==0)
{
printf("%d\t",a[i]);
sum=sum+a[i];
count++;
}
}
printf("\nThe number of prime numbers is %d",count);
printf("\nThe sum of prime no. is %d",sum);
getch();
}
To find prime numbers in an array and to calculate the sum of those prime numbers |
1 is not a prime number. is there any way that i enter 1 in array but it did not calculte 1 as prime number... i m havine the same issue by iostream.h
ReplyDelete#Mady Gee, Here is the correct program:
Delete#include
using namespace std;
int main()
{
int a[50],c,n,sum=0;
cout<<"Enter the no. of elements:";
cin>>n;
for(int i=0;i>a[i];
}
cout<<"\nprime numbers are:\n";
for(int i=0;i<n;i++)
{
c=0;
for(int j=2;j<=a[i];j++)
{
if(a[i]%j==0)
{
c++;
}
}
if(c==1)
{
cout<<"\t"<<a[i];
sum=sum+a[i];
}
}
cout<<"\nThe sum of prime no. is "<<sum;
}
This comment has been removed by the author.
ReplyDeleteExcellent explanation by admin
ReplyDeleteprogram for Print Prime numbers from 1 to n
Easiest Program to Find Whether Given Number Is Prime Number Or Not
ReplyDeletePrime number program in C
ReplyDeleteThanks this is my homework
nice
Deletefind the number of prime numbers count, store them into a separate array and display it.
ReplyDeleteThis code is correct ?Prime Number Program in C
ReplyDelete#include
#include
void main()
{
int num,res=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
res=prime(num);
if(res==0)
printf("\n%d is a prime number",num);
else
printf("\n%d is not a prime number",num);
getch();
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else
return 1;
}
return 0;
}
Why is this incorrect?
ReplyDelete#include
#include
void main()
{
int n;
printf("Enter the numbr of elements:");
scanf("%d",&n);
int arra[n];
getch();
clrscr();
}
#include
ReplyDelete#include
pls enter the correct header file
There is no need to use break in the program
ReplyDeletePrograms rums properly avoiding break
ckeck code at http://www.programmingsimplysolved.com/c-programs/c-program-to-check-prime-number-or-not/
ReplyDeleteHow the count value is increasing
ReplyDelete#include
ReplyDeleteusing namespace std;
int main()
{
int arr[5],i,odd=0,j,k;
cout<<"Read array:";
for(i=0;i<=4;i++)
cin>>arr[i];
cout<<"i have read the following array:"<<endl;
for(i=0;i<=4;i++)
cout<<arr[i]<<endl;
for(i=0;i<=4;i++)
{
if(arr[i]%2==1)
odd++;
}
cout<<"odds are:"<<odd<<endl;
}
how i can make the list of odd numbers