By Nirmal Priyankara.
C language simple projects
Calculator
This calculator is a simple one. using this calculator we can do addition, subtraction, multiplication, and division of two numbers. First get try to build it yourself.
Output:
Code:
#include <stdio.h>
int main()
{
double num1;
double num2;
char operator;
printf("Enter first number: ");
scanf("%lf",&num1);
printf("Enter operator(+ - * /): ");
scanf(" %c",&operator);
printf("Enter second number: ");
scanf("%lf",&num2);
if (operator == '+') {
printf("Number1 + Number2:%f", num1 + num2);
}
else if (operator == '-') {
printf("Number1 - Number2: %f", num1 - num2);
}
else if (operator == '*') {
printf("Number1 * Number2: %f", num1 * num2);
}
else if (operator == '/') {
printf("Number1 / Number2: %f", num1 / num2);
}
else {
printf("Something Wrong, Try again!");
}
}
Guessing game
This game is also easy to program using C. This game has a default secret number lower than 10. So, we have to guess that number. We have three times to guess that number. After that if you could not guess that number game over. First, Try to develop it yourself.
Output:
Code:
#include <stdio.h>
int main()
{
int secretNumber = 5;
int guessNumber = 0;
int maxGuesstimes = 3;
int count = 0;
while (secretNumber != guessNumber && maxGuesstimes > count)
{
printf("Enter your gessed number: ");
scanf("%d", &guessNumber);
count++;
}
if (secretNumber != guessNumber)
{
printf("You have no more times. Try again!");
}
else
{
printf("You Won!");
}
return 0;
}
Mad libs game
Mad Libs game is a very funny game. As I think some of you already know. However, Here we give a chance player to input strings( like pronouns, colors, celebrities, animals and so on). We have already some sentences also(these are not visible to the player). So, We will change those sentences using user inputs. After that Final answers will be displayed. some times those outputs are very funny. So, Try to build it.
Output:
Code:
#include <stdio.h>
int main()
{
char pluralNoun[20];
char color[20];
char celebrity[20];
char animal[20];
printf("Enter a plural noun: ");
scanf("%s", pluralNoun);
printf("Enter a color: ");
scanf("%s", color);
printf("Enter a celebrity: ");
scanf("%s", celebrity);
printf("Enter an animal name: ");
scanf("%s",animal);
printf("\n%s are blue.\n", pluralNoun);
printf("Roses are %s.\n", color);
printf("I love %s.\n", celebrity);
printf("%s is very innocent.\n", animal);
return 0;
}
Summary: These are very basic programs in C. If you are a beginner in C programming follow this article and get experience with how to use C useful and check your basic knowledge with C.
Comments
Post a Comment