Module 2 (PSP)
Module 2 (PSP)
* Visit https://vtuconnect.in for more info. For any queries or questions wrt our
platform contact us at: [email protected]
Download VTU Connect App Now From Google Play Store Go Digital, Go Green, Save Paper
C PROGRAMMING FOR PROBLEM SOLVING
Module II
Format Specifier:
It is used to specify the type of the value to be read or written using standard input output
functions.
Commonly used scanf format Codes
CODE MEANING
%c Read a single character
%d Read a decimal integer
%e Read a floating point value with exponents
%f Read a floating point value
%g Read a floating point value
%hd Read a short integer
%i Read decimal integer
%o Read a octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a Hexadecimal integer
%lf Read a double
%[…] Read a String of word(S)
2. Output Function:
Syntax:
printf(“control string”, list of variable or expression);
Rules for printf:
Control String consist of three types of items:
1. Characters that will be printed on the screen as they appear.
2. Format specification that define the output format for display of each item.
3. Escape sequence character such as \n, \t and \b.
Conversion Specification for printf:
Minimum
% flag padding precision Conversion code
width
Flag: It is use to specify the sign for justification either left justify or right justify. By default, if
the sign is not specified then it is right justified.
Minimum Width: Number of column available to store and display the value.
Padding: if the value is lesser than number of width then the free space is padded with white space
or with zero.
Precision: For floating values after decimal point how many digits’ user like to display.
Conversion code: For different data types we have separate conversion code i.e., same as scanf
format code in above table.
Example:
printf(“the sum is %-04.2d”, a); where a=12
- indicate left justification
0 indicate padding with zero if pad value is not specified then default white space is
considered.
4 indicate minimum width i.e., 4 columns to display but we have the value of only two digits
of 2 so remaining two space is padded with zero
.2 if it is float value then after decimal point only two digits is displayed
d indicate conversion code for integer data type value
Output:
1 2 0 0
gets(ch);
Syntax for puts:
puts(ch);
Syntax:
if(conditional expression)
{
Statements;
}
Statement Y;
Flowchart:
Marks=35
2. Enter marks: 15
Marks=15
2.2.1.2. ”if-else” Statement
Explanation:
“if-else” statement is a two-way selection statement which executes statements based on
whether the condition is true (or) false. If the condition is true, then the set of statements associated
with “if” is executed (i.e., Statement X) and then the rest of the code (i.e., Statement A) will be
executed skipping the “else” part. If the condition is false, then the set of statements associated
with else is executed (i.e., Statement Y) and then the rest of the code (i.e., Statement A) will be
executed skipping the “if” part.
Syntax:
if (conditional expression)
{
Statement X;
}
else
{
Statement Y;
}
Statement A;
Flowchart:
Case 2: if the “conditional expression1” is false, then else is executed. If the “conditional
expression3” is true, then Statement Y is executed. If the “conditional expression3” is false, then
Statement Z is executed. Later the control comes out of “if/else” and executes “statement A”.
Syntax:
if (conditional expression1)
{
if (conditional expression2)
{
Statement X;
}
}
else
{
if (conditional expression3)
{
Statement Y;
}
else
{
Statement Z;
}
}
Statement A;
Flowchart:
Flowchart:
Example: Program to check whether a given number is positive, negative or “zero” number.
#include<stdio.h>
main()
{
int a;
printf(“Enter the number\n”);
scanf(“%d”,&a);
if(a==0)
{
printf(“%d is a zero number\n”,a);
}
else if(a>0)
{
printf(“%d is a positive number\n”,a);
}
else
{
printf(“%d is a negative number\n”,a);
}
}
Flowchart:
break;
case ‘o’: printf(“ It is a Vowel\n”);
break;
case ‘u’: printf(“ It is a Vowel\n”);
break;
default: printf(“ It is a consonant\n”);
}
}
Output:
1. Enter a character:
a
It is a Vowel
2. Enter a character:
u
It is a Vowel
3. Enter a character:
x
It is a consonant
main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“ Iteration %d\n”,i);
if(i==3)
{
continue;
}
printf(“i=%d\n”,i);
}
}
Output:
Iteration 1
1
Iteration 2
2
Iteration 3
Iteration 4
4
Iteration 5
5
3. goto: A “goto” statement provides an unconditional jump from the goto to a labeled statement.
Here label is an identifier and it can be set anywhere in the program above or below to goto
statement. If the label statement is set above the goto then it is called backward jump and if it
is set below it is called forward jump.
Forward jump Syntax:
goto label_name;
…..
label_name: Statements
Definition:
Looping statement are the statements execute one or more statement repeatedly several
number of times until a given condition fails. These statements are also called repetitive or
iterative statements.
Looping Process:
Step1: Initialize a condition variable
Step 2: Test the condition variable as specified in the conditional expression for the execution
of the loop.
Step 3: If the condition is true, then execute the steps inside the loop where the updating of the
condition variable is done and go to step 2.
Step 4: If the condition in step 2 is false, then stop execution of loop and continue with
execution of rest of the statements in the program.
Depending upon the position of a control statement in a program, a loop is classified into
two types:
1. Entry controlled/ Pre-Test loop:
The condition is checked before executing the body of a loop. The body of the loop
executes zero (or) more times.
2. Exit controlled/ Post-Test loop:
The condition is checked after executing the body of a loop. The body of the loop executes
at least one time.
Output:
The first 5 natural numbers are:
1
2
3
4
5
Definition:
A do-while loop is a post-test loop where the body of the loop is executed and then the
condition is checked. It executes the statements in the loop until the condition of the loop evaluates
to true. Loop gets terminated once the condition becomes false.
Working:
The statement inside the body of do-while are first executed one after the other.
Then, the expression is evaluated. If the expression is evaluated to true, the body of the loop is
executed again and the process is repeated.
If the expression is evaluated to false, control comes out of the do-while and A-statements that
appear after the do-while loop is executed.
Syntax:
do
{
statements;
} while (condition);
Statement A;
Flowchart:
Output:
The first 5 natural numbers are:
1
2
3
4
5
2.2.3.2.3 FOR LOOP
Definition:
A “for” loop is a pre-test loop where the condition is checked first and then the body of the
loop is executed. It executes the statements in the loop until the condition of the while loop
evaluates to true. Loop gets terminated when the condition becomes false.
Working:
Step1: Initialize the conditional variable(i=1)
Step 2: Check the condition(i<=5)
Step 3: If the condition is true, execute the steps inside the loop. Then goto step 5
Step 4: If the condition is false, go out the for loop and goto step 6
Step 5: Update the variable(i++) and goto step 2.
Step 6: Execute statement A.
Syntax:
for(initialization; condition; updation)
{
statements;
}
Statement A;
Flowchart:
Output:
The first 5 natural numbers are:
1
2
3
4
5
Step 1: Start
Step 2: [Input coefficients of quadratic equation]
read a, b, c
Step 3: [Check for the valid coefficients]
if a=0 and b=0 then
print “Invalid coefficients”
else a=0 then
root1 = -c/b
print “Linear equation”
print root
Step 4: [Compute discriminate value]
disc = b+b-4*a*c
Step 5: [Based on discriminate value, classify and calculate all possible roots and print them]
if disc=0 then
root1 = root2 = -b/(2*a)
print “The roots are real and equal”
print root1, root2
else if disc>0 then
root1 = (-b+⎷disc)/(2*a)
root2 = (-b -⎷disc)/(2*a)
print “The roots are real and distinct”
print root1,root2
else
real = -b/(2*a)
imag = ⎷disc / (2*a)
print “The roots are real and complex”
print root1,root2
end if
end if
Start 6: Stop
Program:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,disc,root1,root2,real,img;
printf("enter a,b,c values\n");
scanf("%f %f %f",&a,&b,&c);
if((a==0)&&(b==0))
{
printf("roots cannot be determined\n");
}
else if(a==0)
{
printf("linear equation\n");
root1=-c/b;
printf("Root1=%.3f\n",root1);
}
else
{
disc=b*b-4*a*c;
if(disc==0)
{
printf("The roots are real and equal\n");
root1=root2=-b/(2*a);
printf("Root1=%.3f\n Root2=%.3f\n",root1,root2);
}
else if(disc>0)
{
printf("The roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%.3f\n Root2=%.3f\n",root1,root2);
}
else
{
printf("The roots are real and imaginary\n");
real=-b/(2*a);
img=sqrt(fabs(disc))/(2*a);
printf("Root1=%.3f+i%.3f\n Root2=%.3f-i%.3f",real,img,real,img);
}
}
}
Program:
#include<stdio.h>
main()
{
int i,n,r;
long int x=1, y=1, z=1, nCr;
printf(Enter the value of n and r\n”);
scanf(“%d%d”, &n,&r);
for(i=n; i>=1; i--)
{
x=x*i;
}
for(i=n-r; i>=1; i--)
{
y=y*i;
}
for(i=r; i>=1; i--)
{
z=z*i;
}
nCr=x/(y*z);
printf(“ %dC%d=%d”,n,r,nCr);
}
Program:
#include<stdio.h>
main()
{
int rows,num=1,space,i,j;
printf("Enter number of rows: ");
scanf(‘%d”,&rows);
for(i=0; i<rows; i++)
{
for(space=1; space<=rows-i; space++)
printf(“ “);
for(j=0; j<=i; j++)
{
if(j==0 || i==0)
num=1;
else
num=num*(i-j+1)/j;
printf(“%4d”,num);
}
printf(“\n”);
}
}