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: