C program 1 to 10
C program 1 to 10
"
Code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Output:
Hello, World!
---
Code:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
int sum = a + b;
cout << "Sum: " << sum;
return 0;
}
Output:
Sum: 8
---
Code:
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20, temp;
temp = x;
x = y;
y = temp;
cout << "x = " << x << ", y = " << y;
return 0;
}
Output:
x = 20, y = 10
---
Code:
#include <iostream>
using namespace std;
int main() {
int num = 7;
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}
Output:
Odd
---
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 8;
if (a > b)
cout << a << " is greater";
else
cout << b << " is greater";
return 0;
}
Output:
12 is greater
---
Code:
#include <iostream>
using namespace std;
int main() {
char op = '+';
int a = 10, b = 5;
switch(op) {
case '+': cout << "Sum = " << a + b; break;
case '-': cout << "Difference = " << a - b; break;
case '*': cout << "Product = " << a * b; break;
case '/': cout << "Quotient = " << a / b; break;
default: cout << "Invalid operator";
}
return 0;
}
Output:
Sum = 15
---
Code:
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 10; i++) {
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
---
Code:
#include <iostream>
using namespace std;
int main() {
int n = 5, sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 15
---
Code:
#include <iostream>
using namespace std;
int main() {
int n = 5;
long long fact = 1;
Output:
Factorial = 120
---
Code:
#include <iostream>
using namespace std;
int main() {
int n = 5, a = 0, b = 1;
return 0;
}
Output:
01123