Professional Documents
Culture Documents
printf() function
printf() function is used to print the character,
string, float, integer, octal and hexadecimal values
onto the output screen
We use printf() function with %d format specifier to
display the value of an integer variable.
Similarly %c is used to display character, %f for
float variable, %s for string variable, %lf for double
and %x for hexadecimal variable.
To generate a newline,we use \n in C printf()
statement.
case sensitive
2
int main()
{
char ch = 'A';
char str[20] = "fresh2refresh.com";
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;
}
4
Output :
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
scanf() function
scanf() function is used to read character, string,
numeric data from keyboard
Consider below example program where user
enters a character. This value is assigned to the
variable ch and then displayed.
Then, user enters a string and this value is
assigned to the variable str and then
displayed.
6
int main()
{
char ch;
char str[100];
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}
Output:
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
char Example
#include <stdio.h>
int main ( )
{
char ch ;
printf (Enter a character: ) ;
scanf (%c, &ch) ;
printf (The value of %c is %d.\n, ch, ch) ;
return 0 ;
}
getchar ( ) Example
#include <stdio.h>
int main ( )
{
char ch ; /* int ch would also work! */
printf (Enter a character: ) ;
ch = getchar( ) ;
printf (The value of %c is %d.\n, ch, ch) ;
return 0 ;
}
*/
LOOPING
16
When to Use
We know how many loop
repetitions will be needed
in advance.
C Structure
while, for
while, for
Repeated processing of
data until a desired
condition is met.
while, for
FOR
20
#include <stdio.h>
int main()
{
const int max = 5;
int i;
for(i = 0; i < max; i++)
printf("%d ",i);
return 0;
}
21
while Loop
Statement is
executed while
condition is true
Note that the
condition must
first be true in
order for the
statement to be
executed even
once
condition
FALSE
TRUE
statement
Repetition Structure
t=0
t=10
A dynamics problem (ME 101)
F ma
1 2
d gt v0t d 0
2
mg
Equation of
motion
Solution Algorithm
Start
Pseudocode
1. Start
2. Declare variables: ____
d (distance traveled)
t (time)
(Are these good names?)
3.
4.
Initialize variables
While time is less than or
equal to 10 s
calculate d
print time
print distance
increment time
5.
Stop
while
loop
Flowchart
Declare variables
Initialize variables
t <= 10? T
F
1 2
gt
2
print t, d
t = t +1
Stop
Solution Code
Nested Loops
Nested loops consist of an outer loop with one
or more inner loops.
loops
e.g.,
Outer loop
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
Inner loop
}
}
The above loop will run for 100*50 iterations.
5-27
*************
*************
*************
*************
28
*************
a ************
***********
**********
29
Nested loops
b
*
**
***
****
int a,b;
for (int i = 0; i < a; i++)
{
for (int j=0; j<b && j <= i; j++)
{
printf (*);
}
return 0;
}
30
Nested loops
int a,i,j;
for (i = 0; i < a; i++) {
for (j=0; j<a; j++) {
if (j < a-i)
printf(" );
else
printf("*);
}
return 0;
}
*
**
***
****
*****
31
Nested loops
*
***
*****
*******
*********
***********
32
#include <stdlib.h>
int main()
{
int i, k, levels, space;
printf("Enter the number of levels in pyramid:");
scanf("%d",&levels);
space = levels;
for ( i = 1 ; i <= levels ; i++ )
{
for ( k = 1 ; k < space ; k++ )
printf(" ");
space--;
for ( k = 1 ; k <= 2*i - 1 ; k++ )
printf("*");
printf("\n");
}
return 0;
}
33
34
WHILE
35
37
if(times == ALLOWED_TIMES)
{
printf("You lose! The secret number is %d",secret);
}
return 0;
}
38
selection
39
Selection
if ( age >= 17 )
if ( age >= 17 )
{
printf(Vote!\n) ;
printf(Vote!\n) ;
}
}
else
{
printf(Maybe next time!\n) ;
}
Selection
41
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
printf("Please enter a number:");
scanf ("%d",&x);
if(x > 0){
printf("\nThe number %d is greater than 0",x);
}
return 0;
}
42
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
printf("Please enter a number:");
scanf ("%d",&x);
if(x > 0)
{
printf("\nThe number %d is greater than 0",x);
}
else
{
printf("\nThe number %d is less than or equal zero",x);
}
return 0;
}
43
Nested Selection
44
Compares the input number with zero (0) and displays corresponding message
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
printf("Please enter a number:");
scanf ("%d",&x);
if(x > 0)
{
printf("%d > 0",x);
}
else
if(x < 0)
{
printf("%d < 0",x);
}
else
{
printf("zero");
}
return 0;
}
45
47
48
/* Convert currency */
if(rate > 0){
fc_amount = usd_amount * rate;
}
printf("Result: %.2f",fc_amount);
return 0;
}
49
Multiple Selection
Sometimes it is necessary to branch in more than two
directions.
We do this via multiple selection.
(continued)
if (day == 4) {
printf (Thursday) ;
}
if (day == 5) {
printf (Friday) ;
}
if (day == 6) {
printf (Saturday) ;
}
if ((day < 0) || (day > 6)) {
printf(Error - invalid day.\n) ;
}
...
default:
statement(s)
break ;
}
Switch flowchart
entry
Expression?
value1
Statement(s)
value2
Statement(s)
value3
Statement(s)
value4
Statement(s)
Value n
Statement(s)
57
switch ( day )
{
case 0: printf (Sunday\n) ;
break ;
case 1: printf (Monday\n) ;
break ;
case 2: printf (Tuesday\n) ;
break ;
case 3: printf (Wednesday\n) ;
break ;
case 4: printf (Thursday\n) ;
break ;
case 5: printf (Friday\n) ;
break ;
case 6: printf (Saturday\n) ;
break ;
default: printf (Error -- invalid day.\n)
;
break ;
}
switch Example
Is this structure more
efficient than the equivalent
nested if-else structure?
#include <stdio.h>
int main() {
int color = 1;
printf("Please choose a color(1: red,2: green,3: blue):\n");
scanf("%d", &color);
switch (color) {
case 1:
printf("you chose red color\n");
break;
case 2:
printf("you chose green color\n");
break;
case 3:
printf("you chose blue color\n");
break;
default:
printf("you did not choose any color\n");
}
return 0;
}
60
int x, y;
for(x=1; x<=10; x++)
{
if(x == 5)
{
y = x;
continue;
}
printf("%d ", x);
}
printf("Skipped x ==
%d\n",y);
1 2 3 4 6 7 8 9 10 Skipped x
== 5