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

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

Screen Shots:

C_program_Macro_Sum

 

C_program_Macro_Sum_Output

Leave a Reply