C programming Homework

+1 vote
asked May 3 by reda cocelli (190 points)

use ONLY “stdio.h” library

Write two functions in a single program. After writing following functions, call them from main function to test if they are working correctly (how functions will be tested depends on programmer).

 i) Write a function that will receive two integer variables (x,y) as parameter. This function will find and show on screen all odd numbers between x and y (both included and order of x and y, while sending them to defined function, is not important).

 ii) Write a function that will perform following mathematical expression (note that, x, y and z are integer values), and return result.  

2 Answers

0 votes
answered May 10 by Afloarei (280 points)
#include<stdio.h>
int x, y, z;
int main(){
 scanf(x);
 scanf(y);
 if(x>y)
  return;
 for(z=x;z<y;z++)
  {
   if(z%2!=0)
    printf(z);
   }
   return 0;
}
//I did not meet all the requirements (I used only one function) but I hope I am helpful. Here is a separate example with the second function:
int soli (int a);
{
  int b;
  b = a + 1;
  return b;
} 
commented May 10 by Afloarei (280 points)
is it correct?
0 votes
answered May 10 by LiOS (4,360 points)
#include <stdio.h>

void oddNumbers(int a, int b){
 
  printf("**oddNumbers function**\n");
  printf("Odd numbers between %d and %d are: ", a, b);

    if (a < b){
        
        for(int counter = a; counter <= b; counter++){
            if(counter % 2 != 0)
            {
                printf("%d ", counter);
            }
        }
    }
    else{
      
        for(int counter = b; counter <= a; counter++){
            if(counter % 2 != 0){
                printf("%d ", counter);
            }
        }
    }

}

int mathematicalExpression(int a, int b){
    
  printf("**mathematicalExpression function**\n");

  char operator;

  printf("Please enter an operator (+, -, * or /): ");
  //operator = getchar();
  scanf(" %c", &operator);

  switch(operator)
    {
    case '+':
      return ("%d", a + b);
      break;
    case '-':
      return ("%d", a - b);
      break;
    case '*':
      return("%d", a * b);
      break;
    case '/':
      return("%d", a / b);
      break;
    default:
      printf ("Error! operator is not correct");
    }
}

int main(){
    
  int x, y;

  printf("Please enter integer x: ");
  scanf("%d", &x);

  printf("\nPlease enter integer y: ");
  scanf("%d", &y);

  oddNumbers(x, y);
  printf("\n\n");
  int result = mathematicalExpression(x, y);
  printf("%d", result);
 
  return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...