0% found this document useful (0 votes)
14 views4 pages

Input A Number and Check Whether It Is Positive, Negative or Zero C++

The document contains C++ code snippets for various programming tasks, including checking if a number is positive, negative, or zero, finding the largest and smallest of three numbers, displaying a digit as a word, generating a multiplication table, calculating the average height of students, and printing numbers in specific sequences. Each task is implemented with clear input and output statements. The code illustrates fundamental programming concepts such as conditionals, loops, and user input.

Uploaded by

ammuaa108
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Input A Number and Check Whether It Is Positive, Negative or Zero C++

The document contains C++ code snippets for various programming tasks, including checking if a number is positive, negative, or zero, finding the largest and smallest of three numbers, displaying a digit as a word, generating a multiplication table, calculating the average height of students, and printing numbers in specific sequences. Each task is implemented with clear input and output statements. The code illustrates fundamental programming concepts such as conditionals, loops, and user input.

Uploaded by

ammuaa108
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Part A c++

1. Input a number and check whether it is positive, negative or zero

C++
#include <iostream>

using namespace std;

int main()

int num;

cout << "Enter a number: ";

cin >> num;

if (num > 0)

cout << "The number is Positive.";

else if (num < 0) {

cout << "The number is Negative.";

else

cout << "The number is Zero.";

return 0;

2. Input three numbers and find the largest

#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
{
cout << a << " is the largest.";
} else if (b >= a && b >= c)
{
cout << b << " is the largest.";
} else {
cout << c << " is the largest.";
}
return 0;
}
3. Input three numbers and find the smallest

#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a <= b && a <= c)
{
cout << a << " is the smallest.";
} else if (b <= a && b <= c) {
cout << b << " is the smallest.";
} else {
cout << c << " is the smallest.";8
}
return 0;
}
4. Input a digit and display the corresponding word using switch statement

#include <iostream>
using namespace std;
int main()
{
int digit;
cout << "Enter a digit (0-9): ";
cin >> digit;
switch (digit)
{
case 0: cout << "Zero"; break;
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
case 3: cout << "Three"; break;
case 4: cout << "Four"; break;
case 5: cout << "Five"; break;
case 6: cout << "Six"; break;
case 7: cout << "Seven"; break;
case 8: cout << "Eight"; break;
case 9: cout << "Nine"; break;
default: cout << "Invalid digit! Please enter 0-9.";
}
return 0;
}
5. Display the multiplication table of a number having 12 rows

#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
for (int i = 1; i <= 12; i++)
{
cout << num << " * " << i << " = " << (num * i) << endl;
}
return 0;
}
6. Input the heights of 10 students and find the average height

#include <iostream>
using namespace std;
int main()
{
float height, sum = 0, average;

cout << "Enter heights of 10 students:\n";


for (int i = 1; i <= 10; i++)
{
cout << "Student " << i << ": ";
cin >> height;
sum = sum + height;
}
average = sum / 10;
cout << "Average height is: " << average;
return 0;
}
7. To print 10 to 1
#include <iostream>
using namespace std;
int main()
{
cout << "Numbers from 10 to 1:\n";
for (int i = 10; i >= 1; i--) {
cout << i << " ";
}
return 0;
}
8. To print even numbers from 1 to 100

#include <iostream>
using namespace std;
int main() {
cout << "Even numbers from 1 to 100:\n";
for (int i = 2; i <= 100; i += 2) {
cout << i << " ";
}
return 0;
}
9. To print odd numbers from 100 to 1

#include <iostream>
using namespace std;
int main() {
cout << "Odd numbers from 100 to 1:\n";
for (int i = 99; i >= 1; i -= 2) {
cout << i << " ";
}
return 0;
}

You might also like