The document contains 5 problems related to structured programming in C along with their solutions. Problem 1 sorts an input list of integers in ascending order. Problem 2 converts a decimal number to binary. Problem 3 prints all prime numbers from 1 to a given input number. Problem 4 finds unique elements in an unsorted array. Problem 5 merges two sorted lists of elements into a single sorted list. For each problem, the algorithm and C code for the solution is provided.
1. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
Structured Programming Language
Advanced Problems on Loop, Nested Loop
Problem 1: Write a program that will sort (ascending order) an input list of n integers from the user.
input list : 5, 7, -10, 100, 50
output : -10, 5, 7 , 50, 100
Solution:
#include <stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int length;
printf("Enter the size of the list to sort: ");
scanf("%d",&length);
printf("Enter the list elements....n");
int i;
for(i=0;i<length;i++) scanf("%d",&arr[i]);
int out;
for(out=0;out<length;out++){
int in;
for(in=out+1;in<length;in++){
if(arr[in]<arr[out]){
int temp=arr[in]; ///swapping both elements
arr[in]=arr[out];
arr[out]=temp;
}
}
}
int j;
for(j=0;j<length;j++) printf("%d ",arr[j]);
return 0;
}
2. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
Problem 2: Write a program that will take input a decimal number and print the corresponding binary number
in the output.
Input : 100
Output : 1100100
Process:
2 | 100
2 | 50 --- 0
2 | 25 --- 0
2 | 12 --- 1
2 | 6 --- 0
2 | 3 --- 0
2 | 1 --- 1
2 | 0 --- 1
Solution:
#include <stdio.h>
#define SIZE 1000
int main()
{
int buffer[1000]; ///to save the bits
int decimal;
printf("Enter the decimal number: ");
scanf("%d",&decimal);
int length=0; ///initial size of buffer is 0
while(decimal!=0){
buffer[length]=decimal%2;
decimal=decimal/2;
length++;
}
int i;
for(i=length-1;i>=0;i--){ ///printing the array in reverse order
printf("%d",buffer[i]);
}
return 0;
}
3. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
Problem 3: Write a program that will print all the prime numbers from the range 1 to n.
Input : 20
Output : 2, 3, 5, 7, 11, 13, 17, 19,
Solution:
#include <stdio.h>
#define SIZE 1000
int main()
{
int n;
scanf("%d",&n);
int i;
for(i=1;i<=n;i++){
if(i==1) continue;
int j;
for(j=2;j<i;j++){
if(i%j==0) break;
}
if(j<i) continue;
else printf("%d, ",i);
}
return 0;
}
4. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
Problem 4: Write a program to find the unique elements of an unsorted array.
Input : 8 3 6 1 7 3 7 8
Output : 8 3 6 1 7
Solution:
#include<stdio.h>
int main() {
int array[100], size, i, j;
printf("Enter number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d numbersn", size);
for(i = 0; i < size; i++){
scanf("%d", &array[i]);
}
printf("Unique Elementsn");
for(i = 0; i < size; i++) {
for (j=0; j<i; j++){
if (array[i] == array[j])
break;
}
if (i == j){
/// No duplicate element found between index 0 to i-1
printf("%d ", array[i]);
}
}
return 0;
}
5. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
Problem 5: Write a program to merge two sorted list of elements.
Input : 5 (size of the first list)
1 3 5 7 9 (elements of first list)
6 (size of the second list)
-1 1 2 8 10 11 (elements of the second list)
Output : -1 1 1 2 3 5 7 8 9 10 11
Solution:
#include <stdio.h>
#define MAX_SIZE 1000
int main()
{
int list1[MAX_SIZE] , list2[MAX_SIZE];
///taking input first list
int sz1;
printf("Enter the size of the first list: ");
scanf("%d",&sz1);
printf("Enter the elements...n");
int i;
for(i=0;i<sz1;i++) scanf("%d",&list1[i]);
///taking input the 2nd list
int sz2;
printf("Enter the size of the 2nd list: ");
scanf("%d",&sz2);
printf("Enter the elements....n");
int j;
for(j=0;j<sz2;j++) scanf("%d",&list2[j]);
///starting the merge operation
int merge_arr[MAX_SIZE]; ///to save the merged outputs
int len=0;
int track1=0,track2=0;
while(len<sz1+sz2){
if(track1>=sz1){ ///the first list is empty
merge_arr[len]=list2[track2];
track2++;
}
else if(track2>=sz2){
merge_arr[len]=list1[track1];
track1++;
}
else if(list1[track1]<=list2[track2]){
merge_arr[len]=list1[track1];
track1++;
}
else{
6. Mohammad Imam Hossain, Lecturer, dept .of CSE, UIU. Email: imambuet11@gmail.com
merge_arr[len]=list2[track2];
track2++;
}
len++;
}
///showing the merged array
int k;
for(k=0;k<sz1+sz2;k++) printf("%d ",merge_arr[k]);
printf("n");
return 0;
}