0% found this document useful (0 votes)
38 views

C++ Assignment

The document contains code snippets for various programming problems involving loops, conditional statements, functions, and other basic programming concepts: 1. Code to check if a number is even or odd using modulo operator and if/else conditional. 2. Code to find the largest of three numbers using ternary operator. 3. Code to accept days as input and calculate years, months, days. 4. Code for a guessing game to find a random number between 1-100. 5. Code to check if a number is a palindrome. 6. Code to print all prime numbers between 1-100 using a function. 7. Code to count digits in a number using loop

Uploaded by

heaven leta
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)
38 views

C++ Assignment

The document contains code snippets for various programming problems involving loops, conditional statements, functions, and other basic programming concepts: 1. Code to check if a number is even or odd using modulo operator and if/else conditional. 2. Code to find the largest of three numbers using ternary operator. 3. Code to accept days as input and calculate years, months, days. 4. Code for a guessing game to find a random number between 1-100. 5. Code to check if a number is a palindrome. 6. Code to print all prime numbers between 1-100 using a function. 7. Code to count digits in a number using loop

Uploaded by

heaven leta
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/ 18

1. Receive 3 numbers and display them in ascending order from smallest to largest.

2, Find the average of two numbers given by the user.

Start

Input i, j

Avg=i+j/2

Display Avg

Stop
3, Swap the contents of two variables using a third variable.

Start

Input x,y

z=x

x=z

y=z

Print x, y

Stop
4, Swap the content of two variables without using a third variable.

Start

Input x, y

x=x+y

y=x-y

x=x-y

Print x, y

Stop
5, Read 10 integers from the keyboardin the range 0 – 100, and cout how many of them are larger than
50, and display the result.

Start

n=0

N=0

Output n N = 100?

no

yes
Stop ARRAY[N] > 50?

no

n=n+1

N=N+1
6, Take an integer from the user and display the factorial of that number.

Start

Read m

i=1
fact = 1

false
I <= m

fact = fact * i

i=i+1

Write fact

Stop
1. //Write a program to check whether the given number is even or odd.

#include <iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

int n;

cout << "Enter an integer: "<<endl;

cin >> n;

if ( n % 2 == 0)

cout << n << " is even."<<endl;

else

cout << n << " is odd."<<endl;

return 0;

2. /*Write a program which input three numbers and

display the largest number using ternary operator. */

#include<iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

double n1,n2,n3;

cout << "Enter three numbers: "<<endl;


cin >> n1 >> n2 >> n3;

if(n1 > n2)

if(n1 > n3)

cout << "Largest = " << n1 << endl;

else

cout << "Largest = " << n3 << endl;

else

if(n2 > n3)

cout << "Largest = " << n2 << endl;

else

cout << "Largest = " << n3 << endl;

return 0;

3./* Write a program which accepts days as integer and display total number of

years, months and days in it.

Output

Enter no. of days: 796

Years: 2

Months: 2

Days: 6 */

#include<iostream>

#include <stdlib.h>

using namespace std;

int main()
{

system("color 0a");

int days,y,m,d;

cout<<"Enter number of days : "<<endl;

cin>>days;

y=days/365;

days=days%365;

m=days/30;

d=days%30;

cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d;

return 0;

4. /*Write a program that calculates a random number 1 through 100. The program then asks

the user to guess the number.

If the user guesses too high or too low then the program should output "too high" or "too

low" accordingly.

The program must let the user continue to guess until the user correctly guesses the

number*/

#include <iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

int number = 57;

int guess;

cout << "Guess the number from 1 and 100.\n";


while(1)

cin >> guess;

if (guess < 0)

{cout << "Too low.\n";}

else if

(guess > 100)

{cout << "Too high.\n";}

else if (guess = number)

{cout << "Your guess is correct.\n";}

else

cout << "Your guess is incorrect.Please try again.\n";

return 0;

5./* A palindrome is a word, number, phrase or other sequence of character which

reads the sane backward as forward,such as madam,racecar,9119,81018,101.......

write a program to check whether a number is palindrome or not.*/

#include<iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

int n, num, digit, rev = 0;

cout<<"enter a positive number : "<<endl;

cin>>num;

n=num;
do

digit = num % 10;

rev = (rev * 10) + digit;

num = num /10;

while (num !=0);

cout<<"the reverse of the number is:" <<rev <<endl;

if(n == rev)

cout<<"the number is a palindrome."<<endl;

else

cout<<"the number is not a palindrome."<<endl;

return 0;

6. /*A prime number is an integer greater than one and divisible only by itself

and one.

The first seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the

prime numbers between 1 and 100.*/

#include <iostream>

#include <stdlib.h>

using namespace std;

int isPrimeNumber(int);

int main()

{
system("color 0a");

bool isPrime;

for(int n = 2; n < 100; n++)

isPrime = isPrimeNumber(n);

if(isPrime == true)

cout<<n<<" ";

return 0;

// Function that checks whether n is prime or not

int isPrimeNumber(int n)

bool isPrime = true;

for(int i = 2; i <= n/2; i++)

if (n%i == 0) {

isPrime = false;

break;

return isPrime;

7./* Write a C++ program that counts the number of digits in an integer number.

For example; 23498 has five digits.*/

#include<iostream>

#include <stdlib.h>
using namespace std;

int main()

system("color 0a");

long int num;

long int count=0;

cout<< "please enter the number you want to count"<<endl;

cin>>num;

while(num!=0)

num = num/10;

count++;

cout<< count;

8. /*Write a program to print the following using loop.

(This question is invalid if loop is not used)*/

a)

#include <iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

int n=6;

int i, j;

for(i = 1; i <= n; i++)

{
//print * equal to row number

for(j = 1; j <= i; j++)

cout << "* ";

cout << "\n";

return 0;

b)

#include<iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

int rows=7;

int i, j, space;

for(i = 1; i <= rows; i++)


{

//for loop to put space in pyramid

for (space = i; space < rows; space++)

cout << " ";

//for loop to print star

for(j = 1; j <= (2 * rows - 1); j++)

if(i == rows || j == 1 || j == 2*i - 1)

cout << "*";

else

cout << " ";

cout << "\n";

return 0;

c)

#include<iostream>

#include <stdlib.h>

using namespace std;


int main()

system("color 0a");

int rows=6;

int i, j, space;

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

//for loop to put space

for(space = i; space < rows; space++)

cout << " ";

//for loop for displaying star

for(j = 1; j <= (2 * i - 1); j++)

cout << "* ";

cout << "\n";

return 0;

d)
e)

#include<iostream>

#include <stdlib.h>

using namespace std;

int main()

system("color 0a");

for (int j=1; j<=3; j++)

for(int i=1; i<=20; i++)

if(i>=4-j && i <=6+j || i>=15-j && i<=17+j)

cout<< "*";

else

cout<< " ";

cout<<endl;

int rows=10;

for (int a = rows; a >= 1; a--)

for (int b = 0; b < rows - a; b++) {

cout << " ";

}
for (int b = (2 - a); b < (2 - a) + (2 * a - 1); b++) {

cout << "*";

cout << "\n";

f)

You might also like