In: Computer Science
c# language
Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will look like this:
The number you entered is odd.
5
4
3
2
1
0
If the user enters 6, the output will look like this:
The number you entered is even.
6
4
2
0
Submit your .c file. Also submit a screen shot of the terminal with the output displayed.
//code using printf and scanf
#include<stdio.h>
int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
//even nnumber check
if(num%2==0)
{
printf("The number you entered is even\n");
while(num>=0)
{
printf("%d\n",num);
num=num-2;
}
}
//odd number check
else if(num%2!=0)
{
printf("The number you entered is odd\n");
while(num>=0)
{
printf("%d\n",num);
num=num-1;
}
}
}
using System;
class Program
{
static void Main() {
int num;
Console.WriteLine("Enter number: ");
num = Convert.ToInt32(Console.ReadLine());
//even nnumber check
if(num%2==0)
{
Console.WriteLine("The number you entered is even");
while(num>=0)
{
Console.WriteLine(num);
num=num-2;
}
}
//odd number check
else if(num%2!=0)
{
Console.WriteLine("The number you entered is odd");
while(num>=0)
{
Console.WriteLine(num);
num=num-1;
}
}
}
}
Enter number: 51 The number you entered is odd
Enter number: 6 The number you entered is even