This program will ask you for two numbers and then ask for a loop type. Look at the source code to see how it works:
C++
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int num1, num2, loop;
cout << "Input the start of your loop: ";
cin >> num1;
system("CLS");
cout << "Input the end of your loop: ";
cin >> num2;
system("CLS");
cout << "Select loop type:\n\n1 - For\n2 - While\n3 - Do\n\n";
cin >> loop;
system("CLS");
if (loop == 1)
{
for (num1; num1 < num2 + 1; num1++)
{
cout << num1 << "\n";
}
}
else if (loop == 2)
{
while (num1 != num2 + 1)
{
cout << num1 << "\n";
num1++;
}
}
else if (loop == 3)
{
do
{
cout << num1 << "\n";
num1++;
} while (num1 != num2 + 1);
}
else
{
cout << "Invalid selection >: |\n\n";
}
system("PAUSE");
system("CLS");
return main();
}
UPDATE, C code added:
Code:
#include <stdio.h>
int num1, num2, loop;
int main()
{
printf("Enter the start of your loop: ");
scanf("%d", &num1);
system("cls");
printf("Enter the end of your loop: ");
scanf("%d", &num2);
system("cls");
printf("Select your loop:\n\n1 - For\n2 - While\n3 - Do While\n\n");
scanf("%d", &loop);
system("cls");
if (loop == 1)
{
for (num1; num1 < num2 + 1; num1++)
{
printf("%d\n", num1);
}
}
else if (loop == 2)
{
while (num1 != num2 + 1)
{
printf("%d\n", num1);
num1++;
}
}
else if (loop == 3)
{
do
{
printf("%d\n", num1);
num1++;
} while (num1 != num2 + 1);
}
else
{
printf("Invalid selection >: |\n\n");
}
getch();
system("cls");
return main();
}
Bookmarks