Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi i wanted to make the output in column form how can i do that and whats wrong with the sum instead of adding odd/even nos it just gives the next even and next odd nos
column form like this:
Column1    Column2   Column3
    100       1221      9348
     23        211       214


C++
# 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, odd_sum=0, even_sum=0;
    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) ;
    odd_sum= odd_sum + i;
    printf("\nThe sum of odd numbers are:\n\n");
    printf("%d\t",odd_sum);
 
    printf("\n\nThe even numbers are :\n\n") ;  
    for(i = 2 ; i <= n ; i = i + 2)  
        printf("%d\t", i) ;  
    even_sum= even_sum + i;
    printf("\nThe sum of even numbers are:\n\n");
    printf("%d\t",even_sum);
}
Posted
Updated 30-Sep-15 9:29am
v5
Comments
phil.o 30-Sep-15 13:45pm    
Since the example given does not match what the code outputs, how can we guess? ^^
The algorithm in itself actually computes the sum of odd and even numbers, so this must be a problem in the way you display the results.
Did you write this code yourself? Do you understand it? Because it does exactly what it is supposed to.

1 solution

Advice 1: Never post code indented with tabs, it can't be rendered on the site. Use spaces instead.

Advice 2: Google is your friend, use it to find tutorials for Turbo C beginners and follow them. Until you understand what you do.

Advice 3: Use the debugger to see your code running and follow variables. With the debugger, you see what your code is really doing.

Your errors show that you need more study about C syntax. The debugger is really a great help to understand what is doing your code.

Forget about columns for now, first you need to get the right values.

See the code bellow, pay attention to comments:
C++
# include <stdio.h>  
# include <conio.h>  
 
void even_odd();
 
int main()  
{  
    int i ;    // i is not used in main function, you can remove the line
    clrscr();
    even_odd();
    getch() ;
    return 0;
}  
 
void even_odd()
{
    int i, n, odd_sum=0, even_sum=0;
    printf("Enter Integer : ") ;  
    scanf("%d", &n) ; 
 
    printf("\nThe odd numbers are :\n\n") ;  
    for(i = 1 ; i <= n ; i = i + 2) {  // the { says that you group more than one line under the loop
        printf("%d\t", i) ;
        odd_sum= odd_sum + i;    // this line was out of the for loop
    }  // end of group
    printf("\nThe sum of odd numbers are:\n\n");
    printf("%d\t",odd_sum);
 
    printf("\n\nThe even numbers are :\n\n") ;  
    for(i = 2 ; i <= n ; i = i + 2) {  // the { says that you group more than one line under the loop
        printf("%d\t", i) ;  
        even_sum= even_sum + i;    // this line was out of the for loop
    }  // end of group
    printf("\nThe sum of even numbers are:\n\n");
    printf("%d\t",even_sum);
}</conio.h></stdio.h>

Should be closer from what you want.
 
Share this answer
 
v2

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