# K & R C Programs Exercise 5-1.

K and R C, Solution to Exercise 5-1:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to get next integer from input into space, so that getint treats a + or – not followed by a digit as a valid representation of zero and fix it to push such a character back on the input. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/include<ctype.h>int getch(void);void ungetch(int);/* getint:  get next integer from input into *pn */int getint(int *pn){ int c, sign, sawsign; while (isspace(c = getch()))   /* skip white space */  ; if (!isdigit(c) && c != EOF && c != '+' && c != '-') {  ungetch(c);    /* it's not a number */  return 0; } sign = (c == '-') ? -1 : 1; if (sawsign = (c == '+' || c == '-'))  c = getch(); if (!isdigit(c)) {  ungetch(c);  if (sawsign)   ungetch((sign == -1) ? '-' : '+');  return 0; } for (*pn = 0; isdigit(c); c = getch())  *pn = 10 * *pn + (c - '0'); *pn *= sign; if (c != EOF)  ungetch(c); return c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 4-11.

K and R C, Solution to Exercise 4-11:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-3, Modify the gettop function so that it doesn’t need to use ungetch by using internal static variable. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdio.h>#include<ctype.h>#define NUMBER '0'int getch(void);/* getop: get next operator or numeric operand. */int getop(char s[]){ int i ; int c; static int lastc = 0; if(lastc == 0)  c = getch(); else{  c = lastc;  lastc = 0; } while((s[0] = c) == ' ' || c == 't')  c = getch(); s[1] = ''; if(!isdigit(c) && c != '.')  return c;               i = 0; if(isdigit(c))  while(isdigit(s[++i] = c = getch()))   ; if(c == '.')  while(isdigit(s[++i] = c = getch()))   ; s[i] = ''; if(c != EOF)  lastc = c; return NUMBER;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# C Program to find sum and product using macros.

Write C Program to find sum and product using macros.
Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdio.h>#define SUM(A, B) (A + B)#define PROD(A, B) (A * B)int main(){ int num1, num2, sum, product; printf("nEnter the Two numbersn"); scanf("%d%d",&num1,&num2); sum=SUM(num1,num2); product=PROD(num1,num2); printf("nnSum of two numbers using Macros is:%dn",sum); printf("nnProduct of two numbers using macros is:%dn",product); return 0;}`
`Read more Similar C ProgramsLearn C ProgrammingNumber System`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 4-10.

K and R C, Solution to Exercise 4-10:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-5, An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. revise the caluculater to use this aproach. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdio.h>#include<ctype.h>#define MAXLINE 100#define NUMBER '0' //SIGNAL THAT A NUMBER WAS FOUNDint getline(char line[], int limit);int li = 0;char line[MAXLINE];/* Getop: get next operator or numeric operand. */int Getop(char s[]){ int i; int c; if(line[li] == '')  if(getline(line, MAXLINE) == 0)   return EOF;  else li = 0; /* Skip whitespace */ while((s[0] = c = line[li++]) == ' ' || c == 't') {  ; } s[1] = ''; /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' )  return c; i = 0; if(isdigit(c))  while(isdigit(s[++i] = c =line[i++]))   ; if(c == '.')  while(isdigit(s[++i] = c =line[i++]))   ; s[i] = ''; li--; return NUMBER;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 4-8.

K and R C, Solution to Exercise 4-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-7, Suppose there will never be more than one character of pushback. Modify getch and ungetch accordingly. Read more about C Programming Language .

```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

#include <stdio.h>
char buf = 0;

//getch: get a (possible pushed character back) character
int getch(void)
{
int c;
if(buf != 0)
c = buf;
else
c getchar();
buf = =0;
return c;
}

/*ungetch: push string back onto the input*/
void ungetch(int c)
{
if (buf != 0)
printf("ungetch: too many characters!n");
else
buf = c;
}
int main(void)
{
int c;

while ((c = getch()) != EOF) {
if (c == '/') {
putchar(c);
if ((c = getch()) == '*') {
ungetch('!');
}
}
putchar(c);
}
return 0;
}
```
```Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise```

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

Â our page!

Â (you can send this program to your friend using this button)

To browse more C Programs visit this link

(c) www.c-program-example.com

# K & R C Programs Exercise 4-5.

K and R C, Solution to Exercise 4-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program to access to library functions like sin, exp, and pow. See “math.h” for more details.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdlib.h>#include<stdio.h>#include<ctype.h>#include<math.h>#include <string.h>#define MAXOP 100#define NUMBER       0#define IDENTIFIER   1#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();void mathfnc(char s[]);int main(void){ int type; double op2; char s[MAXOP]; int flag = TRUE; while((type = Getop(s)) != EOF) {  switch(type)  {  case NUMBER:   push(atof(s));   break;  case IDENTIFIER:   mathfnc(s);   break;  case '+':  push(pop() + pop());  break;  case '*':   push(pop() * pop());   break;  case '-':   op2 = pop();   push(pop()- op2);   break;  case '/':   op2 = pop();   if(op2)    push(pop() / op2);   else    printf("nError: division by zero!");   break;  case '%':   op2 = pop();   if(op2)    push(fmod(pop(), op2));   else    printf("nError: division by zero!");   break;  case '?':   showTop();   break;  case '#':   duplicate();   break;  case '~':   swapItems();   break;  case '!':   clearStack();  case 'n':   printf("nt%.8gn", pop());   break;  default:   printf("nError: unknown command %s.n", s);   break;  } } return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;         double val[MAXVAL]; /* push: push f onto stack. */void push(double f){ if(sp < MAXVAL)  val[sp++] = f; else  printf("nError: stack full can't push %gn", f);}/*pop: pop and return top value from stack.*/double pop(void){ if(sp > 0)  return val[--sp]; else {  printf("nError: stack emptyn");  return 0.0; }}void showTop(void){ if(sp > 0)  printf("Top of stack contains: %8gn", val[sp-1]); else  printf("The stack is empty!n");}void duplicate(void){ double temp = pop(); push(temp); push(temp);}void swapItems(void){ double item1 = pop(); double item2 = pop(); push(item1); push(item2);}void clearStack(void){ sp = 0;}/*check string s for supported math functions */void mathfnc(char s[]){ double op2; if( 0 == strcmp(s, "sin"))  push(sin(pop())); else if( 0 == strcmp(s, "cos"))  push(cos(pop())); else if (0 == strcmp(s, "exp"))  push(exp(pop())); else if(!strcmp(s, "pow")) {  op2 = pop();  push(pow(pop(), op2)); } else  printf("%s Error: is not a supported function.n", s);}int getch(void);void unGetch(int);/* Getop: get next operator or numeric operand. */int Getop(char s[]){ int i = 0; int c; int next; /* Skip whitespace */ while((s[0] = c = getch()) == ' ' || c == 't')  ; s[1] = ''; if(isalpha(c)) {  i = 0;  while(isalpha(s[i++] = c ))   c = getch();       s[i - 1] = '';  if(c != EOF)   unGetch(c);  return IDENTIFIER; } /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' && c != '-')  return c;                  if(c == '-') {  next = getch();  if(!isdigit(next) && next != '.')  {   return c;  }  c = next; } else  c = getch(); while(isdigit(s[++i] = c))  c = getch(); if(c == '.')                 /* Collect fraction part. */  while(isdigit(s[++i] = c = getch()))   ; s[i] = ''; if(c != EOF)  unGetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void){ return (bufp > 0) ? buf[--bufp]: getchar();}void unGetch(int c){ if(bufp >= BUFSIZE)  printf("nUnGetch: too many charactersn"); else  buf[bufp++] = c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# C Program to add one to digits of a number

Write C Program to add one to digits of a number.
C Program that adds the 1 to each single digit of a number, i.e for Example 12345’s output is 23456.
If the digit is 9 it adds 1 and follows the carry system, 9 becomes 0 and 9’s left digit adds one more 1. I.e., 3491’s output is 4602. Read more about C Programming Language .

```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int main()
{
int num, sum = 0;
int rem, check = 0;
clrscr( );
printf("Enter the required number:");
scanf("%d",&num);
printf("nGiven Number: %d",num);
while(num>0)
{
rem = num % 10;
if(rem != 9)
{
if(check == 0)
sum = (10 * sum) + (rem + 1);
else{
sum = (10*sum) + (rem + 2);
check = 0;
}
}
else{
sum = (10 * sum) + 0;
check = 1;
}
num = num/10;
}

num = sum; sum=0;
while(num > 0)
{
rem = num % 10;
sum = (10*sum) + rem;
num = num / 10;
}
getch( );
return 0;
}```
```Read more Similar C Programs
Learn C Programming
Number System```

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now, our page!

(c) www.c-program-example.com

# K & R C Programs Exercise 4-4.

K and R C, Solution to Exercise 4-4:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C program to implement the following:
Show the top item of the stack without permanently popping it.
Swap the top two items on the stack.
Duplicate the top item on the stack.
Clear the stack, with the previous cases i. e K and R C Programs Exercise 4-3. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include "stdlib.h"#include"stdio.h"#include "ctype.h"#include "math.h"#define MAXOP 100#define NUMBER  0#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();int main(void){ int type; double op2; char s[MAXOP]; int flag = TRUE; while((type = Getop(s)) != EOF) {  switch(type)  {  case NUMBER:   push(atof(s));   break;  case '+':   push(pop() + pop());   break;  case '*':   push(pop() * pop());   break;  case '-':   op2 = pop();   push(pop()- op2);   break;  case '/':   op2 = pop();   if(op2)    push(pop() / op2);   else    printf("nError: division by zero!");   break;  case '%':   op2 = pop();   if(op2)    push(fmod(pop(), op2));   else    printf("nError: division by zero!");   break;  case '?':   showTop();   break;  case '#':   duplicate();   break;  case '~':   swapItems();   break;  case '!':   clearStack();  case 'n':   printf("nt%.8gn", pop());   break;  default:   printf("nError: unknown command %s.n", s);   break;  } } return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;          /* Next free stack position. */double val[MAXVAL];  /* value stack. *//* push: push f onto stack. */void push(double f){ if(sp < MAXVAL)  val[sp++] = f; else  printf("nError: stack full can't push %gn", f);}/*pop: pop and return top value from stack.*/double pop(void){ if(sp > 0)  return val[--sp]; else {  printf("nError: stack emptyn");  return 0.0; }}void showTop(void){ if(sp > 0)  printf("Top of stack contains: %8gn", val[sp-1]); else  printf("The stack is empty!n");}void duplicate(void){ double temp = pop(); push(temp); push(temp);}void swapItems(void){ double item1 = pop(); double item2 = pop(); push(item1); push(item2);}/* pop only returns a value if sp is greater than zero. So setting thestack pointer to zero will cause pop to return its error */void clearStack(void){ sp = 0;}int getch(void);void unGetch(int);/* Getop: get next operator or numeric operand. */int Getop(char s[]){ int i = 0; int c; int next; /* Skip whitespace */ while((s[0] = c = getch()) == ' ' || c == 't')  ; s[1] = ''; /* Not a number but may contain a unary minus. */ if(!isdigit(c) && c != '.' && c != '-')  return c;                if(c == '-') {  next = getch();  if(!isdigit(next) && next != '.')  {   return c;  }  c = next; } else  c = getch(); while(isdigit(s[++i] = c))  c = getch(); if(c == '.')                        /* Collect fraction part. */  while(isdigit(s[++i] = c = getch()))   ; s[i] = ''; if(c != EOF)  unGetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;/* Getch: get a ( possibly pushed back) character. */int getch(void){ return (bufp > 0) ? buf[--bufp]: getchar();}/* unGetch: push character back on input. */void unGetch(int c){ if(bufp >= BUFSIZE)  printf("nUnGetch: too many charactersn"); else  buf[bufp++] = c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 4-2.

K and R C, Solution to Exercise 4-2:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C Program to extend the atof function to handle scientific notations of the form 5234.73e-12
atof function converts the intial nptr string to the double. atof means ASCII to float. In this program that atof function handles the scientific notations also like 12.e-3.. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <ctype.h>#include <limits.h>#include <float.h>#include <signal.h>#include <stdio.h>int my_atof(char *string, double *pnumber) { /* Convert char string to double data type. */ double retval; double one_tenth = 0.1; double ten = 10.0; double zero = 0.0; int found_digits = 0; int is_negative = 0; char *num; /* Check pointers. */ if (pnumber == 0) {  return 0; } if (string == 0) {  *pnumber = zero;  return 0; } retval = zero; num = string; /* Advance past white space. */ while (isspace(*num))  num++; /* Check for sign. */ if (*num == '+')  num++; else if (*num == '-') {  is_negative = 1;  num++; } /* Calculate the integer part. */ while (isdigit(*num)) {  found_digits = 1;  retval *= ten;  retval += *num - '0';  num++; } /* Calculate the fractional part. */ if (*num == '.') {  double scale = one_tenth;  num++;  while (isdigit(*num)) {   found_digits = 1;   retval += scale * (*num - '0');   num++;   scale *= one_tenth;  } } /* If this is not a number, return error condition. */ if (!found_digits) {  *pnumber = zero;  return 0; } /* If all digits of integer & fractional part are 0, return 0.0 */ if (retval == zero) {  *pnumber = zero;  return 1; /* Not an error condition, and no need to   * continue. */ } /* Process the exponent (if any) */ if ((*num == 'e') || (*num == 'E')) {  int neg_exponent = 0;  int get_out = 0;  long index;  long exponent = 0;  double getting_too_big = DBL_MAX * one_tenth;  double getting_too_small = DBL_MIN * ten;  num++;  if (*num == '+')   num++;  else if (*num == '-') {   num++;   neg_exponent = 1;  }  /* What if the exponent is empty?  Return the current result. */  if (!isdigit(*num)) {   if (is_negative)    retval = -retval;   *pnumber = retval;   return (1);  }  /* Convert char exponent to number <= 2 billion. */  while (isdigit(*num) && (exponent < LONG_MAX / 10)) {   exponent *= 10;   exponent += *num - '0';   num++;  }  /* Compensate for the exponent. */  if (neg_exponent) {   for (index = 1; index <= exponent && !get_out; index++)    if (retval < getting_too_small) {     get_out = 1;     retval = DBL_MIN;    } else     retval *= one_tenth;  } else   for (index = 1; index <= exponent && !get_out; index++) {    if (retval > getting_too_big) {     get_out = 1;     retval = DBL_MAX;    } else     retval *= ten;   } } if (is_negative)  retval = -retval; *pnumber = retval; return (1);}double atof(char *s) { double d = 0.0; if (!my_atof(s, &d)) {#ifdef DEBUG  fputs("Error converting string in [sic] atof()", stderr);#endif  raise(SIGFPE); } return d;}#ifdef UNIT_TESTchar *strings[] = { "1.0e43", "999.999", "123.456e-9", "-1.2e-3", "1.2e-3", "-1.2E3", "-1.2e03", "cat", "", 0};int main(void){ int i = 0; for (; *strings[i]; i++) printf("atof(%s) = %gn", strings[i], atof(strings[i])); return 0;}#endif`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 4-1.

K and R C, Solution to Exercise 4-1:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program which returns the position of the occurrence of sub string t in string s, or -1 if there is none.

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact info@c-program-example.com* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include<stdio.h>#include<conio.h>void main(){ int flag=0; char str[80],search[10]; puts("Enter a string:"); gets(str); puts("Enter search substring:"); gets(search); flag=strindex(str, search); if (flag == -1) {  printf("SEARCH UNSUCCESSFUL! AND POSITION IS:%d",flag); } else{  printf("SEARCH SUCCESSFUL! AND POSITION IS:%d",flag);  } getch();}//strindex: returns the right most index of t in s, -1 if none*/int strindex(char s[], char t[]){ int k,i,j,pos; pos = -1; for(i=0;s[i] != ''; i++) {  for(j=i, k = 0; t[k] != '' && s[j] == t[k]; j++, k++)   ;  if (k > 0 && t[k] == '')   pos=i; }  return pos;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.