Advertisements
Home > Cpp > C++ Program to check Command Line Arguments – Q4

C++ Program to check Command Line Arguments – Q4


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


Advertisements
Categories: Cpp Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: