Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i, j, a, b, c=0;
scanf("%d %d", &a, &b);
for(i=a; i<=b; i++)
{
for(j=1; j<=i; j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n", i);
}
}
return 0;
}
The program however does not print prime numbers for a given range. Please help.
You have to reset c to 0 after every iteration. The loops should look like this
for(i=a; i<=b; i++)
{
c = 0;
...
An advice, you don't have to go till the number everytime to check for primality, you can go till square root of that number.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I have just started learning programming in c. I have written a program that has an arithmetic sequence based on the number of arithmetic sequence sentences n.
The program will not work after 17 without giving me an error
I have tried several different ideas but I have not received an answer
Thank you for your help.
#include <stdio.h>
int main()
{
int n ,k;
float d ;
printf("please write your arithmetic sequence sentences ");
scanf("%d",&n);
printf("\n");
printf("please write your common differences");
scanf("%d",&k);
printf("\n");
printf("please write your initial element ");
scanf("%f",&d);
printf("error 1");
printf("\n");
printf("error 2");
printf("number \t sum");
printf("erorr 3");
int i = 0;
int j = 0;
int sum = 0;
while (i < n)
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
return 0;
}
it is not python. You need to use {} to declare compound statement
while (i < n)
{
j = d + i*k;
sum += j;
printf("%d\t%d\n",j,sum);
i++;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include <stdio.h>
int main() {
int x, i, counter = 0;
printf("Input number!\t");
scanf("%d", &x);
for (i = 0; i <= x; i++) {
if (x % i == 0) {
counter++;
}
}
if (counter <= 2) {
printf("%d is a prime number.", x);
} else {
printf("%d is not a prime number.", x);
}
return 0;
}
It seems the loop part is the problem but I don't know why. I'm very new to programming so please bear with it if its a silly mistake.
Try this Code.
Its going to Infinity when divide by ZERO after giving input. Make
sure loop start with 1, when division inside loop
#include <stdio.h>
int main()
{
int x,i,counter=0;
printf("Input number!\t");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
if(counter<=2)
{
printf("%d is a prime number.",x);
}
else
{
printf("%d is not a prime number.",x);
}
return 0;
}
As #Some programmer dude mentioned, you can't do the x%0 because of division by 0 - this occured on first iteration.
So change your loop to starting from 1 like below:
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This code aims to exclude non-repeating values in a number list, but does not return repeats in their original sequence. How to keep all repeats with their precedence?
My code:
#include <stdio.h>
#include <stdlib.h>
int count=0;
int main ()
{
int i,j,k,x, altarray[1000],array[1000];
printf("Please enter the number of integers in your list:\n");
scanf("%d",&x);
printf ("Please enter the list of numbers\n");
for (i=0; i<x; i++)
scanf("%d",&array[i]);
printf("\nThe corrected array is...\n");
for (i=0; i<x; i++)
{
for (j=i+1; j<x; j++)
{
if(array[i]==array[j])
{
altarray[count]=array[i];
count++;
}
}
}
for (i=0; i<count; i++)
printf("%d",altarray[i]);
}
To search for duplicated number, you have to cover all elements of your array in the j for loop except the one that have the index i. So that loop should look like this:
for (j = 0; j < x; j++)
{
if ((array[i] == array[j]) && (i != j))
{
altarray[count] = array[i];
count++;
break;
}
}
I have added a break, to avoid wasting cycles after detecting the existence of the duplicated number.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Please help ! Why this Prime number Program in C is not working ?
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
getch();
}
Please answer-- Why This program is not working ??
Use a flag to check whether the number is divisible or not?
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,flag=0;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag = 1;
break;
}
}
if ( flag == 1 )
{
printf("%d is not a prime number",n);
}
else
{
printf("%d is a prime number",n);
}
getch();
}
Also, the for loop: for(i=2;i<=n-1;i++) iterates more than required. You should set i <= sqrt(n).