0% found this document useful (0 votes)
85 views8 pages

Basic C++ Programs

The document contains 12 C++ programs that demonstrate basic programming concepts like: 1) Checking if a number is even or odd using if/else statements. 2) Calculating the factorial of a number using a for loop. 3) Finding the sum of natural numbers using a for loop. 4) Printing prime numbers between 1 to 100 using nested for loops. 5) Checking if a number is a palindrome using loops.

Uploaded by

Manushi Khatri
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)
85 views8 pages

Basic C++ Programs

The document contains 12 C++ programs that demonstrate basic programming concepts like: 1) Checking if a number is even or odd using if/else statements. 2) Calculating the factorial of a number using a for loop. 3) Finding the sum of natural numbers using a for loop. 4) Printing prime numbers between 1 to 100 using nested for loops. 5) Checking if a number is a palindrome using loops.

Uploaded by

Manushi Khatri
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
You are on page 1/ 8

Simple C++ programs

Program 1. Check Whether Number is Even or Odd using if else


#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}

Program 2. Write a program in C++, that finds factorial of a given number


using for loop.

#include<iostream>
using namespace std;
int main()
{
int num, i, fact=1;
cout<<"Enter the Number: ";
cin>>num;

for(i=num; i>=1; i--)


fact = fact*i;

cout<<"\nFactorial = "<<fact;
cout<<endl;
return 0;
}
Program 3. Write a program in C++ that finds the Sum of N Natural Numbers
using loop.
#include<iostream>
using namespace std;

int main()
{
int n, natural, sum=0;
cout<<"Enter the Value of n: ";
cin>>n;
for(natural=1; natural<=n; natural++)
sum = sum+natural;
cout<<"\nSum of First "<<n<<" Natural Numbers = "<<sum;
cout<<endl;
return 0;
}

Program 4. Write a program in C++ to print prime numbers from 1 to 100.

#include<iostream>
using namespace std;
int main()
{
int i, chk=0, j;
cout<<"Prime Numbers Between 1 to 100 are:\n";
for(i=1; i<=100; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{
chk++;
break;
}
}
if(chk==0 && i!=1)
cout<<i<<endl;
chk = 0;
}
cout<<endl;
return 0;
}

Program 5. Write a program in C++ that checks if the number is a palindrome


number or not.
#include<iostream>
using namespace std;
int main()
{
int num, rev=0, rem, temp;
cout<<"Enter the Number: ";
cin>>num;
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
if(rev==num)
cout<<"\nIt is a Palindrome Number";
else
cout<<"\nIt is not a Palindrome Number";
cout<<endl;
return 0;
}

Program 6. Write a program in C++ that checks whether a given number by


user, is an Armstrong number or not.
#include<iostream>
using namespace std;
int main()
{
Int num, temp, noOfDigit=0, res=0, rem, pow, i;
cout<<"Enter the Number: ";
cin>>num;
temp = num;
while(num>0)
{
num = num/10;
noOfDigit++;
}
num = temp;
while(num>0)
{
rem = num%10;
pow = 1;
i = 0;
while(i<noOfDigit)
{
pow = pow*rem;
i++;
}
res = res + pow;
num = num/10;
}
if(res==temp)
cout<<"\nIt is an Armstrong Number";
else
cout<<"\nIt is not an Armstrong Number";
cout<<endl;
return 0;
}

Program 7. Write a program in C++ to input elements in an array and to


display them.

#include<iostream>
using namespace std;
int main()
{
int arr[50], size, i;
cout<<"Enter the Size: ";
cin>>size;
cout<<"Enter "<<size<<" Numbers: ";
for(i=0; i<size; i++)
cin>>arr[i];
cout<<"\nArray with Index\tIts Value\n";
for(i=0; i<size; i++)
cout<<"arr["<<i<<"]"<<"\t\t\t"<<arr[i]<<endl;
cout<<endl;
return 0;
}

Program 8. Write a program in C++ to find and print largest number in an


array.
#include<iostream>
using namespace std;
int main()
{
int arr[100], size, larg, i;
cout<<"Enter the Size (max. 100): ";
cin>>size;
cout<<"Enter "<<size<<" Array Elements: ";
for(i=0; i<size; i++)
cin>>arr[i];
larg = arr[0];
for(i=1; i<size; i++)
{
if(larg<arr[i])
larg = arr[i];
}
cout<<"\nLargest Number = "<<larg;
cout<<endl;
return 0;
}

Program 9. Write a program in C++ to calculate the length of a string.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[200];
int len=0, i=0;
cout<<"Enter the String: ";
gets(str);
while(str[i])
{
len++;
i++;
}
cout<<"\nLength = "<<len;
cout<<endl;
return 0;
}

Program 10. Write a program in C++ to print


*
**
***
****
*****
******
#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}

Program 11. Write a program to Print Full Pyramid Pattern of Stars (*)

*
***
*****
*******
*********
***********

#include<iostream>
using namespace std;
int main()
{
int i, space, k=0;
for(i=1; i<=6; i++)
{
for(space=1; space<=(6-i); space++)
cout<<" ";
while(k!=(2*i-1))
{
cout<<"* ";
k++;
}
k=0;
cout<<endl;
}
cout<<endl;
return 0;
}

Program 12. Write a program to print Fibonaccci Series

#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printe
d
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}

You might also like