Solution1

advertisement
Exercise 1: Write a C program to print the following line
as shown below:
Welcome!
You are able to test your skill of writing C code here.
Solution:
#include <stdio.h>
void main()
{
printf(" Welcome!\n");
printf(" You are able to test your skill of writing C code here.\n");
}
Exercise 2: Write five statements by using printf function
to print the asterisk pattern:
*****
*****
*****
*****
*****
Solution:
#include <stdio.h>
void main()
{
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
}
Exercise 3: Write a C program to declare two integer and one float
variables then initialize them to 10, 15, and 12.6. It then prints these
values on the screen.
#include <stdio.h>
void main()
{
int x;
int y;
float z;
x=10;
y=15;
z=12.6;
printf("x= %d",x);printf("\t");
printf("y=%d",y);printf("\t");
printf("z=%f",z);
printf("\n");
}
Exercise 4: Write a C program to prompt the user to input her/his
age and print it on the screen, as shown below.
Your age is 20 years old.
Solution:
#include <stdio.h>
void main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
printf("You are %d ",age);
printf(" years old.\n");
}
Exercise 5: Write a C program to prompt the user to input 3 integer
values and print these values in forward and reversed order, as
shown below.
Please enter your 3 numbers: 12 45 78
Your numbers forward:
12
45
78
Your numbers reversed:
78
45
12
Solution:
#include <stdio.h>
void main()
{
int val1;
int val2;
int val3;
printf("Please enter your 3 numbers:");
scanf("%d %d %d",&val1,&val2,&val3);
printf("\nYour numbers forward:\n");
printf("%d",val1);printf("\n");
printf("%d",val2);printf("\n");
printf("%d",val3);printf("\n");
printf("Your numbers reversed:\n");
printf("%d",val3);printf("\n");
printf("%d",val2);printf("\n");
printf("%d",val1);printf("\n");
}
Given the following pseudo code, write a
program that executes it.
Exercise 6:
a.
b.
c.
d.
e.
f.
read x
read y
compute p=x*y
compute s=x+y
total=s2+p*(s-x)*(p+y)
print total Solution:
#include <stdio.h>
void main()
{
float x;
float y;
float p;
float s;
float total;
printf("Enter x value:");
scanf("%f",&x);
printf("\n");
printf("Enter y value:");
scanf("%f",&y);
printf("\n");
p=x*y;
s=x+y;
total=s*s+p*(s-x)*(p+y);
printf("Total:%f",total);
printf("\n");
}
Download