I want to show prime numbers between two numbers like this 2,5,7,11
but it shows like this 2,5,7,11, there is an extra ",".
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
if(i!=1)
printf("%d,",i);
}
return 0;
}
Answering the question but not accounting for the point raised by #Bathsheeba:
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
int itemCount = 0; // NEW
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if (f == 0 && i != 1) // TESTS COMBINED
{
if (itemCount++ > 0) putchar(','); // HERE
printf("%d",i); // COMMA REMOVED
}
}
printf("\n"); // newline at the end
return 0;
}
The only way to really remove a comma after you've added it is if you're building a buffer at runtime - which is a reasonable approach - so here we have to only generate a comma when we need it, before all but the first item.
I have the code for finding prime numbers within a range.
The problem is to remove the last comma.
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
printf("%d,",x);
}
}
But the output contains an extra comma in the last.
For example
2,3,5,7,
whereas the expected output is
2,3,5,7
Instead of flag you can decide directly what you want to print between numbers
And note that you can break out of the internal loop as soon as f is set to 1
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
const char* delim = "";
scanf("%d%d",&a,&b);
for(x=a; x<=b; (x++,f=0))
{
for(i=2; i<x; i++)
{
if(x%i==0)
{
f=1;
break; //no need to continue the checking
}
}
if(f==0) {
printf("%s%d",delim,x);
delim = ", ";
}
}
putchar('\n');
}
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
char backspace = 8;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
printf("%d,",x);
}
printf("\b"); // or printf("%c", backspace);
}
Add another flag, just a simple counter that tells you if you are printing the first time then check the flag to decide what to print, e.g.
#include<stdio.h>
int main()
{
int a,b,i,x,c,first=0,f=1;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
{
if(first==0){
printf("%d",x);
}else{
printf(",%d",x);
}
first++
}
}
}
Use a flag to detect the first occurrence of printf() and print the first number as such without any ,. For consecutive number printing precede with ,
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1,flag=0;//Flag to mark first occurrence
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
break;// Once the condition fails can break of the for loop as it fails for the prime number condition at the first case itself
}
}
if(f==0)
{
if(flag==0)
{//Check if it is first time
printf("%d",x);
flag = 1;//If so print without ',' and set the flag
}
else
printf(",%d",x);// On next consecutive prints it prints using ','
}
}
}
This method also avoids the , when only one number is printed.
Eg: When input is 2 and 4. It prints just 3 and not 3,
Simply you need odd number best practice for minimum loop is given below;
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
scanf("%d%d",&a,&b);
while (a < b)
{
if ( (a%2) == 1) {
printf("%d", a);
if ( (a + 1) < b && (a + 2) < b)
printf(",");
}
a = a + 1;
}
}
please check from the site
http://rextester.com/MWNVE38245
Store the result into a buffer and when done print the buffer:
#include <stdio.h>
#include <errno.h>
#define RESULT_MAX (42)
size_t get_primes(int * result, size_t result_size, int a, int b)
{
int i, x, f = 1;
size_t result_index = 0;
if (NULL == result) || (0 == result_size) || ((size_t) -1 == result_size))
{
errno = EINVAL;
return (size_t) -1;
}
for (x = a; x <= b; (x++, f = 0))
{
for (i = 2; i < x; i++)
{
if (x % i == 0)
{
f = 1;
break;
}
}
if (f == 0)
{
result[result_index] = x;
++result_index;
if (result_size <= result_index)
{
fprintf(stderr, "Result buffer full. Aborting ...\n");
break;
}
}
}
return result_index;
}
int main(void)
{
int a = 0, b = 0;
int result[RESULT_MAX];
scanf("%d%d", &a, &b);
{
size_t result_index = get_primes(result, RESULT_MAX, a, b);
if ((size_t) -1 == result_index)
{
perror("get_primes() failed");
}
else if (0 == result_index)
{
fprintf(stderr, "No primes found.\n");
}
else
{
printf("%d", result[0]);
for (size_t i = 1; i < result_index; ++i)
{
printf(", %d", result[i]);
}
}
}
return 0;
}
This example uses a simple fixed-size buffer, if this does not suite your needs replace it by a dynamic one.
This is more of a "language-agnostic" problem: "How do I output a comma-separated list without a final comma?" It is not specifically about prime numbers.
You seem to be thinking of you list as a series of [prime comma] units. It isn't. A better way to think of it is as a single prime as the head of the list, followed by a tail of repeated [comma prime] units.
Some pseudocode to illustrate the general idea:
outputList(theList)
separator = ", "
output(theList.firstItem())
while (theList.hasMoreItems())
output(separator)
output(theList.nextItem())
endwhile
return
/* this is just logic */
for(i=2;i<=n;i++)
{
k=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
k=1;
}
if(k==0)
{
c++;
c++;
}
}
System.out.println(c);
for(i=2;i<=n;i++)
{
k=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
k=1;
}
if(k==0)
{
System.out.print(i);
b++;
if(b!=c-1)
{
System.out.print(",");
b++;
}
}
}
}
}
//comma separated values
#include <bits/stdc++.h>
using namespace std;
int Prime(int a, int n){
bool prime[n+1];
memset(prime,true,sizeof(prime));
for(int p=2;p*p<=n;p++){
if(prime[p]==true){
for(int i=p*p ; i<=n; i+=p ){
prime[i] = false;
}
}
}
for(int i = 2;i<= n;i++){
if(i==2) cout<<i; // here is the logic first print 2 then for other numbers first print the comma then the values
else if(prime[i]) cout<<","<<i;
}
}
int main(){
int a =2 ;
int n = 30;
Prime(a , n);
}
#include <stdio.h>
int main()
{
int i, j, n, count;
scanf("%d", &n);
for(i=2; i<n; i++)
{
count=0;
for(j=2; j<n; j++)
{
if(i%j==0)
count++;
}
if(count==1)
printf("%d," i);
}
printf("\b \b");
}
\b is a nondestructive backspace. It moves the cursor backward, but doesn't erase what's there, it replaces it. For a a destructive backspace,
use "\b \b" i.e. a backspace, a space, and another backspace.
This Program prints all the prime number up to given number with comma separated
The goal of this code was to print a pyramid. First I print a certain number of spaces then print some stars to eventually make a pyramid.
For example to print a pyramid of 5 first it would print a star after 4 spaces then the start and end variables would be changed so the new start would 3 and the new end would be six and it would print 3 stars.
#include <stdio.h>
#include <stdlib.h>
void printSpaces(int num){
int i;
for(i=0; i<num;i++)
{
printf(" ");
}
}
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start=n-1;
end=n+1;
}
}
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
pyramid(5);
return 0;
}
The only thing it seems to be doing printing a row of 2 stars over and over.
you set start to n-1, but the value of n never changes. That means that start will continuously be set to the same value, n-1(4). Same for end, your loop will never terminate.
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start=n-1;
end=n+1;
}
}
Also, on first invocation, k will be 4 and end will be 6, hence two stars.
Your problem is right here:
start=n-1;
end=n+1;
it should be something like start = start + 1 ?
I don't understand your program perfectly but I can tell that this is your error.
Matt McNabb was very close.
The following code contains the correct fix
The main change is to step 'start' and 'end' rather than re-initializing them to the passed parameter
#include <stdio.h>
#include <stdlib.h>
void printSpaces(int num){
int i;
for(i=0; i<num;i++)
{
printf(" ");
}
}
void pyramid(int n){
int start=n,end=n+1;
int k;
while(start>0 && end<2*n)
{
printSpaces(start);
for (k=start; k<end;k++)
{
printf("*");
}
printf("\n");
start--; //<< corrected
end++; //<< corrected
}
}
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
pyramid(5);
return 0;
}
the procedure that you are using to decrement the value of the variables end and start are wrong
I found this code about patterns in c in this site
http://www.programmingsimplified.com/c-program-print-stars-pyramid
#include <stdio.h>
#include <stdlib.h>
void pyramid(int end){
int k,c;
for (k=1; k< end; k++) {
printf(" ");
end= end -1;
for (c=1; c<= 2 * k-1; c++)
printf("*");
printf("\n");
}
}
int main() {
pyramid(15);
return 0;
}
The triangle should look like this
1
11
111
1111
11111
111111
the number of rows is entered by the user and then transforms to the function
Note: Without arrays, must be with one loop(while or for) and not nested loops
the closest I got is a code with 2 loop (but can`t think about how to do it with less loops)
int i,k;
for(i=1;i<=x;i++)
{
for(k=1;k<=i;k++)
{
printf("1");
}
printf("\n");
}
The above question was asked by someone but it was marked as off topic I don't know why..?
I came up with this solution tell me if it's correct or not?
int i=1,k=1;
while (i<=x)
{
if(k<=i)
{
printf("1");
k++;
continue;
}
i++;
k=1;
printf("\n");
}
thanks
You can replace loops(iteration) with recursion, try this(with one loop):
#include <stdio.h>
void draw(int depth)
{
int i;
if(depth <= 0) return;
draw(depth-1);
for(i = 0; i < depth; i++)
printf("1");
printf("\n");
}
int main()
{
draw(5);
return 0;
}
You can do it even without loop
#include <stdio.h>
void draw_num_char(int num)
{
if(num <= 0) return;
printf("1");
draw_num_char(num-1);
}
void draw(int depth)
{
int i;
if(depth <= 0) return;
draw(depth-1);
draw_num_char(depth);
printf("\n");
}
int main()
{
draw(5);
return 0;
}
Recursive solution, one loop, no arrays.
#include <stdio.h>
void liner(int line, int limit) {
int i;
if (line > limit)
return;
for(i=1; i<=line; i++)
printf("1");
printf("\n");
liner (line + 1, limit);
}
int main() {
int limit;
printf ("Enter number of lines ");
scanf ("%d", &limit);
liner (1, limit);
return 0;
}
write a program that enter a series of integer(stores in array),then sorts the integer by calling the function selection_sort. When given an array with n elements, selection_sort must do the following:
1.search the array to find the largest,then move it to the last position.
2.call itself recursively to sort the first n-1 elements of the array.
following is my code i think the code is errors everywhere i hope some master can help me
#include <stdio.h>
int selection_sort(int a[])//this function have error that "i"and"count"is undeclared
{
int max = 0;
for (i = 1; i <= count; i++)// continuous compare to final
{
if (a[i] > max)
{
max=a[i];
}
}
a[count] = a[i]; //put the max to last position
count--;
}
int main(void)
{
int a[100] = { 0 },i=0,count=0;
while (1)
{
scanf("%d",&a[i]);
if (a[i] = '\n') { break; }//this line have error because '\n' not "int" so when i "enter" it would not break
i++;
count++; //counting how many integer i scanf
}
selection_sort();//call this function (i don't know well about function so i don't known where to put is correct )
return 0;
}
You have to compare. BUt you are assigned..
change this if (a[i] = '\n') { break; } to if (a[i] == '\n') { break; }.
You should change follows in your code.
1) change your function call..
2) declare count and i in function..
3) for taking values in to array,follow other method..
Try yourself...
Here is the complete code.Basically there are many syntax and logical error in your code. Consider this as refer and compare with your original source.
int selection_sort(int a[], int count){
int max = 0, i =0;
for (i = 0; i < count; i++){
if (a[i] > max)
{
max=a[i];
}
}
a[count] = max;
count--;
return 0;
}
int main(void) {
int a[100] = { 0 },i=0,count=0;;
printf ("Enter the total num\n");
scanf("%d",&count);
if ( ( count ) >= (sizeof(a)/sizeof(a[0])) )
return -1;
while (i < count){
scanf("%d",&a[i]);
i++;
}
selection_sort(a, count);
printf ("\nmax:%d\n", a [count]);
return 0;
}