1424555292.7254Lab2

advertisement
Lab 2 (Course 1)
1.
2.
Use Microsoft Visual Studio to compile and
Additional
exercise
on
variable
initialization:
statements (x=10; y=20;) from following program,
results!
run your first program
remove
the
assignement
compile and run. Explain
#include <stdio.h>
int main ()
{
int x,y;
x=10;
y=20;
printf ("%d %d\n", x,y);
return 0;
}
3.
Additional
exercise
on
printing
expression
values:
Change
program in the folowing two ways. Compile and run. Explain the results !
previous
#include <stdio.h>
int main ()
{
int x,y;
x=10;
y=20;
printf ("%d\n", x, x+1); // less format chars than expressions
return 0;
}
----------------------------#include <stdio.h>
int main ()
{
int x,y;
x=10;
y=20;
printf ("%d %d %d\n", x, x+1);//more format chars than expressions
return 0;
1/3
}
4.
Additional
exercise on printing multiple lines
of text: Write a C
program in order to display the following, by using ONLY ONE printf call !
*
***
*****
*******
solution
#include <stdio.h>
#include <conio.h>
int main()
{
printf(”\t\t\t *\n \t\t *** \n \t ****\n ******* \n") ;
getch();
return 0;
}
5. Write a program that prints the following text at the terminal.
1. In C, lowercase letters are significant.
2. main is where program execution begins.
3. Opening and closing braces enclose program statements in a routine.
4. All program statements must be terminated by a semicolon.
solution
#include <stdio.h>
#include <conio.h>
int main()
{
printf(”1. In C, lowercase letters are significant \n") ;
printf(”2. main is where program execution begins \n") ;
printf(”Opening and closing braces enclose program statements in a routine \n") ;
printf(”4. All program statements must be terminated by a semicolon \n") ;
getch();
return 0;
}
2/3
6. What output would you expect from the following program?
#include <stdio.h>
int main ()
{
printf ("Testing...");
printf ("....1");
printf ("...2");
printf ("..3");
printf ("\n");
return 0;
}
7. Write a program that subtracts the value 15 from 87 and displays the result, together with an
appropriate message, at the terminal.
solution
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c ;
a=15 ;
b=87 ;
c=b-a;
printf(”\t %d-%d=%d \n",b,a,c) ;
getch();
return 0;
}
8. Identify the syntactic errors in the following program. Then type in and run the corrected
program to ensure you have correctly identified all the mistakes.
#include <stdio.h>
int main ()
(
INT sum;
/* COMPUTE RESULT
sum = 25 + 37 - 19
/* DISPLAY RESULTS //
printf ("The answer is %d\n" sum);
return 0;
}
9. What output might you expect from the following program?
#include <stdio.h>
int main ()
{
int answer, result;
answer = 100;
3/3
result = answer - 10;
printf ("The result is %d\n", result + 5);
return 0;
}
4/3
Download