C programming :
C variables and datatypes exercises:
C variables and datatypes exercises:
Exercise 1: Write a C program to print the following lines:
You are 10 years old.
You are too young to play the game
You are too young to play the game
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int age;
age=10;
printf(" You are %2d ",age);printf("years old.\n");
printf(" You are too young to play the game.\n");
system("PAUSE");
return 0;
}
#include <stdlib.h>
int main(int argc, char *argv[])
{
int age;
age=10;
printf(" You are %2d ",age);printf("years old.\n");
printf(" You are too young to play the game.\n");
system("PAUSE");
return 0;
}
Exercise 2: Write five statements to print the asterisk pattern:
*****
*****
*****
*****
*****
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
system("PAUSE");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
printf("*****\n");
system("PAUSE");
return EXIT_SUCCESS;
}
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.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
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=%3.1f",z);
printf("\n");
system("PAUSE");
return 0;
}
#include <stdlib.h>
int main(int argc, char *argv[])
{
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=%3.1f",z);
printf("\n");
system("PAUSE");
return 0;
}
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>
#include <stdlib.h>
int main
{
int age;
printf("Enter your age:");
scanf("%d",&age);
printf("You are %2d ",age);printf(" years old.\n");
system("PAUSE");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main
{
int age;
printf("Enter your age:");
scanf("%d",&age);
printf("You are %2d ",age);printf(" years old.\n");
system("PAUSE");
return 0;
}
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>
#include <stdlib.h>
int main(int argc, char *argv[])
{
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");
system("PAUSE");
return 0;
}
C exercises - conditional statements:
Example:
Enter 3 integer vales separated by space: 10 15 20
The greatest value is: 20
Solution:
#include <stdio.h>
#include <stdlib.h>
int main
{
int x, y, z, max;
printf("Enter 3 integer vales separated by space:");
scanf("%d %d %d",&x,&y,&z);
max=x; if(max<y) max=y; if(max<z) max=z; printf("The max is %d.",max);
printf("\n");
system("PAUSE");
return 0;
}
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float x;
float y;
float z;
float avg;
printf("Enter 3 score vales separated by space:");
scanf("%f %f %f",&x,&y,&z);
avg=(x+y+z)/3;
if(avg>=90)printf("Grade A");
else if((avg>=70) && (avg<90)) printf("Grade B");
else if((avg>=50) && (avg<70))printf("Grade C");
else if(avg<50) printf("Grade F");
else printf("Invalid");
printf("\n");
system("PAUSE");
return 0;
}
*******
******
*****
****
***
**
*
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
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");
system("PAUSE");
return 0;
}
C exercises - conditional statements:
1. Write a C code that prompts the user to input tree integer values and find the greatest value of the three values.
Example:
Enter 3 integer vales separated by space: 10 15 20
The greatest value is: 20
Solution:
#include <stdio.h>
#include <stdlib.h>
int main
{
int x, y, z, max;
printf("Enter 3 integer vales separated by space:");
scanf("%d %d %d",&x,&y,&z);
max=x; if(max<y) max=y; if(max<z) max=z; printf("The max is %d.",max);
printf("\n");
system("PAUSE");
return 0;
}
2. Write a program that determines a student’s grade. The program will read three scores and determine the grade based on the following rules:
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float x;
float y;
float z;
float avg;
printf("Enter 3 score vales separated by space:");
scanf("%f %f %f",&x,&y,&z);
avg=(x+y+z)/3;
if(avg>=90)printf("Grade A");
else if((avg>=70) && (avg<90)) printf("Grade B");
else if((avg>=50) && (avg<70))printf("Grade C");
else if(avg<50) printf("Grade F");
else printf("Invalid");
printf("\n");
system("PAUSE");
return 0;
}
Loops in C exercises:
1. Write a C program that will print the following pattern
*******
******
*****
****
***
**
*
Solution:
#include <stdio.h>
#include <stdlib.h>
int main
{
int i=7;
int j=7;
for(i=1;i<=7;i++){
for(j=7-i;j>=1;--j)printf("*");
printf("\n");
}
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int main
{
int i=7;
int j=7;
for(i=1;i<=7;i++){
for(j=7-i;j>=1;--j)printf("*");
printf("\n");
}
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
2. Write a C program that will print the following pattern:
1******
12*****
123****
1234***
12345**
123456*
1234567
Solution:
#include <stdio.h>
#include <stdlib.h>
int main
{
int i;
int j;
int k;
for(i=1;i<=7;i++){
for(j=1;j<=i;++j)
cout<<j;
for(k=7-i;k>=1;k--) printf("*");
printf("\n");
}
printf("\n");
system("PAUSE");
return EXIT_SUCCESS; }
0 comments:
Post a Comment