Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Reading Input in a C program

YASH PAL, 29 December 202228 June 2025

To learn about the reading or receiving input from users in the c program we will take an example program and then understand it.

Let’s look at the below-given program. if we assumed the values of p, n, and r to be 1000, 3, and 8.5. then every time we run the program we would get the same value for simple interest. if we want to calculate simple interest for some other set of values we are required to make the relevant change in the program, and again compile and execute it.

Thus the program needs to be more general to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able to make changes in the program Hence it is a good practice to create a program that is general enough to work for any set of values.

To make the program general the program itself should ask the user to supply the values of p, n, and r through the keyboard during execution. this can be achieved using a function called scanf(). this function is a counterpart of the printf() function. printf() outputs the values to the screen whereas scanf() receives them from the keyboard. this is illustrated in the program shown below.

/*Calculate the simple interest */
main()
{
	int p, n;
	float r,si;
	printf("Enter values of p, n, r");
	scanf("%d %d %f", &p,&n,&r);
	si = p * n * r / 100;
	printf("%f",si);
}

The first printf() outputs the message ‘ Enter values of p, n, r ‘ on the screen. here we have not used any expression in printf() which means that using the expression in printf() is optional.

Note that the ampersand (&) before the variables in the scanf() function is a must. & is an ‘Address of’ operator. it gives the location number used by the variable in memory. when we say &a, we are telling scanf() at which memory location should it store the value supplied by the user from the keyboard.

Note that a blank, a tab, or a new line must separate the values supplied to scanf(). note that a blank is created using a spacebar, a tab using the Tab key, and a new line using the Enter key. this is shown below.

Example – The three values are separated by blank

1000 5 15.5

Example – The three values are separated by a tab.

1000 5  15.5

Example – The three values are separated by newline

1000

5

15.5

Let’s take another example that receives an integer input from the user.

main()
{
	int num;
	printf("Enter a number");
	scanf("%d",&num);
	printf("Now I am letting you on a secret...");
	printf("You have just entered the number %d", num);
}

Read other tutorials

  • C programming interview questions/answers
  • C programming tutorials
c Computer Science Tutorials ccomputer science

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes