Print star ('*') diamond in C with nested loops?

前端 未结 10 1905
醉酒成梦
醉酒成梦 2021-01-07 14:41

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

10条回答
  •  隐瞒了意图╮
    2021-01-07 15:26

    here the code with matlab

    clear all, clc
    n=input('Input an Odd Number ');
    for i=1:(n+1)/2
        for m=1:(n+1)/2-i
            fprintf(' ');
        end
        for j=1:(2*i)-1
            fprintf('*');
        end
        fprintf('\n');
    end
    for k=i-1:-1:1
        for m=1:(n+1)/2-k
            fprintf(' ');
        end
        for j=1:(2*k)-1
            fprintf('*');
        end
        fprintf('\n');
    end
    

提交回复
热议问题