# Introduction to Computers and Programming ```Introduction to Computers and
Programming
Class 15
Introduction to C
Professor Avi Rosenfeld
#include &lt;stdio.h&gt;
void main()
{
int num, tenthou, thou, hund, ten, one;
scanf(&quot;%d&quot;,&amp;num);
tenthou=num/10000;
thou=(num%10000)/1000;
hund=(num%1000)/100;
ten=(num%100)/10;
one=(((num%10000)%1000)%100)%10;
printf(&quot;%d%d%d%d%d&quot;,tenthou,ten, hund, thou,
one);
}
#include &lt;stdio.h&gt;
void main()
{
int num, tenthou, thou, hund, ten, one;
scanf(&quot;%d&quot;,&amp;num);
one=num%10; /*gets the last digit */
num=num / 10;
ten=num%10;
num=num / 10;
hund=num%10;
num=num / 10;
thou=num%10;
num=num / 10;
tenthou=num%10;
num=num / 10;
printf(&quot;%d%d%d%d%d&quot;,tenthou,ten, hund, thou, one);
}
Understanding Repetition
#include &lt;stdio.h&gt;
#define digits 7
void main()
{
int num, temp;
scanf(&quot;%d&quot;,&amp;num);
for (int i = 1; i &lt;= digits; i++)
{
temp=num % 10;
num = num / 10;
printf(&quot;Digit #%d is %d\n&quot;, i, temp);
}
}
#include &lt;stdio.h&gt;
void main()
{
float num, high, low;
printf(&quot;Please type the first number &quot;);
scanf(&quot;%f&quot;,&amp;num);
high = low = num; /* new notation same as one two lines */
for (int i = 2; i &lt;=10; i++)
{
scanf(&quot;%f&quot;,&amp;num);
if (num&gt;high)
high = num;
if (num&lt;low)
low = num;
}
printf(&quot;The highest was %f\n&quot;,high);
printf(&quot;The lowest was %f\n&quot;,low);
}
#include &lt;stdio.h&gt;
void main()
{
int size;
scanf(&quot;%d&quot;,&amp;size);
for (int i = 0; i &lt; size; i++)
{
for (int j = 0; j &lt;= i; j++)
printf(&quot;*&quot;);
printf(&quot;\n&quot;);
}
}
#include &lt;stdio.h&gt;
/* The following program converts a number that the user enters from
Celcius to Fahrenheit (decimals allowed). The correct formula is: F =
9/5 C + 32. If the user enters a value less than 0, the program should
print, &quot;Error&quot;. */
int main()
{
float Celcius;
printf(&quot;Please enter a value in Celcius &quot;);
scanf(&quot;%f&quot;,&amp;Celcius);
if (Celcius &lt; 0)
printf(&quot;Error&quot;);
else
printf(&quot;%f degrees Celcius is %f degrees fahrenheit&quot;, Celcius, 9/5
* Celcius + 32);
return 0;
}
a. What is the value after the following statements?
int x = 5;
x -= 1+2 * 3 % 2; Answer:1 or 4
b. If x = 1 and y = 3, what does the following print?
printf(&quot;%d&quot;, 2 * x++ + ++y + 1); Answer: 7
c. If a = 0 and b = 1, does the following expression
in parentheses evaluate to true or false?
( !a &amp;&amp; b ) Answer: T_____
d. What will the following print:
#include &lt;stdio.h&gt;
void main()
{
int x = 1, y = 2;
if (x=2);
printf(&quot;stage 1&quot;);
if (x == y)
printf(&quot;stage 2&quot;);
}
#include &lt;stdio.h&gt;
void main()
{
int x = 90;
switch(x)
{
case 100:
printf(&quot;Here &quot;);
case 90:
printf(&quot;I &quot;);
case 80:
printf(&quot;am &quot;);
break;
default:
printf(&quot;not! &quot;);
}
}
Character constant
• Characters are represented internally through int
values.
• You can reference them by using single quotes
around the character
– E.g. ‘A’, ‘a’, ‘1’ or ‘\n’
• What is actually happening is the computer stores
these as numbers
– The int value for ‘A’ is 65, and for ‘a’ it is 97, etc,
• See Appendix D, page 1198, in your text for a
chart of the int values for each character
getchar()
• Function contained in stdio.h
• A function is like a small program other
programs can use to perform actions (an
example you’ve used is printf() )
• getChar() returns an int code for a character
typed on the standard input (the keyboard)
char c;
c = getchar() ;
How to print a char
• Can use printf with a format control string of %c.
• For example,
printf( “The character is %c\n”, c );
• Can use another function in stdio.h called
putchar()
• For example,
putchar( c ) ;
putchar()
• Prints a character to the standard output
(screen)
• Is a function also referenced in stdio.h
Using getchar() example
#include &lt;stdio.h&gt;
int main()
{
char c ;
/* declare character variable */
/* read a character and store it in c */
c = getchar() ;
/* print it twice, two different ways */
printf(&quot;%c\n&quot;, c );
putchar( c ) ;
/* one character at a time so here’s the newline */
c = '\n' ;
putchar( c );
} /* end program */
Char Input
#include &lt;stdio.h&gt;
void main()
{
int count=0;
char letter;
do
{
scanf(&quot;%s&quot;,&amp;letter);
if (letter=='a')
count++;
}while(letter != 'b');
printf(&quot;There were %d 'A's in the sentence\n&quot;,count);
}
#include &lt;stdio.h&gt;
void main()
{
int count=0;
char letter;