0% found this document useful (0 votes)
5 views10 pages

Assignment

The document contains a series of C++ programs that demonstrate various programming concepts, including finding the factorial of a number, checking for palindromes, Armstrong numbers, Fibonacci series, prime numbers, swapping numbers, summing digits, finding the greatest digit, checking even or odd status, and reversing a number. Each program is accompanied by code snippets that illustrate the implementation of these concepts. The document serves as a practical guide for beginners to learn basic programming techniques in C++.

Uploaded by

sameerghazi368
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)
5 views10 pages

Assignment

The document contains a series of C++ programs that demonstrate various programming concepts, including finding the factorial of a number, checking for palindromes, Armstrong numbers, Fibonacci series, prime numbers, swapping numbers, summing digits, finding the greatest digit, checking even or odd status, and reversing a number. Each program is accompanied by code snippets that illustrate the implementation of these concepts. The document serves as a practical guide for beginners to learn basic programming techniques in C++.

Uploaded by

sameerghazi368
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

1. Write a program to find factorial of a number using loop.

#include <iostream>
using namespace std ;
int main ()
{
int n ;
int fact = 1 ;
cin >> n ;
if (n == 0)
cout << 1 ;
else
for (int i = 1 ; i <= n ; i++)
{fact = fact*i ;
cout << fact ; }
return 0 ;}
2. Write a program to check whether the number is palindrome or not.
#include <iostream>
Using namespace std ;
Int main ()
{
Int n ;
Int original ;
Int reverse = 0 ;
Int reminder ;
Cin >> n ;
Original = n ;
While ( n != 0)
{
Reminder = n%10 ;
Reverse = reverse*10 + reminder ;
N = n/10 ;
}
If (reverse = original)
Cout << “It is a palindrome number” ;
Else
Cout << “It is not a palindrome number “ ;return 0 ;
}
3. Write a program to check whether the number is armstrong or not.

#include <iostream>
#include <cmath>
using namespace std ;
int main ()
{
int n ;
int reminder = 0 ;
int expo ;
int original ;
int power = 0 ;
cin >> n ;
n = original ;
for (int i = 0 ; n != 0 ; i ++)
{
n = n/10 ;
power++ ;
}
while (n !=0 )
{
reminder = n/10 ;
expo += pow(reminder, power) ;
original /= 10 ;
}
if ( original == n)
cout << "It is an armstrong number" ;
else
cout << "It is not an armstrong" ;
return 0 ;
}
4. Write a program to print Fibonacci series upto N terms.
#include <iostream>
Using namespace std;

Int main() {
Int n, a = 0, b = 1, next;

Cout << “Enter number of terms: “;


Cin >> n;

Cout << “Fibonacci Series: “;

For(int i = 1; i <= n; i++) {


Cout << a << “ “ ;
Next = a + b;
A = b;
B = next;
}

Return 0;
}
5. Write a program to check whether the a number is prime or not.
#include<iostream>
Using namespace std ;
Int main ()
{
Int n ;
Int i ;
Cin >> n ;
If (n == 1)
Cout << “it is not a prime number” ;
For ( i = 2 ; i < n ; i++)
{
If (n%i == 0)
Break ; }
If (i == n)
Cout << “it is a prime number” ;
Else
Cout << “it is not a prime number” ;
Return 0 ;
}
6. Write a program to swap two numbers using functions.
#include<iostream>
Using namespace std ;
Void swap (int &x, int &y)
{
Int temp ;
Temp = x ;
X=y;
Y = temp ;
Cout << x << endl ;
Cout << y ;
}
Int main ()
{
Int x ;
Int y ;
Cin >> x ;
Cin >> y ;
Swap (x, y) ;
Return 0 ;
}
7. Write a program to calculate sum of digits of the number.
#include<iostream>
Using namespace std ;
Int main ()
{
Int n ;
Int reminder ;
Int add = 0 ;
Cin >> n ;
While (n != 0)
{
Reminder = n % 10 ;
Add = add + reminder ;
N = n/10 ;
}
Cout << “Addition of digits = “ << add ;
Return 0 ;
}
8. Write a program to find greatest digit in a number.
#include<iostream>
Using namespace std ;
Int main ()
{
Int n ;
Int r ;
Int temp = 0 ;
Cin >> n ;
While (n != 0)
{
R = n%10 ;
N = n/10 ;
If ( r > temp)
Temp = r ;
}
Cout << “Greatest digit = “ << temp ;
Return 0 ;
}
9. Write a program to check whether a number is even or odd using functions.

#include<iostream>
using namespace std ;
void check (int n)
{
if(n%2 == 0)
cout << "it is an even number" ;
else
cout << "it is an odd number" ;
}
int main ()
{
int n ;
cin >> n ;
check(n) ;
return 0 ;
}
10. Write a program to reverse a number without using built-in functions.

#include <iostream>
using namespace std ;
int main ()
{
int n ;
int original ;
int reverse = 0 ;
int reminder ;
cin >> n ;
original = n ;
while ( n != 0)
{
reminder = n%10 ;
reverse = reverse*10 + reminder ;
n = n/10 ;
}
cout << reverse ;
return 0 ;
}

You might also like