Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C Print Methods Write the following methods and test them in the

ID: 3815114 • Letter: P

Question

Programming in C Print Methods

Write the following methods and test them in the main:

int printC(int c); // Outputs the character c if it is printable


int printS(const char *s); // Outputs the string s


int printI(int i); // Outputs the integer i as a base-10 string


int printX(int i); // Outputs the integer i as a hexadecimal string


int printF(double x, int n); // Format: fff.ffff
Outputs x as a fixed point string with n digits to the right of the decimal point. Value should be
properly rounded. If n < 0, do not print the decimal point (otherwise the same as n = 0).


int printE(double x, int n); // Format: f.fffea (x = f.fff * 10^a)
Outputs x in exponential notation with exactly one non-zero digit to the left of the decimal
point (unless x = 0) and n digits to the right, a lowercase ‘e’, and the exponent. If n < 0, do not
print the decimal point (otherwise same as n = 0).

Explanation / Answer

#include <stdio.h>

int printC(int c) // Outputs the character c if it is printable
{
    printf(" character = %c",c);
}


int printS(const char *s) // Outputs the string s
{
    printf(" String = %s",s);
}


int printI(int i) // Outputs the integer i as a base-10 string
{
    printf(" Integer as base 10 = %d",i);
}


int printX(int i) // Outputs the integer i as a hexadecimal string
{
    printf(" Integer as hexadecimal string = %0x",i);
}


//Outputs x as a fixed point string with n digits to the right of the decimal point. Value should be
//properly rounded. If n < 0, do not print the decimal point (otherwise the same as n = 0).

int printF(double x, int n) // Format: fff.ffff
{
    if(n<0)
     printf(" Fixed Format Decimal Number = %f",x);
     else
  
    printf(" Fixed Format Decimal Number = %7.*f",n,x);
}

int printE(double x, int n) // Format: f.fffea (x = f.fff * 10^a)
{
    printf(" Exponential Format Decimal Number = %.*e",n,x);
}

int main(void)
{
   printC(65); // Outputs the character c if it is printable

     printS("Computer"); // Outputs the string s

      printI(567); // Outputs the integer i as a base-10 string


      printX(567); // Outputs the integer i as a hexadecimal string
    printF(45.67676879,4); // Format: fff.ffff


        printE(45.6769,3); // Format: f.fffea (x = f.fff * 10^a)


   return 0;
}


output: