求和误差以及如何在列中输出 [英] Sum Error And How Can I Make The Output In Column

查看:59
本文介绍了求和误差以及如何在列中输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想以列形式输出我怎么能做到这一点并且总和错了而不是添加奇数/偶数nos它只给出下一个偶数和下一个奇数nos

列形式如下:

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







# 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);
}

推荐答案

建议1 :永远不要发布带有标签的代码缩进,它不能在网站上呈现。请改用空格。



建议2 :Google是您的朋友,用它来查找Turbo C初学者的教程并关注它们。直到你明白你做了什么为止。



建议3 :使用调试器查看代码是否正在运行并遵循变量。使用调试器,您可以看到代码的确在做什么。



您的错误表明您需要更多关于C语法的研究。调试器对于理解你的代码是什么有很大的帮助。



暂时忘记列,首先你需要得到正确的值。



请参阅下面的代码,注意评论:

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:
# 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.


这篇关于求和误差以及如何在列中输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆