카테고리 없음
C# 값 입력 출력 합 평균
setposs
2022. 10. 20. 19:50
C# 값 입력 출력 합 평균
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsReadWrite
{
class Program
{
static void Main(string[] args)
{
int N = 2;
int i, j, num, sum;
string str;
Console.WriteLine("임의의 자료를 한 줄 입력하세요");
str = Console.ReadLine();
Console.WriteLine(str);
Console.WriteLine("두개의 정수를 입력하는데 한줄에 하나씩 입력하세요");
for (i = 0, sum = 0; i < N; i++)
{
str = Console.ReadLine();
str.Trim();
num = Int32.Parse(str);
Console.WriteLine(num);
sum = sum + num;
}
Console.WriteLine("두수의합은 {0}입니다", sum);
Console.WriteLine("한줄에 두개의 정수 입력하세요");
int intch;
char ch;
int[] elem = new int[N];
for (i = 0, sum = 0; i < 2; i++)
{
intch = Console.Read();
ch = Convert.ToChar(intch);
num = 0;
while (ch != ' ' && ch != '\r')
{
num = num * 10 + (intch - 48);
intch = Console.Read();
ch = Convert.ToChar(intch);
};
elem[i] = num;
Console.WriteLine(" {0} ", elem[i]);
sum += num;
}
Console.WriteLine(sum);
intch = Console.Read();
Console.WriteLine("정수 3개를 한줄에 입력하세요");
string[] token;
double[] tab = new double[10];
double tot = 0, avg;
str = null;
str = Console.ReadLine();
{
token = str.Split(' ');
for (j = 0; j < token.Length; j++)
token[j].Trim();
for (j = 0; j < token.Length; j++)
tab[j] = Convert.ToDouble(token[j]);
for (j = 0, tot = 0; j < token.Length; j++)
tot += tab[j];
avg = tot / token.Length;
Console.WriteLine(" total={0} avg = {1}", tot, avg);
}
string[] title = new string[5] { "번호", "이름", "전화번호", "국어", "수학" };
Console.WriteLine("please give your data");
for (j = 0; j < 5; j++)
Console.Write("{0}", title[j]);
Console.WriteLine();
intch = Console.Read();
str = Console.ReadLine();
token = str.Split(' ');
for (j = 0; j < token.Length; j++)
token[j].Trim();
for (j = 3; j < token.Length; j++)
tab[j] = Convert.ToDouble(token[j]);
for (j = 3, tot = 0; j < token.Length; j++)
tot += tab[j];
avg = tot / 2.0;
Console.WriteLine("total = {0} avg = {1}", tot, avg);
}
}
}