main method not being executed - c

Here is a program to find prime numbers using sieve of Eratosthenes. The program is compiling but on execution, it becomes non responsive.The print statement itself is not executed. Can I know where I have gone wrong?
#include<stdio.h>
int main()
{
printf("Enter the range");
int n,i;
scanf("%d",&n);
int j;
int a[--n];
for(i=0;i<n;i++)
a[i]=i+2;
for(i=0;i<n;i++)
if(a[i])
{
printf("%d",a[i]);
for(j=2;(i*j)<n;j++)
a[i*j]=0;
}
return 0;
}
Thanks

Your program is infinite looping the first time through the loop.
When i = 0 this loop never ends:
for(j=2;(i*j)<n;j++)

Your printf call might be being buffered which means it might not actually be printed until the buffer fills up or a newline is encountered.
Try adding a newline to the end of your string or call fprintf(stderr, ...) instead (which isn't buffered).

Related

In this program to determine whether number is palindrome or not, I can't figure out what's going wrong? I think scanf isn't even taking value

I tried to detect whether a number is palindrome or not without using library functions and without array. i don't think the logic is the problem here. I introduced printf's as a checkpoint after scan and some of the loops but i think the code isn't even reading the input as it is unable to print the value of input
#include<stdio.h>
int main()
{
long a,b,x,i,j,d,y;
long g=0;
long n=0;
long c=10;
printf("Enter a number: ");
scanf("%li",&a); \\takes input
printf("%li",a); \\prints the scanned input(Checkpoint)
do{
n++;
b=a/10;
}while(b!=0); \\to determine the number of digits in input
printf("%li",n); \\(checkpoint)
x=a;
for(i=0;i<n;i++)
y=c*10;
printf("%li",y); \\(checkpoint)
for(j=0;j<n;j++) \\store the reverse value of input in g
{
d=x%c;
y=y/10;
g=g+(d*y);
x=x/10;
}
if(g==a) \\check if number is palindrome
{
printf("Palindrome");
}
else
{
printf("Not a palindrome");
}
return(0);
}
In the loop
do{
n++;
b=a/10;
}while(b!=0);
you never modify the value of a, so the result of a/10 and thus the value of b will always be the same. If a is equal to or larger than 10 then you will have an infinite loop here, as b will never be equal to zero.
Since the output before the loop isn't flushed (actually written to the terminal) then it can look like the scanf call will never return.

Why does my program crash after entering an integer? - C

I'm extremely new to C coding and i'm wondering why this is crashing like this? After I input a value and press enter, my program instantly crashes. I remember learning there are times when you use a & with an array in the scanf line and sometimes you don't. So when I remove the & it crashes instantly. I'm not sure how to troubleshoot this problem and would appreciate help.
What i'm trying to accomplish:
"Write a program that asks the user to enter a sequence of integers terminated by 0 ( the last number is 0) and prints all the numbers entered on one line."
The program crashes before I can enter the other variables. I was not done coding but since it keeps crashing instantly I can't go further.
int main () {
int ru[1000];
int read;
int nums;
int counts;
printf("Enter integers, press 0 to end user input \n");
while (nums>0) {
scanf("%d",&ru[nums]);
if (nums==0)
printf("%d ", ru[nums]);
}
system("pause>nul");
return 0;
}
As noted by several people already, you don't ever assign a value to nums at any point in your code, but make use of it in several places.
You should populate nums and whilst it's more than zero (this should probably be not equal to zero if you want to also include negative integers), store it's value into your array. You can track where you are in the array using another variable (I've picked the read one that you'd already declared), making sure that it is first initialised to 0.
Once the while loop is terminated, either by nums being zero or you filling up the array, you can then print out the numbers you've collected.
int main (void) {
int ru[1000];
int read=0;
int nums;
int counts;
printf("Enter integers, press 0 to end user input \n");
scanf("%d",&nums);
while ((nums>0)&&(read<1000)) {
ru[read++]=nums;
scanf("%d",&nums);
}
for(counts=0;counts<read;counts++) {
printf("%d ",ru[counts]);
}
printf("\n");
system("pause>nul");
return 0;
}
The scanf() you are using as the following prototype:
int scanf(const char *format, ...);
you should give the de pointer to the buffer variable as a parameter but your are giving a pointer to an array (pointer to a pointer).:
scanf("%d",&ru[nums]);
A solution to your problem might be:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int ru[1000];
int i = 0;
printf("Enter integers, press 0 to end user input \n");
do
{
scanf("%d",&ru[i]);
}while (ru[i]!= 0 && i++ < 1000);
for(i = 0; ru[i] != 0 ; i++)
printf("%d ", ru[i]);
return EXIT_SUCCESS;
}

While loop not repeating in c

I have to create a program that requests integer numbers from the user
repetitively though the keyboard until the user enters 0. I've gotten to the while loop and its not repeating and I'm not sure why. Any help would be greatly appreciated.
Thanks
Edit: I've fixed the loop in terms of it not repeating but now it's infinitely repeating and I have no clue why.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char first[30], last[30];
int n, even, odd, etotal = 0, ototal = 0;
printf("What is your first name?\n");
scanf("%s", &first);
printf("What is your last name?\n");
scanf("%s", &last);
// asks for name
printf("Enter a number\n");
printf("To quit the program enter 0\n");
scanf("%d", &n);
while (n!=0){
if(n%2==0){
printf("%d is even.\n",n);
etotal++; }
else{
printf("%d is odd.\n",n);
ototal++; }
}
return 0;
}
The loop isn't repeating because you have a return statement in the loop, that will leave the main function immediately. Just remove it.
You also have another problem that's much worse: Undefined behavior. Local non-static variables, like for example n in your code, don't get initialized, instead they have an indeterminate value. Attempting to use such an uninitialized variable leads to said undefined behavior.
You need to explicitly initialize the variable to some value before using it in the condition, for example to reorder the code so you read input before the loop and then also at the end of the loop.
Local variable n is uninitialized producing undefined results collaboratively called undefined behavior. Initialize it before entering loop:
n = 1;
And check result of scanf:
if (scanf("%d", &n) != 1)
// error
Because n is not initialized, so it has a random value:
int n, even, odd, etotal, ototal;
/* ... */
while (n!=0){
Loop is not working because you didn't initialize n and unnecessary return 0 inside the while loop. You can fix it with do..while as below. Because you have the input statement as your first statement, do..while is best opted here.
do{
scanf("%d", &n);
if(n%2==0){
printf("%d is even.\n",n);
etotal++;
}
else {
printf("%d is odd.\n",n);
ototal++;
}
} while (n!=0);
You use scanf to read your input, but after your if/else statement you are returning 0 hence you are leaving the program. So it doesnt matter if you write an even or an odd number.
while (n!=0){
scanf("%d", &n);
if(n%2==0){
printf("%d is even.\n",n);
etotal++; }
else{
printf("%d is odd.\n",n);
ototal++;
}
return 0;
}

program running infinite times if entered character sometimes and sometimes not

this is a menu driven program having two functions. everything works fine if i enter numbers but when i enter character it runs infinite times sometimes :(
like when i enter integers it works fine and if i enter char it shows some junk value and then try again option is showed and i again enter char it runs infinite times
#include<stdio.h>
#include<conio.h>
#include<math.h>
void cal()
{
int x,y,z;
printf("enter two numbers\n");
scanf("%d%d",&x,&y);
z=x+y;
printf("%d\n",z);
}
void mul()
{
int x,y,z;
printf("enter two numbers\n");
scanf("%d%d",&x,&y);
z=x*y;
printf("%d\n",z);
}
void main()
{
int x,c;
clrscr();
menu :
printf("1.sum\n");
printf("2.mul\n");
printf("enter choice\n");
scanf("%d",&x);
switch(x)
{
case 1:cal();break;
case 2:mul();break;
default :printf("try again\n");
}
printf("press 5 to run another function\n");
scanf("%d",&c);
if(c==5)
{
goto menu;
}
getch();
}
You could try updating your gcc compiler. I ran the code on gcc 4.8 and the code terminated well for characters as well.
Otherwise if you really want to handle characters as well you take an input using a char pointer (string that is ) and then using atoi(str) store it in an int variable and then process it. And you can check : if the user enters characters ( using isalpha() ) then terminate the code.
Sample Code : (ran well enough)
char *s = malloc(64);
scanf("%s",s);
if(isalpha(s[0]))
return ;
else
int x = atoi(s);
int sum = x + 1; //or whatever manipulations you need to do
"when i enter character it runs infinite times sometimes ..."
All the code that tries to read an int uses the following of some sort. When non-numeric data is entered, scanf("%d", ... does not consume that IO (leaving it for the next IO operation), does not set c to any value, and then returns a 0 (which is not tested). At that point c has whatever value it had before the scanf() call. Since c was not initialized, its value could be anything - hence undefined behavior as suggested by #BLUEPIXY
int c;
...
scanf("%d",&c);
Since the non-numic data is left for the next IO and the next IO could be another scanf("%d", code is stuck in a rut - infinite loop.
To best fix, initialize variables, get the user input via fgets() and detemrine the value via sscanf() or strtol().
int c = 0;
char buf[80];
if (fget(buf, sizeof buf, stdin) == NULL) Handle_EOForIOerror();
if (sscanf(buf, "%d",&c) != 1) Handle_NonNumericInpupt();

program to find the sum of digits

I can't figure out the problem in this:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d ",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
printf ("hence the simplified result is %d",count);
}
return 0;
}
There's a silent killer in your code:
scanf("%d ",&a);
The extra space in your scanf will make entering numbers harder: this will match 12<space>, but not 12. Replace the "%d " with "%d".
You do not terminate your printf() with a "\n". The output stream (stdout) is, usually, line buffered. That means that incomplete lines need not be printed unless you force them with fflush(). But there's no need for that.
Simply add a "\n" to your printf()
printf("hence the simplified result is %d\n", count);
One issue is you print the count with every loop, rather than than after the loop.
Not an issue, but C has arithmetic assignment (aka compound assignment) operators that can be more readable. For example, a /= 10 is equivalent to a = a/10.
I think the printf statement should be outside the loop.
Move the printf out side the loop. That will fix it.
Try the following:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
}
printf ("hence the simplified result is %d",count);
return 0;
}

Resources