printing too much stars [closed] - c

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 7 years ago.
Improve this question
I type this code on Ubuntu 14.04
#include<stdio.h>
int main() {
int i, j, n;
scanf("%",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
but it print too much stars and it Continue until I close it.

Should be:
#include<stdio.h>
int main() {
int i, j, n;
scanf("%d",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
You forgot to tell scanf you were reading in an integer by using the %d argument

There is mistake in
scanf("%",&n);
it will be:
scanf("%d",&n);
to tell the complier that the value of n is an integer type.

Related

The program is not terminating [closed]

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
#include<stdio.h>
int reverse(int );
int main()`
{
int num,rem,r;
printf("Enter a number:");
scanf("%d",&num);
r=reverse(num);
r=reverse(num);
return 0;
}
int reverse(int num)
{
int rem,rev=0;
while(num>=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
return rev;
}
Its not showing any errors and after entering the number the program stay still, its neither showing any output nor terminating
You have not used printf for output.
r = reverse(num);
printf("%d",r);
return 0;
Use this.
And also while condition should be
while (num > 0)
There is no need to call reverse function twice as well.

output is address instead of values [closed]

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 2 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i,limit,sum=0;
int a[100];
setbuf(stdout,NULL);
printf("enter the limit");
scanf("%d",&limit);
printf("enter the values");
for(i=0;i<limit;i++)
{
scanf("%d",&a[limit]);
}
for(i=0;i<limit;i++)
{
sum=sum+a[i];
}
printf("the sum is : %d",sum);
return 0;
}
Output:
enter the limit3
enter the values3
3
3
the sum is : 19265880
The actual output should be 9
for(i=0;i<limit;i++)
{
scanf("%d",&a[limit]); //replace limit with i
}
You can see that scanf is reading into the array a[limit] instead of a[i]. This is causing the issue.

C - Warning while compiling simple program [closed]

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 6 years ago.
Improve this question
Im receiving warnings like this "warning: array subscript has type 'char' [-Wchar-subscripts]" in lines 11, 12 and 15. Whats wrong with my code?
#include <stdio.h>
#include <ctype.h>
int main()
{
char s[1000], i, d, a;
printf("Enter a string: ");
fgets(s, 1000, stdin );
for(i = 0; s[i] != '\0'; i++){
if (isalpha(s[i])){
a++;
}
if (isdigit(s[i])){
d++;
}
}
printf("Number of digits: %d ... Number of letters: %d ", d, a);
return 0;
}

int variable should have value 45, is printed as 2358648 [closed]

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 7 years ago.
Improve this question
How this answer is come
#include<stdio.h>
#include<conio.h>
int main() {
int i, sum;
for (i = 0, sum = 0; i < 10; i++)
sum += i;
printf("%d", & sum);
return 0;
}
Output
2358648
You are printing addess instead of value
just remove & in printf
printf("%d",sum);

Why won't a 5x5 multiplication array appear? [closed]

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 years ago.
Improve this question
I did this and I think it should work:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++);
{
for(j=1;j<=5;j++);
{
printf("%d",i*j);
}
}
return 0;
}
But it just prints out 36...
What am I doing wrong?
Remove the two extra ;s:
for(i=1;i<=5;i++);
^
{
for(j=1;j<=5;j++);
^
As others have pointed out, the semicolons before the opening braces of your for loops are preventing the printing.
Additionally, if you want the output to appear as a matrix, you'll want to printf a newline after your first for loop:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d",i*j);
}
printf("\n");
}
return 0;
}

Resources