Professional Documents
Culture Documents
All Tasks
All Tasks
All Tasks
I
DEPARTMENT
Computer Programming
(CS 101 & MDE125)
# include <iostream>
int main ( )
{
double num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << num << " is positive\n";
else if (num < 0)
cout << num << " is negative\n";
}
# include <iostream>
int main ( )
{
double num;
cout << "Enter a number: ";
cin >> num;
if (num != 0)
cout << "Reverse is: " << num * -1 << endl;
else
cout << "No reverse\n";
}
# include <iostream>
int main ( )
{
int score;
cout << "Enter your test score: "; cin >> score;
switch (score/10)
{
case 10:
case 9: cout << "Your grade is an A." << endl; break;
case 8: cout << "Your grade is a B." << endl; break;
case 7: cout << "Your grade is a C." << endl; break;
case 6: cout << "Your grade is a D." << endl; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout << "Your grade is an F." << endl; break;
default: cout << "Error: score is out of range.\n";
}
cout << "Goodbye." << endl;
}
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0)
{
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
return 0;
}
# include <iostream>
using namespace std;
int main ( )
{
int x, d, sum = 0;
cout << "Enter an integer: ";
cin >> x ;
while ( x != 0 )
{
d = x % 10;
sum += d ;
x /= 10;
}
cout << "Sum: " << sum << endl;
return 0;
}
Write a program that reads 10 integers and prints the count of even
numbers using for loop.
#include <iostream>
using namespace std;
int main ()
{
int x, i, count = 0;
cout << "Enter 10 integers:\n";
for (i = 1; i <= 10; i++)
{
cin >> x;
if (x % 2 == 0)
count = count + 1;
}
if (count != 0)
cout << "Number of even numbers is: " << count << endl;
else
cout << "No even numbers!\n";
return 0;
}
# include <iostream>
using namespace std;
int main ( )
{
int x, y, min, g;
cout << "Enter two integers: ";
cin >> x >> y;
if (x < y)
min = x;
else
min = y;
for (int i = 1; i <= min; i++)
if (x % i == 0 && y % i == 0)
g = i;
cout << "GCD is" << g << endl;
return 0;
}
# include <iostream>
using namespace std;
int main ( )
{
int x, i, count = 0;
double sum = 0;
cout << "Enter 10 integers:\n";
for (i = 1; i <= 10; i++)
{
cin >> x;
if ( x % 2 != 0)
{
sum += x;
count ++;
}
}
if (count != 0)
cout << "Average of odd numbers: " << sum / count << endl;
else
cout << "No odd numbers!" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int number, total, count;
double average;
total = 0;
cout << "Enter the values, 9999 to exit: " << endl;
cin >> number;
for ( count = 0; number != 9999; count ++ )
{
total += number;
cin >> number;
}
average = (double) total / count;
cout << "The average is: " << average << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int x, fact = 1;
cout << "Enter a positive integer: ";
cin >> x;
if (x < 0)
cout << "Invalid number!\n";
else
{
for (int i = 1; i <= x; i = i + 1)
fact *= i;
cout << "Factorial = " << fact << "\n";
}
return 0;
}
# include <iostream>
using namespace std;
void square ( int, char );
int main ( )
{
int size;
char c;
cout << "Enter the size: ";
cin >> size;
cout << "Enter a character: ";
cin >> c;
square ( size, c );
return 0;
}
# include <iostream>
using namespace std;
double smallest ( double, double, double );
int main ( )
{
double x, y, z, min;
cout << "Enter three numbers: ";
cin >> x >> y >> z;
min = smallest ( x, y, z );
cout << "The smallest number is: " << min << endl;
return 0;
}
# include <iostream>
using namespace std;
bool multiple ( int, int );
int main ( )
{
int x, y;
bool m;
cout << "Enter two numbers (-1 to exit): ";
cin >> x >> y;
while ( x != -1 && y != -1 )
{
m = multiple ( x, y );
if ( m == true )
cout << y << " is a multiple of " << x << endl << endl;
else
cout << y << " is not a multiple of " << x << endl << endl;
cout << "Enter two numbers (-1 to exit): ";
cin >> x >> y;
}
return 0;
}
1- Arrays (Print-Reverse):
Write a program that reads 10 integers and prints them in a
reverse order.
# include <iostream>
using namespace std;
int main ( )
{
int x [10], i;
cout << "Enter 10 integers: " ;
for (i = 0; i < 10; i ++)
cin >> x [i];
cout << "Printing reverse: ";
for (i = 9; i >= 0; i --)
cout << x [i] << " ";
cout << endl;
return 0;
}
# include <iostream>
using namespace std;
int sum_evens (int [ ], int);
int main ( )
{
int x [10], i, sum;
cout << "Enter 10 integers: ";
for (i = 0; i <= 9; i ++)
cin >> x [i];
sum = sum_evens (x, 10);
cout << "Sum of Even Numbers = " << sum << endl;
return 0;
}
# include <iostream>
using namespace std;
double average (int x [ ], int n)
{
int i, sum = 0;
for (i = 0; i < n; i ++)
sum += x [i];
return sum * 1.0 / n;
}
int main ( )
{
int x [10], i;
cout << "Enter 10 integers: ";
for (i = 0; i <10; i++)
cin >> x [i];
cout << "Average = " << average (x, 10) << endl;
return 0;
}