Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
45. Write a program to find the sum of digits of a 5 digit number using command line argument.
/*
File: Prgrm45.c
Author: Aditya Saini
Date: Jan 20, 2021
Description: Program to find the sum of digits of a 5 digit number using command line argument.
*/
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int num, sum;
if (argc == 1)
printf ("Error! No command line argument found.");
else
{
num = atoi (argv[1]);
//Calculate sum of digits
sum = 0;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
}
//Print result
printf ("Sum of digits: %d", sum);
}
};
Output
C:\Users\digita>"G:\KCS151 or KCS251\Prgrm45.exe" 12345
Sum of digits: 15
No comments:
Post a Comment
Please do not post spam links.