What are the errors in the following programs:

(a)

main(){printf ”Once upon a time,”; print “Ten times”}


(b)

#include <math.h>
main(){
printf(Enter an integer number:);
scanf(“%d”,&X);
printf(“X to the forth power is:%d”,pow(X,4));
}

Errors in Program (a)

  • #include<stdio.h>
    is missing hence the function printf is undeclared.
  • The open and closed bracket is missing in function printf. It should be
    printf("Once upon a time,");
  • The function print is undeclared and is missing the open and closed bracket, as well as a semi column at the end.
  • The ideal program should be:
#include<stdio.h>
main(){printf ("Once upon a time,"); printf ("Ten times");}

Errors in Program (b)

  • Double quotes are missing in the printf function. The correct implementation should be
    printf("Enter an integer number:");
  • The variable X in scanf function is undeclared.
    int X
    should be added after the main function.
  • The standard input/output library stdio.h is missing which leads to implicit declaration of built-in function printf and scanf
  • The function pow returns a double so %d in the last printf should be changed to %f to get correct results. A call such as pow(x,y) returns xy. A domain error occurs if x=0 and y<=0, or if x<0 and y is not an integer.
  • A working program should be:
#include <stdio.h>
#include <math.h>
main(){
int X;
printf("Enter an integer number:");
scanf("%d",&X);
printf("X to the forth power is:%.1f", pow(X,4));
}
  • The header
    <math.h>
    declares mathematical functions and macros.

If you find this tutorial useful, be sure to share it with your fellow friends via Facebook, Twitter, Google Plus or any of your social media forums.

Add comment


Security code
Refresh