Exp-4 B Sanskark f035
Exp-4 B Sanskark f035
Experiment: 4
PART B
Students must execute all the programs, write executed code in the workbook, and submit
part B of experiment 4 on the student portal. The filename should be
PPS_batch_rollno_experimentno. Example: PPS_A1_A001_P4
Tasks:
12 + 22 + 32 +…. N2
2. Write a program to find the sum of all numbers between M and N, where N>M, using
for loop.
3. Write a program to accept a number from the user. Find and print the sum of digits of the ✓
number. (using do-while loop)
4. Write a program that prints the first n Fibonacci numbers using a for loop.
5. Write a program to accept a number from user and display if the number is Armstrong
number. (Armstrong number is the number in any given number base, which forms the
total of the same number, when each of its digits is raised to the power of the number of
digits in the number.)
6. Write an algorithm to find a given number is palindrome or not.
Example of Palindrome number:
12321
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
565
Note:- Its number not string/character array…
7. Write a program to check whether the entered number is prime or not. (make use ✓
of break)
8. Write a program to print the entire uppercase and lowercase letters using a loop (use
continue).
Hint: - ASCII values of A-65, a-97 there are not alphabets from 91 to 96, these values
can be continued
9. Write a program using loop to find the Greatest Common Divisor (GCD) and Least
Common Multiple (LCM) of two numbers.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
1. Write program to find the sum of the following series using while loop
12 + 22 + 32 +…. N2
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int N;
cout << "Enter the value of N: ";
cin >> N;
int sum = 0;
int i = 1;
return 0;
}
Input Output: -
// Paste the input/output of executed code
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
2. Write a program to find the sum of all numbers between M and N, where N>M,
using for loop.
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int n , m, sum=0;
cout<<"enter the value of n";
cin>>n;
}
cout<<"sum of all numbers between m and n is"<<sum;
return 0;
}
Input Output: -
// Paste the input/output of executed code
3. Write a program to accept a number from the user. Find and print the sum of
digits of the number. (using do-while loop)
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int n, s = 0, t, d;
cout << "Enter a number: ";
cin >> n;
t = n;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
do {
d = n % 10;
s = s + d;
n = n / 10;
} while (n != 0);
cout << "Sum of digits of " << t << " is: " << s << endl;
return 0;
}
Input Output: -
// Paste the input/output of executed code
4. Write a program that prints the first n Fibonacci numbers using a for loop.
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int n;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
cout << "Enter the number of terms in the Fibonacci series: ";
cin >> n;
int a = 0, b = 1;
cout << "Fibonacci series: " << a << " " << b << " ";
return 0;
}
Input Output: -
// Paste the input/output of executed code
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
5. Write a program to accept a number from user and display if the number is
Armstrong number. (Armstrong number is the number in any given number
base, which forms the total of the same number, when each of its digits is raised
to the power of the number of digits in the number.)
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int r,sum = 0,n,temp;
cout<<"enter the value of n";
cin>>n;
n = temp;
while(n!=0)
{
r = n%10;
sum = sum + (r*r*r);
n = n/10;
}
if(sum=temp)
{
cout<<"the number is an amstrong";
}
else
{
cout<<"number is not amstrong";
}
return 0;
}
Input Output: -
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
int main() {
int revnum = 0, n, temp, r;
temp = n;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
while (n != 0) {
r = n % 10;
revnum = revnum * 10 + r;
n = n / 10;
}
if (temp == revnum) {
cout << temp << " is a palindrome." << endl;
} else {
cout << temp << " is not a palindrome." << endl;
}
return 0;
}
Input Output: -
// Paste the input/output of executed code
7. Write a program to check whether the entered number is prime or not. (make
use of break)
Executed Code: -
// Paste the executed code here
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
#include <iostream>
using namespace std;
int main() {
int num;
bool isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
if (isPrime) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
return 0;
}
Input Output: -
// Paste the input/output of executed code
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
8. Write a program to print the entire uppercase and lowercase letters using a loop
(use continue).
Hint: - ASCII values of A-65, a-97 there are not alphabets from 91 to 96, these
values can be continued
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
return 0;
}
Input Output: -
// Paste the input/output of executed code
9. Write a program using loop to find the Greatest Common Divisor (GCD) and
Least Common Multiple (LCM) of two numbers.
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;
int main() {
int n1, n2, r, gcd, lcm, tn1, tn2;
tn1 = n1;
tn2 = n2;
while (n2 != 0) {
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
r = n1 % n2;
n1 = n2;
n2 = r;
}
gcd = n1;
return 0;
}
Input Output: -
// Paste the input/output of executed code
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering