Assignment 1
Assignment 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int year;
if ( year == 2000 )
printf("%d is leap year.\n", year);
else
printf("%d is not leap year.\n", year);
return 0;
}
OUTPUT
Input year : 2000
2000 is leap year.
Exercis 02: Write a a simple arithmetic calculator in C.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
char op;
int x, y, result;
switch (op)
{
case '+':
result = x + y;
printf(”%d + %d = %d\n”, x, y, result)
break;
case '-':
result = x - y;
printf(”%d - %d = %d”, x, y, result)
break;
case '*':
result = x * y;
printf(”%d * %d = %d”, x, y, result)
break;
case '/':
result = x / y;
printf(”%d / %d = %d”, x, y, result)
break;
case '%':
result = x % y;
printf(”%d % %d = %d”, x, y, result)
break;
default:
printf(”Error”)
break;
}
return 0;
}
OUTPUT
Input an equation(ex: 2 + 5) >> 2 + 1
2 + 1 = 3
Input an equation(ex: 2 + 5) >> 3 - 1
3 - 1 = 2
Input an equation(ex: 2 + 5) >> 3 * 3
3 * 3 = 9
Input an equation(ex: 2 + 5) >> 10 / 2
10 / 2 = 5
Input an equation(ex: 2 + 5) >> 7 % 3
7 % 3 = 1
Exercis 03: Write a C program to roots of a quadratic equation.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, dis;
double x1, x2;
if (a == 0)
{
x1 = -c / b;
printf("The root of the equation is %f.", x1);
}
else
{
dis = b * b - 4.0 * a * c;
if (dis >= 0)
{
x1 = ((-b + sqrt(dis)) / 2 * a);
x2 = ((-b - sqrt(dis)) / 2 * a);
OUTPUT
Input numbers(a b c) : 0 2 3
The root of the equation is - 1.500000.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int n, sum = 0;
return 0;
}
OUTPUT
Enter a positive integer: 100
The sum of natural numbers from 1 to 100 is 5050
Exercis 05: Write a C program to print a pyramid pattern using a for
loop.
#include <stdio.h>
int main()
{
int num = 5;
// #1 right angle
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("\n\n\n");
// #2 right angle
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - i; j++) {
printf("*");
}
printf("\n");
}
printf("\n\n\n");
// #3 pyramid
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - i - 1; j++) {
printf(" ");
}
for (int k = 0; k < 2 * i + 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT
* ***** *
** **** ***
**** ** *******
***** * *********