Edited Merged
Edited Merged
Branch- IT-A
Roll no- 22I2038
Subject- C++
ASSIGNMENT-1
Q- Write a C++ program that inputs a student marks in three subjects (out of 100) and prints the
1 percentage marks.
PROGRAM
#include <iostream>
using namespace
std;int main ()
{
int a,b,c, t;
float s;
cout<< "enter marks scored by you in english\n";
cin>> a;
cout<< "enter marks scored by you in c++\n";
cin>> b;
cout<< "enter marks scored by you in maths\n";
cin>> c;
t= a+b+c;
cout<< "total no. of marks scored by student:-"<<t;
s = (float) t / 3;
cout << "\npercentage marks scored by student:-"<<s;
return 0;
}
Q. Write a C++ program that accepts a character between a to j and prints next 4 characters.
2 PROGRAM
#include <iostream>
using namespace std;
int main()
{
int i;
char x;
cout<<"enter any alphabet: ";
cin>>x;
int n=(int)x;
cout<<"the next four alphabets are "<<endl;
for(i=n+1;i<=n+4;i++)
{
cout<<(char)i<<endl;
}
return 0 ;
}
Q. Write a C++ program to convert a given number of days into years, weeks and days.
3 PROGRAM
#include
<iostream>using
namespace std;
int main()
{
int y, d ,w,x,m;
cout<<"enter number of
days:\n";cin>>d;
if(d%365 == 0 || d/365 >=1)
{
cout<<d/365<<"years\n";
}
x = d%365;
if(x%7 == 0 || x%7 >= 1){
cout<<x/7<<"weeks\
n";
}
m = x%7;
cout<<m<<"days
";
return 0;
}
Q.6 Write a program to find area of triangle. Take base and height from user.
#include <iostream>using
namespace std;
int main()
{
float b, h, a;
cout<<"enter base and height of the triangle;\n";cin>>b>>h;
a= (b*h)/2;
cout<<"Area of triangle is:\n"<<a;return 0;
}
Q.7 Write a program that asks for your height in centimeters and then converts your height to feet and inches.(1
foot = 12 inches , 1 inch = 2.54 cm)
#include <iostream>using
namespace std;int main()
{
float h;
cout<<"enter your height in cms\n";cin>>h;
cout<<"your height in inches is:\n"<<h*1/2.54;cout<<"\nyour
height in feet is:\n"<<h*1/30.48;return 0;
}
QUE-8 Write a program to convert given inches into its equivalent yards and feet.(1 yard
= 36 inches , 1 foot = 12 inches)
#include <iostream>using
namespace std;
int main()
{
float i;
cout<<"enter given inches\n";cin>>i;
cout<<"yards in given inches is:\n"<<i/36;
cout<<"foot in given inches is:\n"<<i/12;
return 0;
QUE-9 Write a C++ program that reads temperature in Celsius and displays it in fahrenheit.
#include <iostream>using
namespace std;
int main()
{
float c,f;
cout<<"enter temperature in degree celsius:\n";
cin>>c;
f=1.8*c+ 32;
cout<<"temperature is degree farhenheit is:\n"<<f;
return 0;
}
Q10 Assuming that there are 7.418 gallons in a cubic foot, write a program that asks theuser
to enter the number of gallons, and then display the equivalent in cubic feet.
#include <iostream>using
namespace std;int main()
{
float g,c;
cout<<"enter the number of gallons:\n";
cin>>g;
c =g/7.418;
cout<<"no. of cubic foot in given number of gallons is:\n"<<c;
return 0;
}
ASSIGNMENT-2
Q-1 Write a C++ program that inputs experience and age of a person. The salary of the person is 6000 if the
person is experienced and his age is more than 35, otherwise if the person is experienced and his age is more
than 28 but less than 35 than the salary should be 4800 otherwise for experienced person the salary should
be 3000 and for inexperienced person the salary should be 2000.The program should be made using ternary
operator.
Program:-
#include <iostream>
using namespace std;
int main()
{
int age, exp;
cout<<"Enter age and no. of years of experience";
cin>>age>>exp; //taking input(age and experience) from user
(age>=35 && exp>0)? (cout<<"the salary is 6000"):(age>28 && age<35 && exp>0)?(cout<<"the salary is
4800"): (exp>0)?(cout<<"the salary is 3000"):(cout<<" the salary is 2000"); //using ternary operator
return 0;
}
OUTPUT:-
Q.2 A computer programming contest requires teams of five members each. Write a program that asks for the numb
of players, and then gives the number of teams and number of players left over.
Program:-
#include <iostream>
using namespace std;
int main()
{
int n,t,l;
cout<<"Enter number of team members";
cin>>n; //taking number of players from user
t= n/5;
l=n%5;
cout<<"number of teams="<< t<<endl;
cout<<"number of players left="<<l<<endl;
return 0;
}
OUTPUT:-
Q.3 The value of e is known to be 2.71828. Using this value, write a program to determine the value of the expression
2 – ye2y + 4y. Obtain the value of y from the user.
PROGRAM :-
#include <iostream>
#include<cmath> //to access functions like pow(),sqrt()
using namespace std;
int main()
{
int y;
long double e =2.71828;
float x;
cout<<"enter the value of y";
cin>>y; //taking value of y from the user
x= 2*y* ( pow(e,2*y)) + (pow(4,y)); //calculation of the given expression
cout<<"answer = "<< x;
return 0;
}
Q.4 Write a program to compute simple interest and compound interest. Take values from user.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int p,r,t,si,n,ci;
cout<<"enter the principle amount";
cin>>p;
cout<<"enter the rate of interest";
cin>>r;
cout<<"enter time in years";
cin>>t;
cin>>n;
si= p*r*t/100;
ci= (p*(pow((1+r/n),n*t)))-p;
cout<<"simple interest= "<<si<<endl;
cout<<"compound interest= "<<ci;
return 0;
}
Q.5 Write a program that reads in a character <char> from the keyboard and then displays one of the following
messages: Hint. You will need to refer to a table of ASCII characters.
(i) If <char> is a lower case letter, the message
“The upper case character corresponding to <char> is …”
(ii) If <char> is an upper case letter , the message
“The lower case character corresponding to <char> is …”
(iii) If <char> is not a letter, the message <char> is not a letter”.
PROGRAM:-
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"enter a character"<<endl;
cin>>ch; //taking character value from user
int num= (int)ch; //assigning the ascii value of the entered character to integer num
if (num>64 && num<=90) //range of ascii value of lower case characters
{
num= num+ 32;
cout<<"character entered " <<ch<<endl;
cout<<"lower case= "<<(char)num;
}
else if(num>=97 && num<=122) //range of ascii value of upper case characters
{
num= num-32;
cout<< "character entered "<< ch<<endl;
cout<<"upper case = "<<(char)num;
}
else
{
cout<<"invalid input";
}
return 0;
Q.6 Write a program to input three integers and print the largest of three.
PROGRAM:-
#include <iostream>
using namespace std;
int main()
{
int n1,n2,n3;
cout<<"enter value of any 3 numbers to be compared";
cin>>n1>>n2>>n3;
(n1>n2 && n1>n3)?(cout<<n1<<" is the greatest number"):(n2>n3)?(cout<<n2<<" is the
greatest"):(cout<<n3<<" is the greatest number");
return 0;
}
Q.7 Write a C++ program to input a number. If the number n is odd and positive, print its square root otherwise print
n3.
PROGRAM:-
#include <iostream>
#include<cmath> //math library to access sqrt and power functions in the program
using namespace std;
int main()
{
int n;
cout<<"Enter a number";
cin>>n; //taking number from user
if( n%2==0) //condition for number is even
{
float p=sqrt(n);
cout<<"the sqaure root of n= "<<p<<endl;
}
else
{
int p= pow(n, 3);
cout<<"n cube= "<<p<<endl;
}
return 0;
}
Q.8 Write a C++ program to create the equivalent of a four –function calculator. The program requires the user to en
two numbers and an operator. It then carries out the specified arithmetical operation: addition, subtraction,
multiplication, or division of the two numbers. Finally, it displays the result.
PROGRAM:-
#include <iostream>
using namespace std;
int main()
{
int a,b,x;
char oper;
cout<<"enter two int numbers on which operation is to be performed"<<endl;
cin>>a>>b;
cout<<"choose operator: + for addition - for subtraction * for multiplication / for division and % for
finding remainder"<<endl;
cin>> oper;
switch(oper)
{
case'+':
cout<<"the addition of numbers is: "<<endl;
x= a+b;
cout<<x;
break;
case'-': cout<<"the difference of numbers is: "<<endl;
x= a-b;
cout<<x;
break;
case '*': cout<<"the product of numbers is: "<<endl;
x= a*b;
cout<<x;
break;
case '/': cout<<"the division of numbers is: "<<endl;
x= a/b;
cout<<x;
break;
case '%': cout<<"the remainder on diving first number by second number is: "<<endl;
x= a%b;
cout<<x;
break;
default:
cout<<"invalid operator";
break;
}
return 0;
}
Q.9 Program to input a character and to print whether a given character is an alphabet, digit or any other character.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
char ch;
int num;
cout<<"enter any character"<<endl;
cin>>ch;
num= ch;
PROGRAM
#include <iostream>
using namespace std;
int main()
{
long int sales, comm;
cout<<"enter sales"<<endl;
cin>>sales;
if(sales>=300001)
{
comm=15*sales/100;
cout<<" commission= "<< comm;
}
else if(sales>=22001 && sales<30000)
{
comm=10*sales/100;
cout<<" commission= "<< comm;
}
else if(sales>=12001 && sales<=22000)
{
comm=7*sales/100;
cout<<" commission= "<< comm;
}
else if(sales>=5001 && sales<=12000)
{
comm=3*sales/100;
cout<<" commission= "<< comm;
}
else
{
comm=0;
cout<<"commission is ="<<comm;
}
return 0;
}
ASSIGNMENT-3
Q.1 Program to print whether a given character is an uppercase or a lowercase character or a digit or any
other character. Use ASCII codes for it. The ASCII codes are as given below:
Characters ASCII Range
‘ 0’ – ‘ 9’ 48-57
‘ A’ -‘ Z’ 65-90
‘ a’ –‘ z’ 97-122
Other characters 0-255 excluding the above mentioned codes.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
char ch;
int num;
cout<<"enter any character"<<endl;
cin>>ch;
num= ch;
if( num>=65 && num<=90){
cout<<"the character " <<ch <<" is an alphabet in UPPER CASE"<<endl;
}
else if( num>=97 && num<=122){
cout<<"the character " <<ch <<" is an alphabet in LOWER CASE"<<endl;
}
else if(num>=48 && num<=57){
cout<<"the character "<<ch<<" is a digit"<<endl;
}
else{
cout<<"the character "<<ch <<" is neither an alphabet nor a digit"<<endl;
}
return 0;
}
Q.2 Program to calculate and print roots of a quadratic equation ax 2+bx+c=0(where a is not equal to 0).
PROGRAM
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b,c;
float x1,x2;
cout<<"enter the values of a,b,c";
cin>>a>>b>>c;
x1=(-b+(sqrt(b*b-4*a*c)))/2*a;
x2=(-b-(sqrt(b*b-4*a*c)))/2*a;
cout<<"roots of the quadratic "<<a<<"x^2 + "<<b<<"x + "<<c<<" are : "<<x1<<" and "<<x2;
return 0;
}
Q.3 Program to input number of week’ s day (1-7) and translate to its equivalent name of the day of the
week. (e.g., 1 to Sunday, 2 to Monday, . . . . , 7 to Saturday).
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"enter any number from 1 to 7"<<endl;
cin>>n;
switch(n)
{
case 1: cout<<"SUNDAY"<<endl;
break;
case 2: cout<<"MONDAY"<<endl;
break;
case 3: cout<<"TUESDAY"<<endl;
break;
case 4: cout<<"WEDNESDAY"<<endl;
break;
case 5: cout<<"THURSDAY"<<endl;
break;
case 6: cout<<"FRIDAY"<<endl;
break;
case 7: cout<<"SATURDAY"<<endl;
break;
}
return 0;
}
Q-4 Program to calculate area of a circle, a rectangle or a triangle depending upon user’ s choice.
#include <iostream>
using namespace std;
int main()
{
char choice;
cout<<"enter R to calculate area of rectangle"<<endl;
cout<<"enter C to calculate area of circle"<<endl;
cout<<"enter T to calculate area of triangle"<<endl;
cout<<"enter E to exit the program"<<endl;
do{
cout<<endl<<"enter choice"<<endl;
cin>>choice;
switch(choice)
{
case'R':
{int b,l;
cout<<"enter length and breadth"<<endl;
cin>>l>>b;
int area1= l*b;
cout<<"the area of rectangle= "<< area1;
break;
}
case'C':
{int r;
cout<<"enter radius of the circle"<<endl;
cin>>r;
float area2= 3.14*r*r;
cout<<"the area of circle= "<<area2;
break;
}
case 'T' :
{int p,h;
cout<<"enter base and height"<<endl;
cin>>p>>h;
float area3= 0.5* p*h;
cout<<"the area of triangle"<<area3;
break;}
case 'E':
{cout<<"quitting the program"<<endl;
break;}
default :
{
cout<<"invalid input"<<endl;
break;}
}
}while( choice!= 'E');
return 0;
}
Q.7 Program to calculate and print the sums of even and odd integers of the first n natural numbers using
while loop.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n, j,i;
cout<<"enter the value of n"<<endl;
cin>>n;
int sum1=0;
for(i=1; i<=n; i=i+2)
{
sum1= sum1+i;
}
int sum2 =0;
for(j=0; j<=n; j=j+2)
{
sum2= sum2+j;
}
cout<<"Sum of even numbers = "<<sum2<<endl;
cout<<"Sum of odd numbers = "<<sum1<<endl;
return 0;
}
Q.8 Program to display a menu regarding rectangle operations and perform according to user’s response
using do-while loop and switch case.
Hint..
Rectangle menu
Display the area if the user enters number 1, 2 for perimeter, 3 for diagonal, 4 for exit, and default case
for wrong choice and to again enter a valid choice.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int choice, l, b;
cout<<"enter dimensions of rectangle: length and breadth"<<endl;
cin>>l>>b;
cout<<"Enter 1 to calculate the perimeter of rectangle"<<endl;
cout<<"Enter 2 to calculate the area of rectangle"<<endl;
cout<<"Enter 3 to calculate the diagonal of rectangle"<<endl;
cout<<"Enter 4 to exit the program"<<endl;
do {
cout<<endl<<"enter choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{cout<<"perimeter of rectangle = ";
int p= 2*(l+b);
cout<<p;
break;
}
case 2 : {
cout<<"area of rectangle= ";
int a= l*b;
cout<<a;
break;
}
case 3:
{cout<<"diagonal of rectangle= ";
int d= sqrt(l*l+b*b);
cout<<d;
break;}
case 4:{ cout<<"quitting the program";
break;}
default :{cout<<" invalid input";
break;}
}
}while (choice != 4);
return 0;
}
Q.9 Write a program using for loop to display the following output
*
**
***
****
PROGRAM
#include <iostream>
int main()
{
int n,i,j;
cout<<"enter value of n"<<endl;
cin>>n;
for (i=0; i<n;i++){
for(j=0; j<=i; j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Q10
Program to check whether a number is prime or not.
Hint. A number is not prime if it is completely (leaves reminder 0 on dividing) divisible by 2 or any
numbers greater than 2 till half of the number itself. Any number cant be completely divisible by a
number which is greater than half of the number itself
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n, i, p=0;
cout<<"enter any number"<<endl;
cin>>n;
for (i=2; i<n; i++){
if(n%i==0)
{
cout<<n<<" is not a prime number"<<endl;
p=1;
break;
}}
if(p==0){
cout<<n<<" is a prime number"<<endl;
}
return 0;
}
ASSIGNMENT-4
Q.1 Evaluate the following C++ expressions with the help of a program and analyze their output where a, b, c
are integers and d, f are floating point numbers. The value of a = 5, b=3 and d=1.5
I. f = a+b/a
II. c = d*a+b
III. c= (a++)*d+a
IV. f=(++b)*b-a
V. c=a-(b++)*(--a)
PROGRAM
#include <iostream>
int main()
{
int a=5,b=3,c;
float d=1.5,f;
f = a+b/a;
cout<<" 1. f = a+b/a = "<<f<<endl;
c = d*a+b;
cout<<" 2. c = d*a+b = "<<c<<endl;
c= (a++)*d+a;
cout<< " 3. c= (a++)*d+a = "<<c<<endl;
f=(++b)*b-a;
cout<<" 4. f=(++b)*b-a = "<<f<<endl;
c=a-(b++)*(--a);
cout<<" 5. c=a-(b++)*(--a) = "<<c<<endl;
return 0;
}
Q.6 Write a C++ program to check whether the given number is palindrome or not.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n,i,rev,r,temp;
cout<<"enter any number"<<endl;
cin>>n;
temp=n;
rev=0;
while(n>0)
{
r=n%10;
rev= rev*10 + r;
n=n/10;
}
if(rev== temp)
{
cout<<temp<< " is a pallindrome number";
}
else{
cout<<temp<<" is not a pallindrome number";
}
return 0;
}
Q.7 Write a C++ program to generate divisors of integers.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout<<"enter any number"<<endl;
cin>>n;
cout<<"the divisors of "<<n<<" are :- "<<endl;
for(i=1; i<=n; i++)
{
if(n%i==0)
{
cout<<i<<", ";
}
}
return 0;
}
Q.8 Write a C++ program to find whether a given number is odd or even prime. The program should continue as
long as the user wants.
#include<iostream>
int main(){
int n,p,choice;
do{
cout<<"enter 0 to exit the program"<<endl;
cout<<"enter 1 to continue"<<endl;
cin>>choice;
switch (choice)
{
case 1:
cout<<"enter the number"<<endl;
cin>>n;
p=0;
if(n==2)
{
cout<<"the number is even prime"<<endl;
}
else if(n==1)
{
cout<<"the number is neither prime nor composite"<<endl;
}
else if(n<1){
cout<<"Invalid input"<<endl;
}
else{
for (int i = 2; i < n; i++)
{
if (n%i==0)
{
p=1;
break;
}
}
if(p==0)
{
cout<<"the number is odd prime number"<<endl;
}
else if (p==1)
{
cout<<"the number is not prime"<<endl;
}
}
break;
case 0:
{
cout<<"quitting the program"<<endl;
break;
}
default:
cout<<"invalid input"<<endl;
break;
}
}while(choice!=0);
return 0;
}
Q.9 Write a C++ program to print table of a given number.
#include <iostream>
using namespace std;
int main()
{
int n,i;
cout<<"enter n"<<endl;
cin>>n;
for(i=1; i<=10; i++)
{
cout<<n<<" * "<<i<<" = "<< n*i<<endl;
}
return 0;
}
Q.10 Write a C++ program to print sum of negative numbers, sum of positive even numbers, and sum of
positive odd numbers from a list of numbers entered by the user. The list terminates when the user enters
0.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n, j,i,choice;
do{
cout<<"enter 1 to continue and 0 to exit"<<endl;
cin>>choice;
switch(choice){
case 1 :{
cout<<"enter number of elements to be entered"<<endl;
cin>>n;
cout<<"enter elements"<<endl;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
int sum1=0;
int sum2=0;
int sum3=0;
for(j=0; j<n; j++){
if(arr[j]<=0)
{
sum1= sum1+ arr[j];{
else
{
if(arr[j]%2==0)
{
sum2=sum2+arr[j];
}
else{
sum3+=arr[j];}}}
cout<<"sum of negative integers= "<<sum1<<endl;
cout<<"sum of even positive integers= "<<sum2<<endl;
cout<<"sum of odd positive integers= "<<sum3<<endl;
break;
}
case 0 : {
cout<<"quitting the program";
break;
}
default : cout<<"invalid input"<<endl;
}
}while (choice!=0);
return 0;
}
ASSIGNMENT-5
Q.1 Write a C++ program to find out whether a year (entered in 4-digit number representing it) is a
leap year.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"enter the year"<<endl;
cin>>n;
if( n%4==0)
{
cout<<n<<" is a leap year"<<endl;
}
else{
cout<<n<<" is not a leap year"<<endl;
}
return 0;
}
Q.2 Given three numbers A, B and C, write a program to write their values in descending order. For
example, if A=7,B = 4, and C=10, your program should print out : 10, 7, 4.
#include<iostream>
using namespace std;
int main(){
int a,b,c;
cout<<”enter the numbers”<<endl;
cin>>a>>b>>c;
if (a>=b&&a>=c&&b>=c)
{
cout<<a<<","<<b<<","<<c;
}
else if (b>=a&&b>=c&&a>=c)
{
cout<<b<<","<<a<<","<<c;
}
else if (c>=b&&c>=a&&b>=a)
{
cout<<c<<","<<b<<","<<a;
}
else if (a>=c&&a>=b&&c>=b)
{
cout<<a<<","<<c<<","<<b;
}
else if (c>=a&&c>=b&&a>=b)
{
cout<<c<<","<<a<<","<<b;
}
else
{
cout<<b<<","<<c<<","<<a;
}
}
#include<iostream>
using namespace std;
int main(){
int n;
int a=0,b=1,temp=0;
cout<<"enter number"<<endl;
cin>>n;
int num=0;
for (int i = 0; i < n; i++)
{
if (i==0)
{
cout<<0<<” “;
}
else if (i==1)
{
cout<<1<<” “;
}
else
{
temp=a+b;
a=b;
b=temp;
cout<<temp<<” “;
}
}
return 0;
}
Q-4 Write a C++ program to print largest even and largest odd number from a list of numbers
entered through keyboard.
// The list terminates as soon as one enters 0(zero).
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter 0 at any stage to terminate the list"<<endl;
cout<<"enter the no. of times you want to enter number"<<endl;
cin>>n;
int x[n];
cout<<"enter the number"<<endl;
for (int i = 0; i < n; i++)
{
cin>>x[i];
if (x[i]==0)
{
break;
}
}
int max = x[0],max1=x[0];
for (int i = 0; i < n; i++)
{
if (x[i]%2==0 && x[i]>max)
{
max=x[i];
}
else if (x[i]%2!=0 && x[i]>max1)
{
max1=x[i];
}
}
cout<<"even largetst number = "<<max<<endl;
cout<<"odd largest number = "<<max1<<endl;
return 0;
}
(iii)form an integer Y that has the number of digits n at ten’s place and the most significant digit
of X at one’s place.
(iv)Output Y
(For example, if X is equal to 2134, then Y should be 42 as there are 4 digits and the most
significant number is 2)
PROGRAM
#include <iostream>
#include<cmath>
using namespace std;
int main()
{ int n, temp,y, p,i;
cout<<"Enter a number"<<endl;
cin>>n;
temp=n;
p=0;
while(n>0)
{
p++;
n=n/10;
}
cout<<"number of digit is = "<<p<<endl;
int signo;
signo=temp/(pow(10,p-1));
cout<<"the most significant number is = "<<signo<<endl;
y= p* 10+ signo;
cout<<"y is "<<y<<endl;
return 0;
}
Q.8 Write a program to accept the age of n employees and count the number of persons in the
following age group : -
#include <iostream>
using namespace std;
int main()
{
int n,i;
cout<<"enter number of employees"<<endl;
cin>>n;
int em[n];
cout<<"enter ages of the employees"<<endl;
for(i=0;i<n;i++)
{cin>>em[i];
}
int grp1=0, grp2=0, grp3=0;
for(i=0;i<n;i++)
{
if(em[i]>=26 && em[i]<=35)
{
grp1++;
}
else if(em[i]>=36 && em[i]<=45)
{
grp2++;
}
else if(em[i]>=46 && em[i]<=55)
{
grp3++;
}
}
cout<<"Number of employees between the age group (26-35) are : "<<grp1<<endl;
cout<<"Number of employees between the age group (36-45) are : "<<grp2<<endl;
cout<<"Number of employees between the age group (46-55) are : "<<grp3<<endl;
return 0;
}
Q.9 Write programs using nested loops to produce the following designs:-
a) b)
A & & & & & & &
A B & & & & &
A B C & & &
A B C D E &
A B C D E F
c) d)Write a program using nested
& loops to produce a rectangle of *
& & with 6 rows and 20 * per row.
& &
& &
& &
& & & & & & & & & & &
[A]-PROGRAM
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
cout<<"enter n";
cin>>n;
for(i=65;i<(65+n);i++)
{
for(j=65;j<=i;j++)
{
cout<<(char)j;
}
cout<<endl;
}
return 0;
}
[B]- PROGRAM
#include <iostream>
using namespace std;
int main()
{
int i,n,j;
cout<<"enter n"<<endl;
cin>>n;
for(i=n;i>0;i--)
{
for(j=0; j<=n-i ;j++){
cout<<" ";
}
for(j=1;j<=(2*i-1) ;j++) {
cout<<"&";
}
cout<<endl;
}
return 0;
}
[C]- PROGRAM
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter the number of columns you want to print"<<endl;
cin>>n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j==(n/2-i))
{
cout<<"&";
}
else if (j==(n/2+i))
{
cout<<"&";
}
else
{
cout<<" ";
}
}
cout<<endl;
if (i==(n-1)/2)
{
for (int j = 0; j < n; j++)
{
cout<<"&";
}}}
return 0;
}
[D]-PROGRAM
#include <iostream>
using namespace std;
int main()
{
int i,m,n,j;
cout<<"enter dimensions of rectangle to be printed"<<endl;
cin>>n>>m;
for(i=1; i<=m; i++)
{
for(j=1; j<=n;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Q.10 Write programs to find sum of the following series take the values of x and n from user.
(i) S= 1 + x + x2 + x3 + ………..xn .
(ii) S = x+x2 /2 + x3/3 + ………..xn/n.
PROGRAM
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int n, x,p,t;
cout<<"enter the value of x and n"<<endl;
cin>>x>>n;
int sum1=0;
for( int i=0; i<=n; i++)
{
cout<<x<<"^"<<i<<" + ";
p= pow(x,i);
sum1+=p;
}
cout<<" = "<< sum1;
cout<<endl;
int sum2=0;
for(int i=1; i<=n;i++)
{
cout<<x<<"^"<<i<<"/"<<i<<" + ";
t= (pow(x,i))/i;
sum2=sum2+t;
}
cout<<" = "<<sum2;
return 0;
}