Users Online
· Guests Online: 28
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Find Sum of Numbers given in Command Line Arguments
Here is source code of the C Program to find sum of numbers given in command line arguments recursively. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find Sum of Numbers given in Command Line Arguments
* Recursively
*/
#include <stdio.h>
int count, s = 0;
void sum(int *, int *);
void main(int argc, char *argv[])
{
int i, ar[argc];
count = argc;
for (i = 1; i < argc; i++)
{
ar[i - 1] = atoi(argv[i]);
}
sum(ar, ar + 1);
printf("%d", s);
}
/* computes sum of two numbers recursively */
void sum(int *a, int * b)
{
if (count == 1)
return;
s = s + *a + *b;
count -= 2;
sum(a + 2, b + 2);
}
$ cc arg4.c
$ a.out 1 2 3 4
sum is 10
/*
* C Program to Find Sum of Numbers given in Command Line Arguments
* Recursively
*/
#include <stdio.h>
int count, s = 0;
void sum(int *, int *);
void main(int argc, char *argv[])
{
int i, ar[argc];
count = argc;
for (i = 1; i < argc; i++)
{
ar[i - 1] = atoi(argv[i]);
}
sum(ar, ar + 1);
printf("%d", s);
}
/* computes sum of two numbers recursively */
void sum(int *a, int * b)
{
if (count == 1)
return;
s = s + *a + *b;
count -= 2;
sum(a + 2, b + 2);
}
$ cc arg4.c
$ a.out 1 2 3 4
sum is 10
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.