0% found this document useful (0 votes)
34 views

(Document Title) : Submitted By: Sp21-Bcs-048 (Hassan Arslan)

This document contains solutions to various selection and repetition problems in C programming using different control structures like if, if-else, switch case, while, for, do-while and nested loops. Real world problems are presented for each control structure, followed by the C code solution. Selection structures covered include problems to check if a number is smaller than 10, determine if a number is even or odd, and print a grade based on marks. Repetition structures covered include problems to print a number table, print a pattern of numbers, get user choice in a loop, and print a nested loop pattern.

Uploaded by

Haider Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

(Document Title) : Submitted By: Sp21-Bcs-048 (Hassan Arslan)

This document contains solutions to various selection and repetition problems in C programming using different control structures like if, if-else, switch case, while, for, do-while and nested loops. Real world problems are presented for each control structure, followed by the C code solution. Selection structures covered include problems to check if a number is smaller than 10, determine if a number is even or odd, and print a grade based on marks. Repetition structures covered include problems to print a number table, print a pattern of numbers, get user choice in a loop, and print a nested loop pattern.

Uploaded by

Haider Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

[DOCUMENT TITLE]

SUBMITTED BY: SP21-BCS-048 (HASSAN ARSLAN)

SUBMITTED TO: SIR RAO ADEEL NAWAB


[Company address]
1. Task 01 :
For each of the following Selection Structures select a Real-
world Problem and solve them by writing C Programs (similar to the
one I have solved for you)
1. if
2. if-else
3. if-else-if
4. switch-case
If –statement
Real world problem:
Write a program to find that if an entered number is smaller than 10?

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;
}

 Task 02 For each of the following Repetition Structures select a


Real-world Problem and solve them by writing C Programs (similar
to the one I have solved for you).
1. While loop
2. For loop
3. Do-while loop
4. Nested loops

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()
{

for (int i=11; i<=55; i=i+11)


{
printf("%d ",i);
}
return 0;
}

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;
}

You might also like