Professional Documents
Culture Documents
int main()
{
int honours;
printf(“Enter 1 if you have Honours: ”);
scanf(“%d”,&honours);
if (honours == 1) {
printf(“with Honours”);
}
return 0;
}
Example Program – if..else
#include<stdio.h>
int main()
{
int age;
printf(“Enter age: ”);
scanf(“%d”,&age);
if (age >= 18 )
printf(“Eligible to vote”);
else {
printf(“Not eligible to vote”);
}
return 0;
}
Example Program – if..else if..else
#include<stdio.h>
int main()
{
int a,b;
printf(“Enter 2 numbers: ”);
scanf(“%d%d”,&a,&b);
if (a > b)
printf(“First number is larger”);
else if (a < b)
printf(“Second number is larger”);
else
printf(“The numbers are equal”);
return 0;
}
Example Nested if Code
if (var1 != var2)
{
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
Exercise
• Given the length of three sides of a triangle, write a C program to find
if a triangle is equilateral, isosceles or scalene triangle.
• Write a C program to calculate the commission payable and print it
along with the D.D amount. Commission rates are as follows:
Up to Rs.5,000 - Rs.25.
Rs.5,000 to Rs.10,000 - Rs.50.
Rs.10,000 to Rs.1 lakh - Rs.5 per Rs.1,000 (Min. Rs.60)
Above Rs.1 lakh - Rs.4 per Rs.1,000 (Min. Rs.600; Max. Rs.2,000)
Conditional Operator
• Also called ternary operator (3 operands)
• Allows coding of simple if..else construct
expr1 ? expr2 : expr3
eg.
(age > 18) ? printf(“Eligible”) : printf(“Not eligible”);
max = (a > b) ? a : b;
Iterative Structures
Iterative Structures
• Repeat a set of statements until a condition becomes false
• Type:
• Logic Controlled loops (while, do..while)
• Counter Controlled loops (for)
While loop
• Syntax:
while (condition)
{
statements;
}
• The statements are repeated while the condition is true
• Loop terminates when the condition becomes false
Program to print all perfect Squares below
100
#include <stdio.h>
int main(void)
{
int i=1;
printf("The perfect squares below 100 are:\n");
while(i*i<100)
{
printf("%d",i*i);
i++;
}
return 0;
}
printf("Sum = %d",sum);
Differences between while and do..while
while do..while
Pre-tested or top-tested Post-tested or bottom tested
int main(){
int fact=1,num;
int i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact = fact*i;
}
printf("Factorial=%d\n",fact);
return 0;
}
Examples
sum=1; index=9; for(i=0;i<=25;i++)
do{ {
index = index-1; i++;
sum=2*sum; i++;
} while(index>9) }
sum=0; i=20;
for(i=0;i<5;i++) for(;i;){
sum=sum+i; i--;
printf(“%d ”,sum); printf(“%d ”,i);
}
Practice Programs
• Write a program to print out the integers 1 to 500, five per line.
• Write a program to print the largest of n given numbers.
• Write a program to print the sum of first 50 even natural numbers
• Write a program to print all perfect squares < 1000
• Write a program to print the following sequence until last value is less
than 1000
1, 3, 7, 15, 31, …
Nested Loops
• A loop construct appears within another loop construct
• The loop variables must not overlap
eg. for(i=10;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf(“%d”,i);
}
}
Caveat