Scanf ("Control String", &var1, &var2... &varn)
Scanf ("Control String", &var1, &var2... &varn)
Scanf ("Control String", &var1, &var2... &varn)
scanf() function is used to accept the input from the user through keyboard.
Syntax
scanf(“control string”, &var1, &var2... &varN);
Variable name is prefixed with & (ampersand) symbol. It is known as “Address Of”
operator and specifies a memory location to the scanf function where the value
associated with variable will be stored in the computer’s memory. The “&” symbol in
scanf() is mandatory.
Note: Missing the ampersand in scanf() is the most common C Programming error
because in this case, the compiler will not produce any compile time error.
Control String: It contains field specifications which direct the interpretation of input data.
It consists of conversion character % and type specifier. It is written in double quotes.
Various type specifiers are listed below. Commonly used control strings are:
%d integer
%f float
%c char
The if statement enables us to test for a condition (such as whether two variables are
equal) and branch to different parts of the code, depending on the result of the
conditions constructed using relational and logical. The simplest form is the following:
Test expression can either have a relational operator(&&, ||, ! ) or a logical operator (==,
>, <, >=, <= != ) but eventually evaluate to a boolean value, true (represented as non-
zero value) or false (represented as 0).
The if..else statement enables us to test for a condition (such as whether two
variables are equal) and branch to different parts of the code, depending on the result of
the conditions constructed using relational and logical. The simplest form is the
following:
IV. Problems:
1. Try to achieve the following design using printf(). Notice the space between the
characters.
#
# #
# # #
# # # #
Code:
#include<stdio.h>
int main()
{
printf(“#\n”);
printf(“#\t#\n”);
printf(“#\t#\t#\n”);
printf(“#\t#\t#\t#\n”);
return 0;
}
2. Given values a, b, c and x, write a program to compute and display the value of the
following equation.
y = ax3 + bx2 + cx + 5
Code:
#include<stdio.h>
#include<math.h>
int main()
{
int a = 2 ,b = 3,c = 4,x = 2,y;
y = a*pow(x,3) + b*pow(x,2) + c*x + 5;
printf("%d", y);
return 0;
}
3. Write a program to take 2 integers as input and print the largest of the two. Consider that
the values of 2 integers are different. Your program should only use if conditional
statements.
#include<stdio.h>
int main()
{
int num1, num2;
printf(“Enter two integers:\n”);
scanf("%d",&num1);
scanf("%d",&num2);
if(num1 > num2)
{
printf("%d\n",num1);
}
return 0;
}
4. Write a program to take 2 floating point numbers as input and print if the two numbers
are equal or less than one another or greater than one another. Your program should
use nested if..else conditional statement(s).
#include<stdio.h>
int main()
{
float num1, num2;
printf(“Enter two floating point numbers:\n”);
scanf("%f %f",&num1,&num2);
return 0;
}
NOTE: Upload the screenshots of the Exercise programs along with the displayed results into
your corresponding Google Classroom.