Why is it showing the error parameter is initialized [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 2 years ago.
Improve this question
#include<stdio.h>
int main()
int x=45,y=46,z=65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;

You forgot two braces
#include<stdio.h>
int main() {
int x=45,y=46,z=65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;
}

Because you forgot the braces. It thinks you're trying to create a function in the line that's getting an error. Here's the fixed code:
#include <stdio.h>
int main() {
int x = 45, y = 46, z = 65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;
}

Related

Please help me for the correct My Code looping (C Language) [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 1 year ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for(i=1; i<5; i++)
{
printf(" %d Perulangan %d \n ", i);
}
return 0;
}
Please help me for the correct My Code looping (C Language) , i want print this output :
1 perulangan 1
2 perulangan 2
3 perulangan 3
4 perulangan 4
Super easy to do. Just add in an additional i parameter for the 2nd %d like so:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for(i=1; i<5; i++)
{
printf(" %d Perulangan %d \n ", i, i);
}
return 0;
}
One would normally just repeat the argument.
printf("%d Perulangan %d\n", i, i);
Alternatively, a POSIX-compliant compiler will accept references to arguments by position:
printf("%1$d Perulangan %1$d\n", i);

how can i copy string to another variable and output with if else statement [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<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
poinless comments removed
code snippets moved into main
code formatted properly
scanf("%i \n", &i); replaced with scanf("%i", &i);
if (i = 0) replaced with if (i == 0)

My function does not return what it should [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 3 years ago.
Improve this question
I have an issue with a function I've created. It doesn't return 1. I tried using the code in main instead of in the function and it worked.
I don't know if I'm missing something or what so please check it out and tell me:
int happynum(int n); // THIS IS THE FUNCTION
int main()
{
int num,digito,i,dig,temp,sum=0, lol;
do {
printf("Escribe un numero positivo:\n");
scanf("%d", &num);
} while (num <= 0);
lol = happynum(num); // THIS IS WHAT THE FUNCTION RETURN
printf("%d ", lol);
while(num!=89 && num!=1) //THIS IS THE SAME FUNCTION BUT IN THE MAIN
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
printf("%d", num);
/*
if(num== 1)
{
printf("Happy Number\n");
}
else
printf("UnHappy Number\n"); */
return 0;
}
This is the function:
int happynum(int n) // THIS IS THE FUNCTION
{
int i,dig,num,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You're using the num local to happynum instead of the parameter that you pass. Name the argument num and then use that, like so:
int happynum(int num) // THIS IS THE FUNCTION
{
int i,dig,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You should then probably un-comment the block at the end of your main so you get the Un/Happy Number result.

What should I do to get rid of this error message? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I am running a hollow box statement and cant figure out the error message
error: expected identifier or ‘(’ before ‘{’ token" {
I have tried multiple variations of the bracket in different positions and it just causes more errors. Here is the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int i;
printf("Even numbers between 25 to 75: \n");
for (i = 25; i<=75; i++)
{
if(i%2 == 0)
{
printf("%d\t", i);
}
}
printf("All odd numbers between 500 to 400: \n");
for (i = 500; i>=400; i--)
{
if(i%2 == 0)
{
printf("%d\t", i-1);
}
}
int number, result, exponent;
result = 1;
printf("Enterthe base number: ");
scanf("%d", &number);
printf("Enter the exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= number;
--exponent;
}
printf("Answer = %d \n", result);
}
int f, w;
{
for (f = 1; f <= 7; f++)
{
for (w = 1; w <= 7; w++)
{
if (f==1 || f==7 || w==1 || w==7)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This is an update to the first question since my first post didnt show the whole code and everyones answer was asking for it.
You should put an int main(void) before the body of your main function. You can then move your variables inside the function. After you've done this, the top of your code should look like:
int main(void) /* Here! */
{
int f, w; /* Move this inside the function. */
for (f = 1; f <= 7; f++)
...

C: prime factoring [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 do you test for primes with the IsPrime method below? I cannot seem to get the printf to work in my IsPrime method and no errors were thrown.
#include <stdlib.h>
#include <stdio.h>
int IsPrime(unsigned int number) {
if (number <= 1) {
return 0; // zero and one are not prime
printf("zero and one are not prime.");
}
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) {
return 0;
printf("not a prime.");
}
}
return 1;
printf("You've found a prime!");
}
int main(void) {
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
if(a >= 1 && a <= 1000) {
printf("You entered: %d\n", a);
IsPrime(a);
}
else {
printf("Error! Please enter a value between 1 and 1000.");
}
}
You're return-ing from function before printf
You wrote printf statement after return; It's an easy fix, just swap the two instructions.

Resources