Exercise 1 Find the error(s) in each of the following program segments. Write in
ID: 3672693 • Letter: E
Question
Exercise 1 Find the error(s) in each of the following program segments. Write in a txt file the corrected commands a) printf ("The value is %d ", &number;); b) scanf("%d%d",&number1;, number2); c) printf ('Enter a value: d) int 3d; e) printf(%f, 123.879); Exercise 2 Write a program that prints the numbers 1 to 4 on the same line. Write the program using the following methods: a. Calling the function printf) once with four format specifiers. b. Calling the function printf) four times. Exercise 3 One mile equals 1609 meters. Write a program that a) Prompts the user to write his name and saves his name in a variable called name. eg.John b) Prompts the user to give a value of miles and saves the input in variable called miles. eg.1609 c) Converts the miles to kilometers. d) Prints the following message Hi John. The destination is 1 kmExplanation / Answer
Exercise 1
a) . Wrong statement:printf("The value is %d ",&number);
correct statement:printf("The value is %d ",number);
b. Wrong statement:scanf("%d%d",&number1,number2);
correct statement:scanf("%d%d",&number1,&number2);
c. Wrong statement:printf('enter a value');
correct statement:printf("enter a value");
d. Wrong statement:int 3d;
correct statement:int d;
e. Wrong statement:printf(%f,123.789);
correct statement:printf("%f",123.789);
Exercise 2:
a. printf("%d%d%d%d",1,2,3,4);
b. printf("1");
printf("2");
printf("3");
printf("4");
Exercise 3:
a. main() {
char name[20];
scanf("%s",name);
printf("%s",name);
}
b. int miles;
scanf("%d",&miles);
printf("%d",miles);
c. main()
{
float k,m;
printf("Enter miles");
scanf("%f",&m);
k=m*1.609;
printf("%f miles=%f kilometers",m,k);
}
d. printf("hi jhon");
printf("The destination is 1 km");
Exercise 4:
program determine the solutions of second order polynomial equation
#include <stdio.h>
#include <math.h>
main()
{
float a, b, c, determinant, x1,x2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
x1= (-b+sqrt(determinant))/(2*a);
x2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %f %f",x1 , x2);
}
else if (determinant==0)
{
x1 = x2 = -b/(2*a);
printf("Roots are: %f %f", x1, x2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %f%f%f%f", real, imag, real, imag);
}
}
output of the program is:
Enter coefficients a, b and c:1 -5 6
Roots are: 3.000000
2.000000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.