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

前端 未结 10 1647
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:59

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
    

提交回复
热议问题