0% found this document useful (0 votes)
14 views15 pages

Exp-4 B Sanskark f035

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Exp-4 B Sanskark f035

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering / School of


Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Experiment: 4
PART B

(PART B: TO BE COMPLETED AND SUBMITTED BY STUDENTS)

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

Roll No.: F019 Name: Nathan DSouza


Prog/Yr/Sem:Mechanical/1/1 Batch:F1
Date of Experiment:22/08/24 Date of Submission:22/08/24

Aim: Programming using looping and unconditional statements

Tasks:

Sr. Problem Statement Flow


No chart
.
1 Write program to find the sum of the following series using while loop

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Executed Code, Input and Output

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;

// Calculate the sum of the series using a while loop


while (i <= N) {
sum = sum + i * i;
i++;
}

// Output the result


cout << "The sum of the series is: " << sum << 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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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<<"enter the value of m";


cin>>m;
if(n>m)
{
for(int i =m ;i<=n;i++)
{
sum = sum +i;
}
}
else
{
cout<<"invalid case";
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

}
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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cout << "Enter the number of terms in the Fibonacci series: ";
cin >> n;

int a = 0, b = 1;

cout << "Fibonacci series: " << a << " " << b << " ";

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


int c = a + b;
cout << c << " ";
a = b;
b = c;
}

cout << 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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

// Paste the input/output of executed code

6. Write an algorithm to find a given number is palindrome or not.


Example of Palindrome number:
12321
565
Note:- Its number not string/character array…
Executed Code: -
// Paste the executed code here
#include <iostream>
using namespace std;

int main() {
int revnum = 0, n, temp, r;

cout << "Enter the value of n: ";


cin >> n;

temp = n;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

#include <iostream>
using namespace std;

int main() {
int num;
bool isPrime = true;

cout << "Enter a number: ";


cin >> num;

if (num <= 1) {
isPrime = false;
} else {

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}
}

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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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() {

cout << "Uppercase letters: ";


for (char ch = 'A'; ch <= 'Z'; ++ch) {
cout << ch << " ";
}
cout << endl;

cout << "Lowercase letters: ";


for (char ch = 'a'; ch <= 'z'; ++ch) {
cout << ch << " ";
}
cout << endl;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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;

cout << "Enter the value of n1: ";


cin >> n1;
cout << "Enter the value of n2: ";
cin >> n2;

tn1 = n1;
tn2 = n2;

while (n2 != 0) {
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

r = n1 % n2;
n1 = n2;
n2 = r;
}
gcd = n1;

lcm = (tn1 * tn2) / gcd;

cout << "GCD is: " << gcd << endl;


cout << "LCM is: " << lcm << 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

B. Tech/MBA Tech Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Observation and Learning: -


- Write your observation and learning

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy