0% found this document useful (0 votes)
4 views86 pages

All Program

The document contains a series of programming tasks in C++ that cover various basic concepts such as checking if a number is positive or negative, determining if a year is a leap year, performing arithmetic operations, and printing patterns. Each task is accompanied by its corresponding code snippet. The tasks range from simple calculations to more complex loops and conditionals.

Uploaded by

nihalx2003
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)
4 views86 pages

All Program

The document contains a series of programming tasks in C++ that cover various basic concepts such as checking if a number is positive or negative, determining if a year is a leap year, performing arithmetic operations, and printing patterns. Each task is accompanied by its corresponding code snippet. The tasks range from simple calculations to more complex loops and conditionals.

Uploaded by

nihalx2003
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

Enrollment Num:

202502626010142

TASK

Task 1:Check if a number is positive, negative or


zero.

Code:

#include<iostream>

using namespace std;


int main()

int a;

cout<<"enter number";

cin>>a;

if(a>0){

cout<<"number is positive";

else if(a<0){

cout<<"number is negative";
}

else{

cout<<"number is zero";

return 0;

TASK 2: Check if a number is even or odd.


Code:

#include<iostream>

using namespace std;

int main()

int a;

cout<<"enter year";
cin>>a;

if(a%4==0){

cout<<"year is leap year";

else{

cout<<"year is not leap year";

return 0;

}
TASK 3:Check if a year is a leap year or not.

Code:

#include<iostream>

using namespace std;

int main()

int a;

cout<<"enter number";
cin>>a;

if(a%2==0){

cout<<"number is even";

else if(a%2!=0){

cout<<"number is odd";

else{

cout<<"number is zero";

}
return 0;

TASK 4:. Check eligibility to vote.

Code:

#include<iostream>

using namespace std;

int main()
{

int a;

cout<<"enter your age";

cin>>a;

if(a<18){

cout<<"you are not eligible for vote";

else if(a>60){

cout<<"you are eligible for vote and seniorcitizen";

}
else if(18<a<60){

cout<<"you are eligible for vote amd not


seniorcitizen";

return 0;

TASK 5:Print your name and age.


Code:

#include<iostream>

using namespace std;

int main()

int num1,num2;

cout<<"enter number1:";

cin>>num1;

cout<<"enter number2:";

cin>>num2;
cout<<"sum is:"<<num1+num2;

return 0;

TASK 6:Add two numbers.

Code:

#include<iostream>
using namespace std;

int main()

string a;

int b;

cout<<"enter your name:";

cin>>a;

cout<<"enter your age:";

cin>>b;

cout<<"Name is:"<<a<<endl;
cout<<"Age is:"<<b;

return 0;

TASK 7A:Find the square of given number.

Code:
#include<iostream>

using namespace std;

int main()

int a;

cout<<"enter num";

cin>>a;

cout<<"square is:"<<a*a;

return 0;

}
TASK 7B:Find the square of given number.

Code:

#include<iostream>

using namespace std;

int main()

{
int a;

cout<<"enter num";

cin>>a;

cout<<"square is:"<<a*a*a;

return 0;

}
TASK 8: Area and perimeter of Circle,
Square,Triangle and Rectangle

Code:

#include<iostream>

using namespace std;

int main(){

float square,as;

float redius;

float ac;

int lr,wr,ar;

cout<<"Enter side of square";


cin>>square;

cout<<"Enter redius of circle";

cin>>redius;

cout<<"Enter length of ractangle";

cin>>lr;

cout<<"Enter width of ractangle";

cin>>wr;

as=square*square;

ac=3.14*redius*redius;

ar=lr*wr;
cout<<"Area of square is:"<<as<<endl;

cout<<"Area of circle is:"<<ac<<endl;

cout<<"Area of ractangle is:"<<ar<<endl;

return 0;

}
TASK 9: Convert Celsius to Fahrenheit and vice
versa.

Code:

#include<iostream>

using namespace std;

int main(){

float C, F;

cout << "Enter temp in ce: " << endl;

cin >> C;
F = (C * 9/5) + 32;

cout << "fe: " << F;

return 0;

TASK 10:. Find the factorial of a given number

Code:

#include<iostream>
using namespace std;

int main(){

int a;

int b=1;

cout<<"Enter a number to find factorial";

cin>>a;

for(int i=1;i<=a;i++){

b=b*i;

}
cout<<b;

return 0;

Task 11:Convert kilometers to meters

Code:

#include<iostream>
using namespace std;

int main(){

float a,m;

cout<<"Enter meter in KM";

cin>>a;

m=a*1000;

cout<<"KM into meter is:"<<m;

return 0;

}
Task 12:Print numbers from N to 1 using a loop.

Code:

#include<iostream>

using namespace std;

int main(){

int a;
cout<<"Enter a number";

cin>>a;

for(int i=a;i>=0;i--){

cout<<i<<endl;

return 0;

}
Task 13:Find the sum of all even numbers
between 1 and 100Task 13:Find the sum of all
even numbers between 1 and 100

Code:

#include<iostream>

using namespace std;

int main(){

int sum=0;

for(int i=0;i<=100;i++){

if(i%2==0){
sum=sum+i;

else{

continue;

cout<<sum;

return 0;

}
Task 14:Create a simple calculator using switch
case (add, subtract, multiply, divide)

Code:

#include <iostream>

using namespace std;

int main() {
char op;

double num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

cout << "Enter an operator (+, -, *, /): ";

cin >> op;

switch (op) {

case '+':
cout <<num1<<"+"<<num2<<"="
<<num1+num2<<endl;

break;

case '-':

cout <<num1<<"-"<<num2<<"="<<num1-
num2<<endl;

break;

case '*':

cout <<num1<<"*"<<num2<<"="
<<num1*num2<<endl;

break;

case '/':

if (num2 != 0) {
cout << num1 <<"/"<< num2 << " = " <<
num1 / num2 <<endl;

} else {

cout << "Division by zero is not possible."


<<endl;

break;

default:

cout << "Error! The operator is not valid."


<<endl;

break;

}
return 0;

Task 15:Write a function to find the sum of digits


of a number.

Code:
#include<iostream>

using namespace std;

int main(){

int a;

int sum=0;

int dig;

cout<<"Enter a number";

cin>>a;

while(a>0){

dig=a%10;
sum=sum+dig;

a=a/10;

cout<<sum;

return 0;

}
Task 16:Count positive, negative, and zero
numbers from user input

Code:

#include <iostream>

using namespace std;

int main() {

int n, num;

int positive = 0, negative = 0, zero = 0;

cout << "Enter how many numbers: ";


cin >> n;

for(int i = 0; i < n; i++) {

cout << "Enter number " << i + 1 << ": ";

cin >> num;

if(num > 0)

positive++;

else if(num < 0)

negative++;

else
zero++;

cout << "\nPositive numbers: " << positive <<


endl;

cout << "Negative numbers: " << negative <<


endl;

cout << "Zero numbers: " << zero << endl;

return 0;

}
Task 17:Print the multiplication table of a given
number

Code:

#include<iostream>
using namespace std;

int main(){

int a;

cout<<"Enter a number :";

cin>>a;

for(int i=1;i<=10;i++){

cout<<a<<" X " <<i<<" = "<<a*i<<endl;

return 0;

}
Task 18: Check whether a character is a vowel or
consonant.

Code:

#include<iostream>

using namespace std;


int main()

char a;

cout<<"Enter a char:";

cin>>a;

if(a=='a'|| a=='e'|| a=='o'|| a=='i'|| a=='u'){

cout<<"vowels";

else{

cout<<"consonants";

}
return 0;

Task 19:Reverse a given number

Code:

#include<iostream>

using namespace std;

int main()
{

int a,digit,rev=0;

cout<<"Enter a number";

cin>>a;

while(a>0){

digit=a%10;

rev=rev*10+digit;

a=a/10;

cout<<rev;
return 0;

Task 20:Find the product of all odd numbers


between 1 and 10

Code:

#include<iostream>

using namespace std;

int main()
{

int product=1;

for(int i=1;i<=10;i++){

if(i%2!=0){

product*=i;

else{

continue;

cout<<"Product is:"<<product;
return 0;

Task 21:Right Angle Triangle

Code:

#include<iostream>

using namespace std;

int main()
{

for(int i=1;i<=4;i++){

for(int j=1;j<=i;j++){

cout<<"*";

cout<<endl;

return 0;

}
Task 22:Number Triangle

Code:

#include<iostream>

using namespace std;

int main()

for(int i=1;i<5;i++){

for(int j=1;j<=i;j++){
cout<<j<<" ";

cout<<endl;

return 0;

Task 23:Rectangle

Code:
#include<iostream>

using namespace std;

int main()

for(int i=1;i<=4;i++){

for(int j=1;j<=4;j++){

cout<<"*";

cout<<endl;

}
return 0;

Task 24:Reverse Number Triangle

Code:

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 4; i++) {

for (int j = i; j >= 1; j--) {

cout << j;

cout << endl;

return 0;

Task 25:Inverted Star Triangle


Code:

#include<iostream>

using namespace std;

int main(){

for(int i=4;i>=1;i--){

for(int j=i;j>=1;j--){

cout<<"*";

}
cout<<endl;

return 0;

Task 26:Pyramid

Code:

#include<iostream>

using namespace std;


int main(){

int n=10;

for(int i=1;i<=4;i++){

for (int s = n - i; s > 0; s--) {

cout << " ";

for(int j=1; j<=i; j++){

cout<<"* ";

cout << endl;

}
Task 27:Hollow Square

Code:

#include<iostream>

using namespace std;

int main(){

for (int i=1;i<=4;i++){

for(int j=1;j<=4;j++){

if(i==1 or i==4 or j==1 or j==4){

cout<<"* ";
}

else{

cout<<" ";

cout<<endl;

return 0;

}
Task 28:Diamond

Code:

#include<iostream>

using namespace std;

int main(){

int n=10;

for(int i=1;i<=4;i++){

for(int k=n-i;k>=1;k--){

cout<<" ";

for(int j=1;j<=i;j++){

cout<<"* ";
}

cout<<endl;

for(int i=3;i>=1;i--){

for(int k=n-i;k>=1;k--){

cout<<" ";

for(int j=1;j<=i;j++){

cout<<"* ";

cout<<endl;

return 0;

}
Task 29:Floyd’s Triangle

Code:

#include<iostream>

using namespace std;

int main(){

int num=1;
for(int i=1;i<=4;i++){

for(int j=1;j<=i;j++){

cout<<num<<" ";

num++;

cout<<endl;

return 0;

}
Task 30:

Code:

#include <iostream>

using namespace std;

int main() {

int n;

cin >> n;

for (int i = 0; i < n; i++) {

for (int space = 0; space < n - i - 1; space++)

cout << " ";


int val = 1;

for (int j = 0; j <= i; j++) {

cout << val << " ";

val = val * (i - j) / (j + 1);

cout << endl;

return 0;

}
Task31:Number Pyramid

Code:

#include <iostream>

using namespace std;

int main() {

int n=10;

for(int i=1;i<=4;i++){

for(int s=n-i;s>=1;s--){

cout<<" ";
}

for(int j=1;j<=i;j++){

cout<<j<<" ";

cout<<endl;

return 0;

}
Task 32: Right Aligned Triangle

Code:

#include<iostream>

using namespace std;

int main(){

int n=10;

for(int i=1;i<=7;i++){

for (int s = n - i; s > 0; s--) {

cout << " ";

for(int j=1; j<=i; j++){


cout<<"*";

cout << endl;

Task 33:Inverted
Code:

#include <iostream>

using namespace std;

int main() {

int n=10;

for(int i=4;i>=1;i--){

for(int j=1;j<=i;j++){

cout<<j;

cout<<endl;

return 0;

}
Task 34: Alphabet Triangle

Code:

#include <iostream>

using namespace std;

int main() {
char v[4]={'A','B','C','D'};

for(int i=0;i<=3;i++){

for(int j=0;j<=i;j++){

cout<<v[j];

cout<<endl;

return 0;

}
Task 35:Continuous Number Triangle

Code:

#include <iostream>

using namespace std;

int main() {

int num=1;

for(int i=1;i<=4;i++){

for(int j=1;j<=i;j++){

cout<<num;

num++;

}
cout<<endl;

return 0;

Task 36:Hollow Triangle

Code:

#include <iostream>

using namespace std;


int main() {

int n=10;

for(int i=1;i<=4;i++){

for (int s = n - i; s > 0; s--) {

cout << " ";

for(int j=1;j<=i;j++){

if(j==1 or j==i or i==4){

cout<<"* ";

else{

cout<<" ";

}
}

cout<<endl;

return 0;

Task 37:
Code:

#include <iostream>

using namespace std;

int main() {

int n=10;

for(int i=4;i>1;i--){

for (int s = n - i; s > 0; s--) {

cout << " ";

for(int j=i;j>=1;j--){

if(j==1 or j==i or i==4){

cout<<"* ";

}
else{

cout<<" ";

cout<<endl;

for(int i=1;i<=4;i++){

for (int s = n - i; s > 0; s--) {

cout << " ";

for(int j=1;j<=i;j++){

if(j==1 or j==i or i==4){

cout<<"* ";
}

else{

cout<<" ";

cout<<endl;

return 0;

}
Task 38: Left Triangle

Code:

#include <iostream>

using namespace std;

int main() {

for(int i=1;i<4;i++){

for(int j=1;j<=i;j++){

cout<<"*";

cout<<endl;
}

for(int i=4;i>=1;i--){

for(int j=i;j>=1;j--){

cout<<"*";

cout<<endl;

return 0;

}
Task 39:. Binary Triangle

Code:

#include <iostream>

using namespace std;

int main() {

for(int i=1;i<=4;i++){

for(int j=1;j<=i;j++){

if((i+j)%2==0){

cout<<"1";

else{
cout<<"0";

cout<<endl;

return 0;

}
Task 40:. Zigzag Pattern. Zigzag Pattern. Zigzag
Pattern. Zigzag Pattern

#include<iostream>

using namespace std;

int main() {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 9; j++) {

if ((i + j) % 4 == 0 || (i == 2 && j % 4 == 0))

cout << "* ";

else

cout << " ";

cout << endl;

}
return 0;

You might also like