Problem-Solving in C (Part-1): Nested Loop, Function, and Pointer
Pattern
Problem Statement
You will be given a positive integer N, you need to print a pattern shown below using this N.
For example: If N=5, the pattern will look like below.
See the sample test cases for more clarification.
Input Format
- Input will contain only N.
Constraints
- 1 <= N <= 100
Output Format
- Output the pattern.
Sample Input 0
4
Sample Output 0
#
---
#####
-------
#####
---
#
Sample Input 1
1
Sample Output 1
#
Sample Input 2
7
Sample Output 2
#
---
#####
-------
#########
-----------
#############
-----------
#########
-------
#####
---
#
Try on your own.
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<=(2*n)-1;i++){
for(int j=1;j<=s;j++){
printf(" ");
}
if (i%2==1){
for(int j=1;j<=k;j++){
printf("#");
}
}
else if (i%2==0){
for(int j=1;j<=k;j++){
printf("-");
}
}
if(i<=n-1){
s--;
k=k+2;
}
else{
s++;
k-=2;
}
if(i<n ||i>=n){
printf("\n");
}
}
return 0;
}
Count Before One
Problem Statement
You will be given an array A and the size of that array N. Take input in the main function. You need to write a function named count_before_one() which receives that array of integers and the size of that array and return type will be a 32-bit integer. The function counts the number of elements in that array until you find 1 and returns that count. Print that count in the main function.
Input Format
- The first line will contain N, the size of the array.
- The next line will contain the array A.
Constraints
- 1 <= N <= 1000
- 0 <= A[i] <= 1000; Here 0 <= i < N
Output Format
- Output the number of elements before the first 1.
Sample Input 0
5
5 4 3 1 2
Sample Output 0
3
Sample Input 1
5
1 2 3 4 5
Sample Output 1
0
Try on your own first.
Answer:
#include<stdio.h>
#include<limits.h>
#include<string.h>
int count_before_one(int ar[], int x){
int element=0;
for(int i=0;i<x;i++){
if(ar[i]==1){
return element;
}
else{
element++;
}
}
return element;
}
int main(){
int n;
scanf("%d", &n);
int s[n];
for(int i=0;i<n;i++){
scanf("%d", &s[i]);
}
int z= count_before_one(s,n);
printf("%d", z);
return 0;
}
Pattern 2
Problem Statement
You will be given a positive integer N, you need to print a pattern shown below using this N.
For example: If N=5, the pattern will look like below.
See the sample test cases for more clarification.
Input Format
- Input will contain only N.
Constraints
- 1 <= N <= 9
Output Format
- Output the pattern.
Sample Input 0
1
Sample Output 0
1
Sample Input 1
4
Sample Output 1
1
21
321
4321
Sample Input 2
5
Sample Output 2
1
21
321
4321
54321
Try on your own.
Answer:
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int n;
scanf("%d", &n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
printf(" ");
}
for(int k=i;k>=1;k--){
printf("%d",k);
}
printf("\n");
}
return 0;
}
If you could kindly leave a clap and response on my work, it would be greatly appreciated. Your feedback will motivate me to create and share more content. Thank you!