Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
See more:
Hi, I would like to display the odd and even integers from 1 upto given number and display the sum of odd and sum of even but there's always an error saying too few parameters. (void function is required) And lastly where should i put the formula for getting the sum of odd and sum of even?

C++
# include <stdio.h>  
# include <conio.h>  

void even_odd(int n);

int main()  
{  
    int n, i ;  
    clrscr() ;  
    even_odd();
    getch() ;  
    return 0;
}  

void even_odd(int n){
    printf("Enter Integer : ") ;  
    scanf("%d", &n) ;  
    printf("\nThe odd numbers are :\n\n") ;  
    for(i = 1 ; i <= n ; i = i + 2)  
        printf("%d\t", i) ;  
    printf("\n\nThe even numbers are :\n\n") ;  
    for(i = 2 ; i <= n ; i = i + 2)  
        printf("%d\t", i) ;  
}
</conio.h></stdio.h>
Posted
Updated 9-Jan-23 7:42am
v2

Others have already described the problem with the call. So what comes to calculating the sum; You already have two loops, one for odd and another for even numbers, why not calculate the sum inside those loops.

Something like:
C++
...
for(i = 1 ; i <= n ; i = i + 2) {
   printf("%d\t", i) ;  
   odd_sum = odd_sum + i;
}
...
 
Share this answer
 
Comments
[no name] 30-Sep-15 10:51am    
thank you guys
Leo Chapiro 30-Sep-15 10:54am    
A good one, +5!
Quote:
an error saying too few parameters. (void function is required)

Your function

C
void even_odd(int n) 


expects a parameter, but you call it without any:

even_odd();


Try it like this:

# include <stdio.h>  
# include <conio.h>  
 
void even_odd();
 
int main()  
{  
    int i ;  
    clrscr();
    even_odd();
    getch() ;
    return 0;
}  
 
void even_odd()
{
    int i, n;
    printf("Enter Integer : ") ;  
    scanf("%d", &n) ; 
	
    printf("\nThe odd numbers are :\n\n") ;  
    for(i = 1 ; i <= n ; i = i + 2)  
        printf("%d\t", i) ;

    printf("\n\nThe even numbers are :\n\n") ;  
    for(i = 2 ; i <= n ; i = i + 2)  
        printf("%d\t", i) ;  
}
 
Share this answer
 
v4
Comments
Wendelius 30-Sep-15 11:07am    
That should work just fine, a 5.
Leo Chapiro 30-Sep-15 11:33am    
Thank you, Mika! :)
Your problem is that n is not a parameter but a local variable
C++
void even_odd(int n){

must be replaced by
C++
void even_odd()
    int n;{


Ooops, you also need to change the line before main to
C++
void even_odd();


If you compile as is, Turbo C will tell you that there is another problem because i and n are used in even_odd but declared in main.
the source code with both corrections will look like
C++
# include <stdio.h>
# include <conio.h>

void even_odd();

int main()
{
    clrscr() ;
    even_odd();
    getch() ;
    return 0;
}

void even_odd(){
    int n, i ;
    printf("Enter Integer : ") ;
    scanf("%d", &n) ;
    printf("\nThe odd numbers are :\n\n") ;
    for(i = 1 ; i <= n ; i = i + 2)
        printf("%d\t", i) ;
    printf("\n\nThe even numbers are :\n\n") ;
    for(i = 2 ; i <= n ; i = i + 2)
        printf("%d\t", i) ;
}
</conio.h></stdio.h>
 
Share this answer
 
v3
Comments
Wendelius 30-Sep-15 11:06am    
Looks good, 5.
Patrice T 30-Sep-15 11:23am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900