UNIT-2 Material - Introduction to Programming (1)
UNIT-2 Material - Introduction to Programming (1)
if (condition)
{
//statements;
}
Note: In the above case-2 output, the given number is 251 which is odd number. According
the source code, if the given number is even number then statements inside in its block get
executed, otherwise no output. If we want to perform two operations on single condition, then
we have to take help from if-else statement.
Program 2 source code: To find the largest number among three numbers
if(condition)
{
//statements1;
}
else
{
//statements2;
}
• In the preceding syntax, if the given condition is true, then statements1 will be executed.
If the condition is false, then statements2 will be executed.
Program 6
AIM: Write a C program to check whether a person is eligible to vote or not without curly
braces
source code: without curly braces
Program 6 output:
Output: case-1
Output: case-2
Program 11:
AIM: Write a C program on else if-ladder statement
Source code:
Program 12:
AIM: The marks obtained by a student in 5 different subjects are input through the keyboard.
The student gets a division as per the following rules:
Rule 1: Percentage above or equal to 60 – First division
Rule 2: Percentage between 50 and 59 – Second division
Rule 3: Percentage between 40 and 49 – Third division
Rule 4: Percentage less than 40 – Fail
Source code:
Program 13:
AIM: Write a program to calculate the salary as per the following table:
Gender Year of service Qualifications Salary
>=10 Post Graduate 11,000
>=10 Graduate 10,000
Male
<10 Post Graduate 9,000
<10 Graduate 7,000
>=10 Post Graduate 12,000
>=10 Graduate 9,000
Female
<10 Post Graduate 10,000
<10 Graduate 6,000
Program 14:
AIM: Write a C program to find ASCII value for various character like lower case alphabet,
upper case alphabet, digit (0-9) and special symbol
Source code:
Program 14 Output:
Program 16:
AIM: Write a C program to implement switch case statements to perform arithmetic operations
Source code:
Program 21 Output:
Note: Change the a and b values and try to execute the above program in your PC/Laptop, to
find the remaining cases of output.
Note: However, the usage of switch with conditions isn't ideal and doesn't work as intended in
C. Observe the below program: Error program. So, the solution is converting the below code
by using if-else.
Program 24 Output:
Output:
Output:
Program 27:
AIM: Write a program to find the value of one number raised to the power of another number
Source code:
Program 28:
AIM: Write a program to find the reverse of a given number using while loop
Source code:
Output:
Output: case-1
Output: case-2
Output: case-1
Output: case-2
Output: case-3
Output:
Output: Case-1
Output: Case-2
Output: Case-3
Output: case-1
Output: case-2
Output: case-3
Output: case-1
Output: case-2
Output: case-3
Output:
Output: case-1
Output: case-2
• It is possible to write a for loop without using any one expression, any two expressions,
or all the three expressions.
Case 1: for loop without initialization statement
Output:
Infinite Loops:
Output:
Program 39:
AIM: Write a C program to demonstrate Nested for-loop
Source code:
Output:
Pattern-1 Output:
Pattern-3 Output:
Pattern-4 Output:
Output:
Note:
What is the difference between exit(0) and exit(1)?
Ans. Both the exit(0) and exit(1) functions will terminate the program. exit(0) represents
normal termination of the program. exit(1) represents that the program is being terminated
because of an error.
Output: case-1
Output: case-2
Output: case-3
Output:
Output:
Output:
Output: case-1
Output: case-2