Problem-Solving in C (Part-3): Nested Loop, Function and Pointer

Mohammed Muzahidul Islam
3 min readDec 31, 2023

--

H. Sorting

Given a number N and an array A of N numbers. Print the numbers after sorting them.

Note:

Input

First line contains a number N (0 < N < 10^3) number of elements.

Second line contains N numbers ( — 100 ≤ Ai ≤ 100).

Output

Print the numbers after sorting them.

Input

3
3 1 2

Output

1 2 3

Input

4
5 2 7 3

Output

2 3 5 7

Try on your own first.

Photo by Joshua Reddekopp on Unsplash

Answer:-

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int ar[n];
for(int i=0; i<n ;i++){
cin>>ar[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ar[j] < ar[i]) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}
for (int i = 0; i < n; i++) {
cout << ar[i] << " ";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main (){
int n;
cin >> n;
int a[n];
for(int i=0; i<n;i++){
cin>>a[i];
}
sort(a, a+n);
for(int i=0; i<n;i++){
cout<<" "<<a[i];
}
return 0;
}

P. Shape 1

Given a number N. Print a face-down right-angled triangle that has N rows.

For more clarification see the example below.

Input

Only one line containing a number N (1 ≤ N ≤ 99).

Output

Print the answer according to the requirements above.

Input

4

Output

****
***
**
*

Note

Don’t print any extra spaces after the symbol “ * “.

Try on your own first.

Photo by Roman Synkevych on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int n;
scanf("%d", &n);
int k=n;
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
printf("*");
}
k--;
if(i<n){
printf("\n");
}
}
return 0;
}

T. Shape 2

Given a number N. Print a pyramid that has N rows.

For more clarification see the example below.

Input

Only one line containing a number N (1 ≤ N ≤ 99).

Output

Print the answer according to the requirements above.

Input

4

Output

   *
***
*****
*******

Note

Don’t print any extra spaces after the symbol “ * “.

Try on your own first.

Photo by Shamsudeen Adedokun on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int n, s, k;
scanf("%d", &n);
s=n-1;
k=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=s;j++){
printf(" ");
}
for(int j=1;j<=k;j++){
printf("*");
}
s--;
k=k+2;
if(i<n){
printf("\n");
}
}
return 0;
}

Codeblocks Questions:

  1. H. sorting
  2. T. shape 2
  3. P. Shape 1

If you appreciate my work, consider following and clapping to encourage me to upload like this.

--

--

Mohammed Muzahidul Islam
Mohammed Muzahidul Islam

Responses (1)