Question

Your assignment: Write a program that takes a number from the command line in the program...

Your assignment:

Write a program that takes a number from the command line in the program C.

It should print:

The number.

The number of bits in the number that are set to 0 and 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

below i have written the complete C program as per the requirement.

=============================================================

Program:

=============================================================

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main(int argc, char *argv[])

{

   //variable declaration

   int ones = 0;
   int zeros = 0;

   char *num = (char*)malloc(100*sizeof(char));

   printf("The number is: %s\n",argv[1]);

   //copy the argv[1] to num variable upto its length

   strncpy(num, argv[1], strlen(argv[1]));

   //if number of arguments passed are less than 2 then print usage message

   if(argc < 2)

   {

       printf("Usage .filename number\n");

       return 1;

   }

   //loop through num until null char occurs

   while(*num != '\0')
   {

       //if char is zero then increment count of zeros by 1
       if(*num=='0')
       {
           zeros++;
       }
       //if char is one then increment count of ones by 1
       if(*num=='1')
       {
           ones++;
       }
       *num++;

   }

   //display the zeros and ones

   printf("number of Bits set to zero = %d\n", zeros);

   printf("number of Bits set to one = %d\n", ones);

   return 0;

}


=============================================================

Sample Output:

=============================================================

Kindly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
Your assignment: Write a program that takes a number from the command line in the program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....

    Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit...

  • Write A Program called Math a. Takes as input two Strings from the Command Line args[0],...

    Write A Program called Math a. Takes as input two Strings from the Command Line args[0], args[1] b. Convert these two strings to doubles. c. Add them together. d. If the second number is no-Zero divide them. e. Report on the results.

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • Write a program in C that takes a file name as the only argument on the...

    Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...

  • calculator: Write a program that takes three command-line arguments: number operator number and performs the required...

    calculator: Write a program that takes three command-line arguments: number operator number and performs the required operation and prints the result on a single complete line in standard output. (The four operators of a arithmetic, + * - /, must be recognized here.) please use C++

  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

  • c++ assignment about command line

    Write a program that uses command-line processing to get a number of integers and then print them out in descending order. The number of input integers is limited to 5 ; otherwise, an error message should be displayed. A sample output is shown below:I still cant figure out how to do the c++ command line assignment on visual studios so help me with the full coding asap.Keep in mind the program should be a beginner's level and dont make it...

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT