카테고리 없음
무한루프와 브레이크문/BMI 계산기 만들기 /팩토리얼구하기/원주율 구하기
이준호
2020. 8. 24. 17:52
반응형
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 무한루프와_브레이크_문
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int days = 1;
int money = 1000;
while (true)
{
sum = sum + money;
Console.WriteLine($"{days,3}일차 : {money,8:C}, sum = {sum,11:C}");
if (sum>=1000000)
{
break;
}
days++;
money = money * 2;
}
Console.WriteLine("");
Console.WriteLine($"{days,3}일차에 {sum:C}원이 됩니다.");
for (sum = 0,days=1,money = 1000 ; ;days++,money *= 2)
{
sum = sum + money;
Console.WriteLine($"{days,3}일차 : {money,8:C}, sum = {sum,11:C}");
if (sum>=1000000)
{
break;
}
}
Console.WriteLine("");
Console.WriteLine($"{days,3}일차에 {sum:C}원이 됩니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
namespace _20200824_BMI계산기
{
class Program
{/// <summary>
/// BMI <20, 저체중
/// 20 <= BMI < 25 정상채중
/// 25 <= BMI < 30 경도비만
/// 30 <= BMI < 40 비만
/// BMI >= 40, 고도비만
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("키를입력하세요 ");
double height = double.Parse(Console.ReadLine());
height = height / 100; // m단위 ?구지?
Console.Write("체중을 입력하세요(kg) : ");
double weight = double.Parse(Console.ReadLine());
double BMI = weight / (height * height); //나누기 할때 이 괄호가 필요하네
string comment = null;
if (BMI<20)
{
Console.WriteLine($"{BMI:F1}" + "저체중입니다");
comment = "저체중입니다.";
}
else if (BMI<25)
{
Console.WriteLine($"{BMI:F1}"+"정상체중입니다.");
comment = "정상체중입니다.";
}
else if (BMI<30)
{
Console.WriteLine($"{BMI:F1}"+"경도비만입니다.");
comment = "경도비만입니다.";
}
else if (BMI < 40)
{
Console.WriteLine($"{BMI}"+"비만입니다.");
comment = "비만입니다.";
}
else
{
Console.WriteLine($"{BMI:F1}"+"고도비만입니다.");
comment = "고도비만입니다.";
}
Console.WriteLine("BMI={0:F1}, \"{1}\"입니다",BMI,comment);
}
}
핵심은 BMI = 몸무게 / (키 *키) 이 공식을 적용하여 작성
height = height / 100; -> cm키를 m로 변환해서 적어야 한다
Console.WriteLine($"{BMI:F1}"+"고도비만입니다.");
using System;
namespace _20200824부터100까지_더하는_프로그램
{
class Program
{
static void Main(string[] args)
{
//x의 y승 구하기
Console.WriteLine("x의 y승을 계산합니다.");
Console.Write("x을 입력하세요 " );
int x = int.Parse(Console.ReadLine());
Console.Write("y을 입력하세요 ");
int y = int.Parse(Console.ReadLine());
int c = 1;
for (int i = 0; i < y; i++)
{
c = c * x; // c변수는 x를 y번 반복해서 곱해주는거
}
Console.WriteLine($"{x}의 {y}승은 {c}입니다.");
}
}
}
여기서 핵심은 제곱승인데 x를 반복해서 곱해야한다는 부분이다 이걸 어떻게 구현하냐인데
for문을 통해 y번 반복하게 하고 1로 초기화 시킨 c에다가 y번 반복으로 x를 곱하게 만들어야 한다.
namespace 팩토리얼_구하기
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("n!을 계산합니다");
Console.WriteLine("정수 n을 입력하세요 : ");
int n = int.Parse(Console.ReadLine());
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
Console.WriteLine($"{n}! = {fact}");
}
}
}
원주율의 계산
라이프니츠의 원주율 공식을 프로그램합니다.
파이 = 4(1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 .....)
using System;
namespace ConsoleApp2
{
class Program
{/// <summary>
/// 파이 = 4(1/1-1/3+1/5-1/7+1/9-1/11......)
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
bool sign = false;
double pi = 0;
int i;
for ( i = 1; i <= 10000; i += 2)
{
if (sign==false)
{
pi = pi + 1.0 / i;
sign = true;
}
else
{
pi = pi - 1.0 / i;
sign = false;
}
Console.WriteLine($"i = {i}, pi = {4 * pi}");
}
}
}
}
무한 반복문 break로 벗어나기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 무한루프와_브레이크_문
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int days = 1;
int money = 1000;
while (true)
{
sum = sum + money;
Console.WriteLine($"{days,3}일차 : {money,8:C}, sum = {sum,11:C}");
if (sum>=1000000)
{
break;
}
days++;
money = money * 2;
}
Console.WriteLine("");
Console.WriteLine($"{days,3}일차에 {sum:C}원이 됩니다.");
for (sum = 0,days=1,money = 1000 ; ;days++,money *= 2)
{
sum = sum + money;
Console.WriteLine($"{days,3}일차 : {money,8:C}, sum = {sum,11:C}");
if (sum>=1000000)
{
break;
}
}
Console.WriteLine("");
Console.WriteLine($"{days,3}일차에 {sum:C}원이 됩니다.");
}
}
}
팩토리열 중첩 for문 !!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.Write("숫자를 입력하세요 : ");
int n = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= n; i++)
{
int fact = 1;
for (int j = 2; j <= i; j++)
{
fact = fact * j;
}
sum = sum + fact;
Console.WriteLine($"{i,2}! = {fact,10:#,#}");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1000까지의_소수를_출력하고_몇개인지_출력
{
class Program
{
static void Main(string[] args)
{
int x;
int y = 0;
for (int i = 2; i < 1000; i++)
{
for (x = 2; x< i; x++)
{
if (i%x==0) //나눠 지면 소수가 아니다 .
{
break;
}
}
if (x==i)
{
y++;
Console.WriteLine($"{i,5}");
}
}
Console.WriteLine("2부터 1000사이의 소수의 개수 : {0}개", y);
}
}
}
이중 루프와 피라미드 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
//(2)
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 2*i-1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 5; i >=1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
반응형