I need to generate the following output of odd numbers in pyramid pattern.
The output will be like
1
3 3
5 5 5
7 7 7 7
I have written the following code. What portion i should modify?
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=1;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
Current Output:
Current output is like-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You could write:
for(r=1; r <= num; r+=2) //we only need odd numbers
{
times = r/2 + 1; //how many times to print odd number
for(c=1; c <= times; c++)
printf("%d",r); //print one character at a time
printf("\n");
}
You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.
you have 2 errors in this code .
m is not declared anywhere .
you are running a infinite loop
try this .
#include<stdio.h>
int main()
{
int num,r,c,m;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=r;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
In your code, instead of
m=1;
you should write
m= ( (r/2) + 1);
Oterwise, all the time, you'll be iterating in the for loop only once.
Some little modifications and it works:
#include<stdio.h>
int main()
{
int num,r,c,m=0;
printf("Enter structure number : \n");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m++;
for(c=1; c<=m; c++)
printf("%d ",r);
printf("\n");
}
}
return 0;
}
m is undeclared
line feed at the end of the printf message
m incremented each odd iteration
space between printed unmbers
Here is a demo.
the inner for loop should look like this:
for(c=1; c <= r/2; c++)
printf("%d ",r);
just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?
like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.
you can run the code here on ideone.com
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
int m=r;
for(c=1; c<=m; c++)
{
if(c%2 != 0){
printf("%d ",r);
}
}
printf("\n");
}
}
return 0;
}
and test
sh-4.3# main
Enter structure number : 9
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.
You can check this:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",2*r-1);
printf("\n");
}
return 0;
}
Please take a look at this:
for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
for(c=1; c<=r; c+=2)// increment by 2
printf("%d",r);
printf("\n");
}
If you want alternate numbers like 1,3,5,7... just increment value by 2.
Related
This is a code for input-output practice. I am getting the correct output for the first two input lines. But I am getting a zero for the third line input.
The given task is: To calculate the sum of some integers.
Input:
4 1 2 3 4
5 1 2 3 4 5
0
Output:
10
15
#include<stdio.h>
int main()
{
int i, first, next, total;
while(scanf("%d", &first) != EOF)
{
total = 0;
for(i = 1; i <= first; i++)
{
scanf("%d", &next);
total += next;
}
printf("%d\n", total);
if(first == 0)
{
printf(" ");
}
}
return 0;
}
If you do not want output when the first number on a line is zero, then you should test first == 0 before calculating and printing a total and break from the loop (break; if you want to stop the loop) or continue to the next iteration (continue;).
I was doing some hackerrank practice and I couldnt solve it. I am newbie any ideas to solve this problem?
Please write a software program that prints the following pyramid pattern to the screen. Your program should get a number ānā as an input and should print n lines of the pyramid. (For example if your input is 4, then the following output is expected.)
(You can choose any programming language that you want.)
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
#include<stdio.h>
int main()
{
int i,j,k,l=1;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=l; k++)
{
printf("%d",k);
}
l = l+2;
printf("\n");
}
return 0;
}
These types of problems can be solved by simple mathematics.
I would advice you to solve this by problem by breaking it into simpler and smaller use cases as follows:
Number of blank spaces on each line.
Number of Elements (on each line) until the centre of pyramid.
Number of Elements (on each line) after the centre of pyramid.
#include<stdio.h>
int main()
{
int N;
scanf("%d", &N);
int numberOfBlankSpaces = N + 2;
for(int i=1; i<=N ;i++){
for(int j=0;j<numberOfBlankSpaces;j++){
printf(" ");
}
numberOfBlankSpaces -= 2;
int number_of_elements_until_centre_on_each_line = i;
int startingElement = i;
//Print increasing order until center
while(number_of_elements_until_centre_on_each_line--){
printf("%d ", startingElement);
startingElement++;
}
// Starting element = Element at center - 1
startingElement-=2;
//Print decresing order
while(startingElement >= i){
printf("%d ", startingElement);
startingElement--;
}
printf("\n");
}
return 0;
}
Input: 4
Output:
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
I'm trying to use a for loop to add a space to the beginning of my output for every line. For every line, it will add more space between as it goes through each line to get the flipped triangle.
My code
#include <stdio.h>
#include <math.h>
// compiler issue on 2nd digit
int main(){
long long number;
printf("Enter a number\n");
printf("Enter your number = ");
scanf("%lld", &number);
for(int j=1; j<= (log10(number)+1); j++){
for(int i=1; i<= (log10(number)+1) - (j-1); i++){
printf("%d ", (number%((int)pow(10, i)))/(int)pow(10, i-1));
}
printf("\n");
}
return 0;
}
My output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
What I'm trying to output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
A sample program as what I had in mind
int main()
{
int l;
for(l=0; l<3; l++){
printf("%*s Moving\n", l, "");
}
return 0;
}
Thank you in advance. I appreciate any and all feedback. I am new to programming and i am working on an assignment that prints the Fibonacci Sequence based on how many numbers the user asks for. I have most of the code complete, but there is one remaining piece I am having difficulty with. I would like my output in a table format, but something is off with my code and I am not getting all of the data I would like in my output. In grey is my code, my output, and my desired output.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int sequence = 1;
int a = 0, b = 1, c = 0;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
printf("\n n\t\t Fibonacci Numbers\n");
printf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b);
for (i=0; i <= (n - 3); i++)
{
c = a + b;
a = b;
b = c;
sequence++;
printf("\t\t\t%d\n ", c);
}
return 0;
}
Here is my output:
How many Fibonacci numbers would you like to print?: 8
n Fibonacci Numbers
1 0
1
1
2
3
5
8
13
Here is my desired output:
How many Fibonacci numbers would you like to print?: 8
n Fibonacci Numbers
1 0
2 1
3 1
4 2
5 3
6 5
7 8
8 13
I am not getting all of the data
That's because you are not printing the sequence in printf() of the for loop.
printf("\t\t\t%d\n ", c);
and even before 2nd number before for loop
printf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b);
try making the below changes to your code :
printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b);
sequence++; //as you've printed 2 values already in above printf
for (i=0; i <= (n - 3); i++)
{
c = a + b;
a = b;
b = c;
printf("%d\t\t\t%d\n ",++sequence, c);
//or do sequence++ before printf as you did and just use sequence in printf
}
sample input : 5
sample output :
How many Fibonacci numbers would you like to print?: 5
n Fibonacci Numbers
1 0
2 1
3 1
4 2
5 3
Edit : you can do it using functions this way... it's nearly the same thing :)
#include <stdio.h>
void fib(int n)
{
int i,sequence=0,a=0,b=1,c=0;
printf("\n n\t\t Fibonacci Numbers\n");
printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b);
sequence++;
for (i=0; i <= (n - 2); i++)
{
c = a + b;
a = b;
b = c;
printf("%d\t\t\t%d\n ",++sequence, c);
}
}
int main()
{
int n;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
fib(n);
return 0;
}
You forgot to print sequence in the for loop. Print sequence along with c in the for loop after giving proper number of \t!
This would work, also it is better to properly indentate your code:
int main() {
int i, n;
int sequence = 1;
int a = 0, b = 1, c = 0;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
printf("\n n\t\tFibonacci Numbers\n");
printf(" %d\t\t\t%d\n", sequence, a);
printf(" %d\t\t\t%d\n", ++sequence, b); // <- and you can use pre increment operator like this for your purpose
for (i=0; i <= (n - 3); i++) {
c = a + b;
a = b;
b = c;
sequence++;
printf(" %d\t\t\t%d\n",sequence, c);
}
return 0;
}
Output:
How many Fibonacci numbers would you like to print?: 4
n Fibonacci Numbers
1 0
2 1
3 1
4 2
I feel quite 'stupid' as asking this question but if anyone can show me the methods to modify the input result appeared on the command window.
Example:
I want to sort 5 numbers (1, 3, 4, 7, 5) in smallest-to-biggest order and the result on the command window must be:
input: 1 3 4 7 5 /* 1 line input */
output: 1 3 4 5 7 /* 1 line output */
Edit:
Here is my code
for (i = 0; i < 5; i++)
{
scanf("%d ", &array[i]);
}
If I use this code the result on command window must be:
1
3
4
7
5
But I want all the input number in only 1 line as:
1 3 4 7 5
So what do I have to do with my code?
Regarding to your edited question, just replace "%d " with "%d".
#include <stdio.h>
#define N 5
int main(void){
int i, j, array[N];
printf("Please enter the %d numbers.\n", N);
printf("input : ");
for(i=0;i<N;++i){
scanf("%d", &array[i]);
if(i!=0){
for(j=i;j>0 && array[j-1] > array[j];--j){
//swap array[j] and array[j-1]
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
printf("output : ");
for(i=0;i<N;++i){
if(i!=0)
putchar(' ');
printf("%d", array[i]);
}
putchar('\n');
return 0;
}