C Mod 3
C Mod 3
Language
MODULE 3
c = getchar ( );
The first statement declares that c is a character-type variable. The second statement causes
a single character to be entered from the standard input device and then assigned to c.
Note: The getchar function can also be used to read multi character strings, by reading one
character at a time within a loop.
getch():-
The getch() in C is a non-standard function used to receive a character as input from the
user. It is defined in the header file conio.h. The character entered by the user is not visible on the
output screen but is stored in the assigned variable. We can also explain the getch() as a function
call that pauses the execution of the program until the user enters a character from the keyboard.
Single characters can be displayed using the C library function putchar. The putchar
function transmits a character to a standard output device. The character being transmitted will
normally be represented as a character-type variable. It must be expressed as an argument to the
function, enclosed in parentheses, following the word putchar.
The putchar takes the following form:
putchar ( character_variable ) ;
Where the character variable refers to some previously declared character variable.
Example:
char c;
putchar (c ) ;
The first statement declares that c is a character-type variable. The second statement causes
the current value of c to be transmitted to the standard output device where it will be displayed.
Note: The putchar function can be used with loops to output a string.
THE gets AND puts FUNCTIONS
The gets and puts functions facilitates the transfer of strings between the computer and the
standard input/output devices. Each of these functions accepts a single argument. The argument
must be a string, (e.g., a character array). The string may include whitespace characters.
Example
main()
{
char a[20];
int b;
float c;
………
scanf (“ %s %d %f ”,a, &b, &c );
………….
}
Within the scanf function, the control string is “%s %d %f “. It contains three character groups.
3 Dept. of CS, IGCAS
Methodology of Programming & C BCA/BSc CS
Language
The first character group %s represents a string.
The second character group %d represents a decimal integer value.
The third character group %f represents a floating-point value.
Numerical variables b and c are preceded by ampersands within the scanf function.
In C programming language, 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.
The general form of printf is:
printf ( "control string”, arg 1, arg 2, …..,arg n) ;
Control string consists of three types of items:
1. Characters that will be printed on the screen as they appear.
2. Format specifications that define the output format for display of each item.
3. Escape sequence characters such as \n,\t,\b.
The control string indicates how many arguments follow and what their types are. The
arguments arg1,arg2,….,argn are the variables whose values are formatted and printed according to
the specification of control string. The arguments should match in number, order and type with the
format specifications.
Some examples of formatted printf statements are:
printf(“Programming in C”);
printf(“ ”);
printf(“ \n ”);
printf(“ %d ”, x); printf(“a=
%f \n b= %f”,a,b);
printf(“sum= %d”, 1234);
printf(“\n \n”);
Entry
True
The ‘statement-block’ may be a single statement or a group of statements. If the expression is true,
the statement/statement-block will be executed; otherwise the statement/statement block will be
skipped and the execution will jump to the statement-x. When the condition is true both the
statement-block and the statement-x are executed in sequence.
If ( expression 1)
{
If ( expression 2 )
{
statement-1 ;
}
else
{
statement-2;
}
}
else
void main()
{
int a,b,c;
printf(“Enter three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
printf(“The largest value is ”);
if(a>b)
{
if(a>c)
printf(“%d”,a);
else
printf(“%d”,c);
}
}
getch();
}
P11. Write a program to input two numbers and check whether the first number is divisible by the
second number.
P12. Write a program to input two numbers and check the sum is even or odd.
P13. Write program to print the largest of three numbers using logical AND.
#include <stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers: \n ");
scanf("%d%d%d",&a,&b,&c);
if (a > b && a > c)
printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d",
b); if (c > a && c > b)
printf("Biggest number is %d", c);
getch();
}
P14. Write a program to input two numbers and find the largest among them using conditional
operator.
P15. Write a program to find he roots of the quadratic equation of the form ax2+bx+c=0.
Roots= -
b+ b2-4ac
2a
#include <math.h>
#include <stdio.h>
Switch statement is multiple branch decision statement. The switch statement tests the
value of a given variable(expression) against a list of case values and when a match is found, a
block of statements associated with that case is executed.
General form/Syntax:
switch(expression)
{
case value-1:
block
1;
break;
case value-2:
block-
2;
break;
…………
………
13 Dept. of CS, IGCAS
Methodology of Programming & C BCA/BSc CS
Language
… default:
P16. Write a program to print name of day based on a given number (1-7) using switch statement.
#include <stdio.h>
void main()
{
int day;
printf("Enter Day Number\n");
scanf("%d", &day);
switch(day)
{
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;
15 Dept. of CS, IGCAS
Methodology of Programming & C BCA/BSc CS
Language
case 6 : printf("Saturday\n");
break;
case 7 : printf("Sunday\n");
break;
default: printf("Invalid Input !!!!\n");
}
getch();
}
while(condition)
{
Body of the loop
}
Example
P17. Write a program to find the sum first five natural numbers
void main()
{
int n, sum;
i=1; /*initialization*/
sum=0;
whie( i<=5) /* testing*/
{
sum=sum+i;
i=i+1; /*incrementing*/
}
printf(“sum =%d”, sum);
}
Output
sum =15
THE do STATEMENT
On some occasions it might be necessary to execute the body of the loop before the test is
performed. Such situations can be handled with the help of the do statement.
The general form is:
do
{
Body of the loop
} while(condition);
sum =15
THE for STATEMENT
The for loop is another entry-controlled loop. The general form of the for loop is :
for ( initialization ; test-condition ; increment )
{
Body of the loop;
}
The execution of the for statement is as follows:
1. Initialization of the control variables is done first, using assignment statements.
2. The value of the control variable is tested using the test condition. If the condition is true, the
body of the loop is executed; otherwise the loop is terminated and the execution continues with the
statement that immediately follows the loop.
3. When the body of the loop is executed, the control is transferred back to the for statement after
evaluating the last statement in the loop. Now the control variable is incremented /decremented and
the new value of the control variable is again tested to see whether it satisfies the loop condition. If
-
if(error)
break;
}
-
Example
for(i=0; i<50; i++)
{
if(i==10)
break;
printf("%d\n",i);
}
output
Only numbers 0 through 9 are printed.
The continue causes the loop to be continued with the next iteration after skipping any
statements in between. In while and do loops, continue causes the control to go directly to the test-
condition and then to continue the iteration process. In the case of for loop, the increment section
of the loop is executed before the test-condition is evaluated.
General form of continue is: