49736

Basic C programming

I'm trying to write a program which given a certain number of input will output the product of the listed input (only accounting inputs of 0-9 and ignoring other). For example: input:345 would result output: 60, or another example would be, input: 3t4 and output: 12

I have given it many attempts and this is what I'm stuck with:

#include <stdio.h> main(){ int c,i; c = getchar(); i = 1; while (c!= '\n'){ if (c>=48 && c<=57){ i=c*i; c=getchar(); } } printf("%d",i); }

Answer1:

Change

i = c*i;

to

i = (c - '0')*i;

What is happening here is the variable c contains the ascii value of the number not the number. So, '0' is 48, '1' is 49 and so forth. So when you enter 12, you will not get 1x2 but 49x50.

Take a look at the ascii chart. http://www.asciitable.com/

Answer2:

Remove the curly braces from inner if block

#include <stdio.h> main() { int c,i; c = getchar(); i = 1; while (c!= '\n'){ if (c>=48 && c<=57) i = (c-'0')*i; c=getchar(); } printf("%d",i); }

Answer3:

You have to convert the characters that you have read into their corresponding numeric values. Since you are only using single digits, you can exploit/abuse the fact that the ASCII characters for '0' to '9' are in order (from 48 to 57).

Using a do { } while(), rather than while() { } obviates the need to “preset” c outside the loop.

The correct header for main is either int main(void) or int main(int argc, char *argv[]), and you should return a value from main.

The following should work, although I admit that I haven't compiled or tested it:

#include <stdio.h> int main(void) { int c; /* Character read */ int i = 1; /* Product */ do { c = getchar(); if (c>='0' && c<='9'){ i *= (c-'0'); } } while( c != '\n' ); printf("Product: %d\n",i); return 0; }

Answer4:

2 things to be taken care of : (1). Remove curly braces of if block. (2). Take care of conversion from ascii code to number. i = (c - '0') * i;

Recommend

  • Powershell Grouping of csv import file
  • Why is the compiler showing error “expected expression before stud”?
  • Why cout is producing no output on Code Blocks?
  • Access object instance inside an event handler
  • How to read piped content in C?
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Optimizing database types to compact database (SQLite)
  • How do I fake an specific browser client when using Java's Net library?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • How do you troubleshoot character encoding problems?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • QuartzCore.framework for Mono Develop
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Akka Routing: Reply's send to router ends up as dead letters
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Calling of Constructors in a Java
  • Circular dependency while pushing http interceptor
  • How do I rollback to a specific git commit
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • bootstrap to use multiple ng-app
  • Transpose CSV data with awk (pivot transformation)
  • How to get icons for entities from eclipse?
  • Linker errors when using intrinsic function via function pointer
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Turn off referential integrity in Derby? is it possible?
  • unknown Exception android
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • How to Embed XSL into XML
  • java string with new operator and a literal