Skip to main content

User input and Conditional statements

User input and Conditional statements

In this part you will learn:
1. Taking input from user
2. Conditional statements
3. C syntax
4. Showing output
5. Tabs and new lines

In this tutorial I will teach you how take input from user and how to use conditional statements.

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>

These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.

  1. int main(){
  2. int number;
  3. char alphabet;

First of all we will declare an integer and an alphabet. In this program we will only use these variables to take input and we will simply print these variables just to check whether the program is taking correct input or not.

  1. printf("Enter a number :");
  2. scanf("%d",&number); // scanf takes input remember ampersand sign &
  3. printf("The number you typed is: %d ",number);
  4. printf("\n\nWhat is the first letter of your name ? ");
  5. scanf(" %c",&alphabet); // scanf takes input remember ampersand sign &
  6. printf("The first letter of your name is : %c",alphabet);
  7. getch();
  8. return 0; // write now just write it as it is
  9. }//end main

Now writing the print statements first and then we write the scanf function which is used to take the input from the user. In scanf function we first write the type of variable in which we are taking input by using the format specifier i.e incase of integer is %d and incases of character is %c and then we used the & symbol with our variable name this is to give compiler the address of the variable in memory , you will understand it later just write this & symbol as it is for now. In the end we just pause the screen using getch function and return 0 ( write it as it is for now ) and our program is complete.
Execute > compile
then Execute > run


Using Conditional statements
File > new source file
The if , else and else if statements in c are called conditional statements. Now we will write a program to compare two numbers using conditional statements.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. int main(){
  4. int num1,num2; //declaring two numbers named num1 and num2
  5. printf("Enter number1 :");
  6. scanf("%d",&num1); // scanf takes input remember ampersand sign &
  8. printf("Enter number2 :");
  9. scanf("%d",&num2); // scanf takes input remember ampersand sign &

We will first write the header files and then start our main function. In the main function we declare two variables for number1 and number 2 and take user inputs in them. Then we start writing the conditional statements
  1. if(num1 > num2) // if number1 is greater than number 2
  2. printf("number1 is greater than number 2");
  3. if(num1 < num2) // if number1 is less than number 2
  4. printf("number2 is greater than number 1");
  5. else // 3rd possiblity number 1 is equal to number 2
  6. printf("Both numbers are equal");
  7. getch();
  8. return 0; // write now just write it as it is
  9. }//end main

There are three possibilities that a number is either greater, smaller or equal so we will write the conditional statements for all these possibilities. When number is greater, when it is smaller and if both possibilities are false then we go to else that both numbers will be equal then. After that we pause the screen and return.
Execute > compile
then Execute > run


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.