I'm scratching my head from the morning and I didn't even reach something which looks like given pattern. Any amount of help is appreciated.
Thanks in advance!
This is not my home work
EDIT 1: Thank You all! I finally arrived at the solution.
`#include<stdio.h>
void main(){
int n,i,count;
scanf("%d",&n);
int prev=n,next=(n*2)+(n-2),tc=1;
for(int i=1;i<=n;i++){
if(i==1) {
for(count=1;count<=(n*2)+(n-2);count++) {
if(count==prev || count==next) printf("*");
else printf(" ");
}
}
else {
for(count=1;count<=2*n*n;count++){
if(count==prev-tc || count==prev+tc || count==next-tc|| count==next+tc)
printf("*");
else printf(" ");
}
tc++;
}
printf("\n");
}
}`
to solve questions of this type you must find a formula for spaces and a formula for stars. these formulas tell the computer how many spaces and stars must be printed in each line.
that's all these types of questions need.
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int x=n;x>=i;x--){
printf(" ");
}
int y=2*i; int f=1;
while(y!=1){
if(f!=1&&f!=2*i-1){
printf(" ");
}
else if(i==n&&f==2*i-1){
printf("");
}
else{
printf("*");
}
f++;
y--;
}
if(i==n){
printf("*");
}
for(int xk=n;xk>i;xk--){
printf(" ");
}
int y1=2*i; int f2=1;
while(y1!=1){
if(f2==2*i-1){
printf("");
}
if(f2!=1&&f2!=2*i-1){
printf(" ");
}
else if(i==n&&f2==1){
printf(" ");
}
else{
printf("*");
}
f2++;
y1--;
}
printf("\n");
}
}
Related
#include <stdio.h>
int main () {
int row, i, j;
printf("Enter a number: ");
scanf("%d", &row);
for (i=1; i<=row; i++) {
for (j=1; j<=row; j++) {
if (i==1 || i==row || i+j==row+1) {
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
printf("\n\n");
}
return 0;
}
The program prints out the letter "Z" out of stars.
I have to add a do-while loop in this. (This is for school, you can clearly see that I'm a begginer.)
Perhaps this is what you want:
do {
printf("Enter a number: ");
scanf("%d", &row);
if (row < 4) puts("Try again");
} while (row < 4);
#include <stdio.h>
int main() {
int M,N,i=0,sum=0;
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
else if(M==N){ printf("%d Sum=%d\n",N,M); }
else if(M<N){
for(i=M; i<=N; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
else if(M>N){
for(i=N; i<=M; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
}
return 0;
}
what is wrong with this code? URI judge saying 100% wrong answer but I tried all the test cases and it passed in those cases
I'm not sure, but before while loop you should get scanf("%d%d",&M,&N); one time.
Because the m and n is not defined.
Like this way :
scanf("%d%d",&M,&N);
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
...
I am trying to make a diamond shape. I ask the user for a number and then I make a shape of 2n+1 by 2n+1. Instead of the stars I ask for a letter and I print it instead. Also instead of the spaces I print a period.
My program has stopped running. It asks me for the input then the window just disappears and the program dies.
I could really use a hint. Yes, this is a homework problem.
#include <stdio.h>
void main(){
int number_user_Input;
int temp;
char letter;
int shape_dimension;
printf("Enter a number please: ");
while((temp=scanf("%d", &number_user_Input))==0)
{
scanf("%*[^\n]");
printf("Please enter an actual number:\n");
}
if(temp==EOF)
{
printf("Nothing to read, number was not found.\n");
}
else
{
printf("You typed: %d\n", number_user_Input);
}
printf("Please type only one letter: ");
scanf(" %c", &letter);
shape_dimension = 2*number_user_Input+1;
printf("Shape dimensions are: %d \n", shape_dimension);
for(int i=0; i<shape_dimension; i++)
{
for(int j=1; j <shape_dimension-i; j++)
{
printf(".");
}
for(int j=1; j<2*i-1; j++){
printf("%c", letter);
}
printf("\n");
}
for(int i=shape_dimension-1; i>=1; i--)
{
for(int j=1;j<=shape_dimension-i;j++){
printf(".");
}
for(int j=1;j<=2*i-1;j++){
printf("%c", letter);
}
printf("\n");
}
}
I thinks change for section to this will solve your problem.
for(int i=0; i<shape_dimension; i++){
for(int j=0; j <shape_dimension-i; j++){
printf(".");
}
for(int j=0; j<2*i-1; j++){
printf("%c", letter);
}
printf("\n");
}
//~ printf("\n");
for(int i=shape_dimension; i>=1; i--){
for(int j=0;j<shape_dimension-i;j++){
printf(".");
}
for(int j=0;j<2*i-1;j++){
printf("%c", letter);
}
printf("\n");
}
Run code will produce this output
./code <<< "1 a"
Enter a number please: You typed: 1
Please type only one letter: Shape dimensions are: 3
...
..a
.aaa
aaaaa
.aaa
..a
Other notes
void main() -> int main(void) and return 0; at the end of main().
#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n)+1;
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
My code is not running properly not printing all the rows it should. It is the code to print a full arrow like
*
**
********
**
*
its just an example.
Your problem is:
m=(2*n)+1;
It needs to be
m=(2*n);
see: Online gcc compiler example draw arrow
https://onlinegdb.com/rk9U4nUFf
It got fixed!!! i was running the loop one time less so it was running but not printing anything as the last loop was skipping a loop which it should not. Above is the correct code.
#include <stdio.h>
int main(void) {
int i,j,n,m;
printf("enter the numbr of rows : ");
scanf("%d",&n);
m=(2*n);
printf("%d, %d",n,m);
for(i=0;i<=m;i++)
{
for(j=0;j<m;j++)
{
if(i==n)
{
printf("*");
}
else
{
printf(" ");
}
}
for(j=0;j<i;j++)
{
if((i+j)>=m)
break;
printf("*");
}
printf("\n");
}
return 0;
}
Source Link: https://www.onlinegdb.com/H1gogfIHYz
I have to write a program that will print a square made of asterisks with one diagonal, with the side of n asterisks. If n is 5, the pattern would be
*****
* **
* * *
** *
*****
My erroneous code is:
#include<stdio.h>
main(){
int n,row,i,star;
scanf("%d",&n);
for(row=1;row<=n;row++){
if(row==1||row==n){
for(i=1;i<=n;i++){
printf("*");
}
}
else { for(star=1;star<=n;star++){
if((star==1)||(star=n-row+1)||(star==n))
printf("*");
else printf(" ");
}
}
printf("\n");
}
}
Instead of doing its job, it prints infinite asterisks.
You are assigning instead of comparing.
if((star==1)||(star=n-row+1)||(star==n))
^
Try this code.
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number of lines to be printed:");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("*");
printf("\n");
for(i=0;i<n-2;i++)
{
printf("*");
for(j=0;j<n-2;j++)
{
if(j==(n-3-i))
{
printf("*");
}
else{
printf(" ");
}
}
printf("*\n");
}
for(i=0;i<n;i++)
printf("*");
printf("\n");
}