0% found this document useful (0 votes)
10 views7 pages

DECFFD41

Uploaded by

huzaifakadri95
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)
10 views7 pages

DECFFD41

Uploaded by

huzaifakadri95
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/ 7

FYCS02 C++ Programming

Practical – IIIa

1. WAP to print the first n integers [Accept n from the user]

#include<iostream.h>
#include<conio.h>
void main(){
int n,i=1;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
cout<<"The first "<<n<<" integers are:"<<endl;
while(i<=n){
cout<<i<<" ";
i++;
}
getch();
}
Output:

2. WAP to print the first n even numbers [Accept n from the user]

#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
while(n>0){
if(n%2==0)
cout<<n<<" ";
--n;
}
getch();
}
Output:
FYCS02 C++ Programming

3. WAP to print all the even numbers less than n [Accept n from the user]

#include<iostream.h>
#include<conio.h>
void main(){
int n,i=2;
clrscr();
cout<<"Enter the value of n:"<<endl;
cin>>n;
while(i<n){
cout<<i<<" ";
i+=2;
}
getch();
}
Output:

4. WAP to accept a number from the user and print the sum of all numbers less than that
number.

#include<iostream.h>
#include<conio.h>
void main(){
int n,s=0,i=1;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
while(i<n){
s=s+i;
++i;
}
cout<<"The sum of all the number less than "<<n<<" is:"<<s<<endl;
getch();
}
Output:
FYCS02 C++ Programming

5. WAP to accept a number from the user and print the sum of all odd numbers less than
that number.

#include<iostream.h>
#include<conio.h>
void main(){
int n,i=1,s=0;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
while(i<n){
if(i%2!=0){
s+=i;
}
i++;
}
cout<<"The odd number less than "<<n<<" is:"<<s<<endl;
getch();
}
Output:

6. WAP to accept a number from the user. Print the sum of digits of that number.

#include<iostream.h>
#include<conio.h>
void main(){
int n,sum=0;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
while(n>0){
sum+=n%10;
n=n/10;
}
cout<<"sum of digits:"<<sum<<endl;
getch();
}
Output:
FYCS02 C++ Programming

7. WAP to accept a number and find its factorial.

#include<iostream.h>
#include<conio.h>
void main(){
int n,f=1,i=1;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
if(n<0){
cout<<"Factorial is not defined for negative numbers."<<endl;
}else{
while(i<=n){
f*=i;
i++;
}
cout<<"Factorial is:"<<f<<endl;
}
getch();
}
Output:

8. WAP to accept a number and check if it is an Armstrong number [Hint: In an


Armstrong number, the sum of the cubes of the digits of the number is the number
itself. Eg 153 is an Armstrong number because 13+53+33=153]

#include<iostream.h>
#include<conio.h>
void main(){
int n,n1,r,arm=0;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
n1=n;
while(n>0){
r=n%10;
arm=arm+(r*r*r);
n=n/10;
}
FYCS02 C++ Programming

if(n1==arm){
cout<<n1<<" is an Armstrong Number."<<endl;
}else{
cout<<n1<<" is not an Armstrong Number."<<endl;
}
getch();
}
Output:

9. WAP to accept a number and print the Fibonacci series up till that number
[Hint:Fibonacci series: 1, 1, 2, 3, 5, 8, 13, … every term in the Fibonacci series is the
sum of the previous two terms. The first 2 terms are fixed -> 1, 1]

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main(){
int x,n,i=0;
long double s=0;
clrscr();
cout<<"Enter x and n:"<<endl;
cin>>x>>n;
while(i<=n){
s=s+pow(x,i);
i++;
}
cout<<"The sum of series is:"<<s<<endl;
getch();
}
Output:

10. WAP to accept a number and check if it is a Perfect number [Hint: A Perfect number
is the sum of its factors. Eg 6 is a perfect number 6 = 1 + 2 + 3, where 1, 2 and 3 are
the factors of 6]

#include<iostream.h>
#include<conio.h>
void main(){
FYCS02 C++ Programming

int n,s=0,i=1;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
while(i<n){
if(n%i==0)
s+=i;
i++;
}
cout<<n<<(s==n?" is a perfect number":" is not a perfect number");
getch();
}
Output:

11. WAP to accept a number and check if it is a prime number [Hint: A prime number is
only divisible by 1 and the number itself]

#include<iostream.h>
#include<conio.h>
void main(){
int n,i=2,flag=0;
clrscr();
cout<<"Enter a positive integer:"<<endl;
cin>>n;
if(n<=1){
cout<<n<<" is not a prime number."<<endl;
}else{
while(i<=n/2){
if(n%i==0){
flag=1;
break;
}
i++;
}
if(flag==0){
cout<<n<<" is a prime number."<<endl;
}else{
cout<<n<<" is not a prime number."<<endl;
}
}getch();
}
FYCS02 C++ Programming

Output:

You might also like