Write a macro to find the sum of n numbers. 7m Jun2007

By | July 11, 2014

Write a macro to find the sum of n numbers. 7m Jun2007

Code:

#include<stdio.h>
# define SUM(n) ( (n * (n+1)) / 2 )
void main()
{
int num;
clrscr();
printf(” Enter number : “);
scanf(“%d”,&num);
printf(“\n Sum of n numbers : %d “,SUM(num));
getch();
}

[codesyntax lang=”c”]

c#include<stdio.h>
# define SUM(n) ( (n * (n+1)) / 2 )
void main()
{
int num;
clrscr();
printf(" Enter number : ");
scanf("%d",&num);
printf("\n Sum of n numbers : %d ",SUM(num));
getch();
}

[/codesyntax]

Screen Shots:

C_program_Macro_Sum

 

C_program_Macro_Sum_Output

Leave a Reply