(Document Title) : Submitted By: Sp21-Bcs-048 (Hassan Arslan)
(Document Title) : Submitted By: Sp21-Bcs-048 (Hassan Arslan)
Solution:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if (num<10)
{
printf("the number is smaller than 10.");
}
return 0;
}
If-else statement:
Real world problem:
Write a program to find if an entered number is even or odd?
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("enter a number: ");
scanf("%d",&number);
if (number%2==0){
printf("the number is an even number");
}
else
printf("the number is an odd number");
return 0;
}
If-else-if statement:
Real world problem:
Write a program which get marks as input an displays your grade ( i.e A,B or
C):
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks;
printf("enter your marks: ");
scanf("%d",&marks);
if (marks>=90){
printf("you got A");
}
else if (marks>=80 && marks<90){
printf("you got B");
}
else if(marks>=70 && marks<80){
printf("you got C");
}
else if (marks>=60 && marks<70){
printf("you got D");
}
else if (marks>=50 && marks<60){
printf("you got E");
}
else
printf("you got F");
return 0;
}
Switch-case :
Real world problem:
Write a program which gets your grade character as input and displays your
marks.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char grade;
printf("enter your grade(i.e A,B or C): ");
scanf("%c",&grade);
switch (grade){
case 'A':
printf("your marks are greater than 90\n");
break;
case 'B':
printf("your marks are between 80 and 90\n");
break;
case 'C':
printf("your marks are between 70 and 80\n");
break;
case 'D':
printf("your marks are between 60 and 70\n");
break;
case 'E':
printf("your marks are between 50 and 60\n");
break;
case 'F':
printf("your marks are less than 50\n");
break;
default:
printf("invalid input!\n");
}
return 0;
}
While loop:
Real world problem:
Write a program to print table of a number?
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int i=1;
printf("enter a number: ");
scanf("%d",&number);
//processing
while (i<=10){
printf("%d x %d = %d\n",number,i,number*i);
i++;
}
return 0;
}
For loop
Real world problem:
write a program to print following pattern of numbers.
11 22 33 44 55.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
Do while loop:
Real world problem:
write a program to get user choice
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char choice='Y';
do {
printf("you are in.\n");
printf("press y to continue or n to exit: \n");
choice=getche();
}
while (choice=='y');
return 0;
}
Nested loop:
Real world problem:
Print the following pattern:
1
12
123
1234
12345
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
for (int i=1; i<=5; i++){
for (int j=1; j<=i; j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}