0% found this document useful (0 votes)
7 views5 pages

Assignment 1

assignment for c

Uploaded by

ghdnjswnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views5 pages

Assignment 1

assignment for c

Uploaded by

ghdnjswnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Exercis 01: Write a C program to decide leap year.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
int year;

printf("Input year: ");


scanf("%d", &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;

printf("Input an equation(ex: 2 + 5) >> ");


scanf("%d %c %d", &x, &op, &y);

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;

printf("Input numbers(a b c): ");


scanf("%lf %lf %lf", &a, &b, &c);

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);

printf("The roots of the quadratic equation are x1 and x2. %.2f


and %.2f.\n", x1, x2);
}
else
printf("There are no real roots of the quadratic equation.\n");
}
return 0;
}

OUTPUT
Input numbers(a b c) : 0 2 3
The root of the equation is - 1.500000.

Input numbers(a b c): 1 -4 4


The roots of the quadratic equation are x1 and x2. 2.00 and 2.00.

Input numbers(a b c): 1 -4 3


The roots of the quadratic equation are x1 and x2. 3.00 and 1.00.

Input numbers(a b c): 1 -4 9


There are no real roots of the quadratic equation.
Exercis 04: Write a C program to calculate the sum of natural
numbers from 1 to n.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
int n, sum = 0;

// Input the value of n


printf("Enter a positive integer: ");
scanf("%d", &n);

// Calculate the sum


for (int = 1; i<= n, 1++)
sum = sum + i;

// Print the sum


printf("The sum of natural numbers from 1 to %d is %d\n", n, sum);

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

* ***** *

** **** ***

*** *** *****

**** ** *******

***** * *********

You might also like