Archive
Archive for January 4, 2010
C++ Program to check Command Line Arguments – Q4
January 4, 2010
Leave a comment
Q4. Program to check Command line arguments:
Accept an n digit number as command line argument & perform various tasks specified below:
i) Find the sum of digits of the number
ii) Find the reverse of a number
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> #include <string.h> #include <math.h> void Sum_Num(char *); void Rev_Num(char *); void main(int argv, char *argc[]){ char patt[10]; clrscr(); if(argv < 1) cout<<"\n Insuffecient Arguments. \n"; else{ strcpy(patt, argc[1]); cout<<”\n Argument is: “<<patt; Sum_Num(patt); Rev_Num(patt); } getch(); } void Sum_Num(char *pt){ int sum = 0, i = 0; while(*(pt+i) != '\0'){ sum += int(*(pt+i)) - 48; i++; } cout<<"\n Sum: "<<sum; } void Rev_Num(char *pt){ long r_num = 0; int i = 0; while(*(pt+i) != '\0'){ r_num += (int(*(pt+i)) - 48) * pow(10, i); i++; } cout<<"\n Reverse: "<<r_num; }
Output:
Argument is: 12345
Sum: 15
Reverse: 54321