0% found this document useful (0 votes)
19 views31 pages

Which of The Followings Are Invalid Identifiers:: Assignment 1 - C++

Uploaded by

palal98655
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)
19 views31 pages

Which of The Followings Are Invalid Identifiers:: Assignment 1 - C++

Uploaded by

palal98655
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/ 31

Assignment 1 – C++

INDIAN INSTITUTE OF TECHNOLOGY, ROORKEE


Name: Shubham Chandra
Course: M.Sc Mathematics (1st Year)
En. No: 21616028

1. Which of the followings are invalid identifiers:


i) 12amd
ii) Intel1
iii) Pentium 3
iv) Pentium_4
v) Email
vi) %age
vii) No return
viii) I am Correct

Answer:
The invalid identifiers are 12amd, Pentium 3, %age, No return and I am correct because identifiers
cannot start from numbers, from special characters and there should be no space between a identifier.

2. Write C++ programs for:

(i) Input radius and height of a cylinder and compute its surface area and volume

#include <iostream>

#include<conio.h>

#include <stdio.h>

#define PI 3.14

Void main ()

clrscr();

float r,h,surfacearea,volume;
cout << "enter the radius of the cylinder: ";

cin >>r;

cout <<"enter the height of the cylinder: ";

cin >>h;

double surfacearea = 2*PI*r*h+2*PI*r*r;

double volume = PI*r*r*h;

cout << "surface area of the cylinder="<< surface_area;

cout << "\n volume of the cylinder="<< volume;

getch();

Output:

enter the radius of the cylinder: 2


enter the height of the cylinder: 2
surface area of the cylinder= 50.24
volume of the cylinder= 25.12
(ii) Compute the sum and average of 100 given numbers.(you may make array)

#include<iostream.h>

int main()

int n,i,sum=0;

float avg;

cout<<"Enter the 100 numbers of your choice:";

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

{cin>>n;

sum=sum+n;

avg=sum/100;

cout<<"Sum of 100 entered numbers is:"<<sum;

cout<<"\nAverage of 100 entered numbers is:"<<avg;

return 0;

Output

Enter the 100 numbers of your choice:1 1 1 1 1 1 1 1


1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1
1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1
11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1

Sum of 100 entered number is: 100

Average of 100 entered number is 1


(iii) The computer should ask you about what you would do if found a moneybag? Display
the question and your answer on the screen.

#include <iostream.h>

#include<conio.h>

using namespace std;

void main ()

int money, n;

cout <<"what would you do if you found a money bag: " ;

cout << "enter the amount in the money bag:$ ";

cin >> n;

if (n<5)

cout << "i will get back to the owner of the money bag:";

else {

cout << "i will keep it ";

getch();

Output

What would you do if you found a money bag:

Enter the amount in the money bag: $10000

I will keep it
(iv) Input a student’s marks out of 400 and display the percentage.

#include <iostream.h>

#include<conio.h>

void main ()

int maths, data structure, cpp,java;

float percentage ,total ;

cout << "enter the marks of four subjects:";

cin >> maths >> data structure >> cpp >> java;

total = maths + data structure + cpp + java;

percentage = (total/(400))*100;

cout <<" marks percentage ="<< percentage ;

getch();

Output

enter the marks of four subjects : 90 79 85 95

marks percentage =87.25


3. Write a program that reads the radius of a circle as a float data type.
The program then computes and prints the diameter, circumference,
and area of the circle. If the user enters a negative value of radius, an
error message must be printed.

#include <iostream.h>

#define PI 3.14

void main ()

float r, c,d,a;

cout << "enter the radius of the circle :";

cin >> r;

if (r<0)

cout <<" radius of circle can't be negative:";

d=2*r;

c=2*PI*r

a =PI*r*r;

cout<< "the diameter of the circle is " << d << endl;

cout << "the circumference of the circle " << c << endl;

cout << "the area of the circle is " << a << endl;

getch();

Output

enter the radius of the circle :2

the diameter of the circle is 4

the circumference of the circle 12.56

the area of the circle is 12.56


4. Write a C++ program to find the square of the numbers from 1 to 10
using

a) while loop

#include <iostream>

using namespace std;

main()

int i=1;

cout<<"\n Square of the first 10 integers are : "<<endl;

while(i<=10)

int square=i*i;

cout<<"\t square of "<<i<<" = "<<i<<"*"<<i<<

" = "<<square<<endl;

i++;

return 0;

}
Output

Square of first 10 integers are :

Square of 1 = 1*1 = 1

Square of 2 = 2*2 = 4

Square of 3 = 3*3 = 9

Square of 4 = 4*4 = 16

Square of 5 = 5*5 = 25

Square of 6 = 6*6 = 36

Square of 7 = 7*7 = 49

Square of 8 = 8*8 = 64

Square of 9 = 9*9 = 81

Square of 10 = 10*10= 100


b) Do..while loop

#include<iostream>

using namespace std;

int main()

int a=1;

cout<<" Square of the first 10 integers are :"<<endl;

do {

cout<<"The Square of "<<a<<" is "<<a<<"*"<<a<<" = "<<a*a<<endl;

++a;

while(a<=10);

return 0;

Output
Square of first 10 integers are :

Square of 1 = 1*1 = 1

Square of 2 = 2*2 = 4

Square of 3 = 3*3 = 9

Square of 4 = 4*4 = 16

Square of 5 = 5*5 = 25

Square of 6 = 6*6 = 36

Square of 7 = 7*7 = 49

Square of 8 = 8*8 = 64

Square of 9 = 9*9 = 81

Square of 10 = 10*10= 100


c) For loop

#include<iostream>

using namespace std;

int main(){

cout<<"Square of the first 10 integers are :"<<endl;

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

int square= i*i;

cout<<"Square of "<<i<<" ="<<i<<"*"<<i<<" = "<<square<<endl;

return 0;

Output
Square of first 10 integers are :

Square of 1 = 1*1 = 1

Square of 2 = 2*2 = 4

Square of 3 = 3*3 = 9

Square of 4 = 4*4 = 16

Square of 5 = 5*5 = 25

Square of 6 = 6*6 = 36

Square of 7 = 7*7 = 49

Square of 8 = 8*8 = 64

Square of 9 = 9*9 = 81

Square of 10 = 10*10= 100


5. Write a C++ program to find the sum of the first 20 terms of the
following series:

SUM=1-(1/1!)+(2/2!)-(3/3!)+(4/4!)………(n/n!)

#include <iostream>

using namespace std;

int main()

int n, count, i;

float fact=1, sum=1;

cout<<"Enter the number of terms: ";

cin>>n;

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

{ fact=fact*i;

if(i/2==0)

{sum=sum+(i/fact);

else

{ sum=sum-(i/fact);

cout<<"Sum of series is\n"<<sum;

return 0;

Output
Enter the number of term: 20

Sum of series is

0.281718
6. Write three separate C++ programs to compute the factorial of a
number ‘n’. The programs should read ‘n’ from the user (should include
all possibility of n such as n is negative, zero, positive) and must use the
following for their implementation (i) for-loop, (ii) while-loop, (iii) do-
while loop

i)for loop

#include <iostream>

using namespace std;

int main ()

int n ;

int f=1;

cout << "enter the number n:";

cin >> n;

// for loop execution

if (n<0)

cout << "can't find factorial for negative numbers:";

else

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

f=f*i;

cout << "the factorial of :"<< n << " is " << f << endl;

return 0;
Output

enter the number n:4

the factorial of 4 is 24
i) do while loop

ii)do while loop


#include <iostream>

using namespace std;

int main ()

int n,f=1,i=1;

cout << "enter the number :";

cin >> n;

if (n<0){

cout <<"can't find factorial for negative numbers:";

}else {

do { f=f*i;

while(++i<=n);

cout << "value of factorial of :"<< n << " is " << f << endl;

return 0;

Output

enter the number :6


value of factorial of 6 is 720
iii) while loop

#include <iostream>

using namespace std;

int main (){

int n,f=1,i=1;

cout<<"enter the number:";

cin >> n;

if (n<0){

cout << "can't find factorial for negative numbers:";

}else {

while (i<=n)

{f=f*i,i++;

cout <<"the factorial of " << n <<" is "<<f;}

return 0;

Output

Enter the number: 7


the factorial of 7 is 5040
7. Write a program in C++ to check whether a given 5 digit number is a
Palindrome or not using a do…while loop. Check your program for 37873
and 37837

#include <iostream>

using namespace std;

int main (){

int n ,num,digit,rev=0;

cout << "enter a positive number :";

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 palindrome";}

else {

cout << "the number is not a palindrome";}

return 0;

}
Output

Output 1

enter a positive number :37873

the reverse of the number is :37873

the number is palindrome

Output 2

enter a positive number :37837

the reverse of the number is :73873

the number is not a palindrome


8. Write a C++ program to change the letter case(upper case to lower
case and vice versa)

#include <iostream>

using namespace std;

int main()

{ char str[100];

cout << "Enter a word :" << endl;

cin.get(str, 100);

for (int i = 0; str[i] != '\0'; i++)

if (islower(str[i]))

str[i] = char(toupper(str[i]));

else

str[i] = char(tolower(str[i]));

cout << "Word after changing its case will be " << str << endl;

Output

Enter a word :

MakE

Word after changing its case will be


mAKe
9. Write a C++ program to print 50 multiplication tables upto 10 using
double for loop.

#include <iostream>

#include <iomanip>

using namespace std;

int main ()

for (int x=1;x<51;x++)

for (int y =1;y<11;y++)

cout << setw(4)<< x*y;

cout<< endl;

return 0;

Output
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20
24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49
56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90
100 11 22 33 44 55 66 77 88 99 110 12 24 36 48 60 72 84 96 108 120 13 26 39 52 65 78 91 104 117
130 14 28 42 56 70 84 98 112 126 140 15 30 45 60 75 90 105 120 135 150 16 32 48 64 80 96 112 128
144 160 17 34 51 68 85 102 119 136 153 170 18 36 54 72 90 108 126 144 162 180 19 38 57 76 95 114
133 152 171 190 20 40 60 80 100 120 140 160 180 200 21 42 63 84 105 126 147 168 189 210 22 44 66 88
110 132 154 176 198 220 23 46 69 92 115 138 161 184 207 230 24 48 72 96 120 144 168 192 216 240 25 50
75 100 125 150 175 200 225 250 26 52 78 104 130 156 182 208 234 260 27 54 81 108 135 162 189 216 243
270 28 56 84 112 140 168 196 224 252 280 29 58 87 116 145 174 203 232 261 290 30 60 90 120 150 180
210 240 270 300 31 62 93 124 155 186 217 248 279 310 32 64 96 128 160 192 224 256 288 320 33 66 99
132 165 198 231 264 297 330 34 68 102 136 170 204 238 272 306 340 35 70 105 140 175 210 245 280 315 350
36 72 108 144 180 216 252 288 324 360 37 74 111 148 185 222 259 296 333 370 38 76 114 152 190 228 266
304 342 380 39 78 117 156 195 234 273 312 351 390 40 80 120 160 200 240 280 320 360 400 41 82 123 164
205 246 287 328 369 410 42 84 126 168 210 252 294 336 378 420 43 86 129 172 215 258 301 344 387 430 44
88 132 176 220 264 308 352 396 440 45 90 135 180 225 270 315 360 405 450 46 92 138 184 230 276 322 368
414 460 47 94 141 188 235 282 329 376 423 470 48 96 144 192 240 288 336 384 432 480 49 98 147 196 245
294 343 392 441 490 50 100 150 200 250 300 350 400 450 500
10. Write a program in C++ to read a set of 10 positive integers and
display them all. If the number entered is nonnegative, the program
continues, but when the user inputs a negative number the program
stops further reading and displays - “A NEGATIVE NUMBER ENTERED”.

#include <iostream>

using namespace std;

int main ()

int a[10], b;

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

{ cout<<"\nEnter "<<i<<" Integer:";

cin>>a[i];

if(a[i]>0)

{ b=1;

else

b=0;

break;

}}

if(b==1)

{ cout<<”Entered integers are: ”

for(int c=1; c<=10; c++)

cout<<”\n”<<a[c];

else

{
cout<<"\nA NEGATIVE NUMBER ENTERED";}

return 0;

Output

Enter 1 Integer: 1

Enter 2 Integer: 2

Enter 3 Integer: 3

Enter 4 Integer: 4

Enter 5 Integer: 5

Enter 6 Integer: 6

Enter 7 Integer: 7

Enter 8 Integer: 8

Enter 9 Integer: 9

Enter 10 Integer: 10

Entered integers are:

10
11. Write a C++ program that reads three nonzero double values and
determines and prints whether they could represent sides of a triangle.

#include <iostream>

using namespace std;

int main ()

double a ,b,c;

cout << "enter sides of triangle:";

cin >> a >> b >> c ;

if (a+b<=c || a+c<=b || b+c<=a )

cout << "no they do not forms sides of triangle:";

else

cout <<"they forms sides of triangle";

return 0;

Output

enter sides of triangle:7 10 5


they forms sides of triangle
12. Write a C++ program to calculate the roots of a quadratic equation
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎. Include all cases, 𝒂 = 𝟎, 𝒂 ≠ 𝟎, equal roots, unequal
roots, real or complex.

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{

float a, b, c, x, y, determinant, realpart, igpart;


cout << "enter coefficients a , b , c :";
cin >> a >> b >> c;

determinant = b*b-4*a*c;

if (a==0)
{
cout <<" a can't be zero:";
}
else {
if (determinant>0){
x=(-b+sqrt(determinant))/(2*a);
y=(-b-sqrt(determinant))/(2*a);

cout << "roots are real and different "<< endl;


cout << "x" << x<< endl;
cout << "y"<< y << endl;}
else if (determinant==0){
cout << "roots are real and same " << endl;
x=(-b+sqrt(determinant))/(2*a);
cout << "x=y"<< x << endl;

else {
realpart =-b/(2*a);
imagpart=sqrt(-determinant)/(2*a);
cout << "roots are complex and different:"<< endl;
cout << "x="<< realpart <<"+"<< igpart << "i"<< endl;
cout << "y="<< realpart <<"-"<< igpart << "i"<< endl;
}
}
return 0;}

Output

enter coefficients a , b , c :1 2 3
roots are complex and different:
x=-1+1.41421i
y=-1-1.41421i

13. Write a C++ program to read a number n and digit d and check
weather d is present in n. If it is so, find out the position of d in number
n. For example in the case of n=45678 and d=5, the digit 5 is present in
the numberat the posiyion 4 from right.

#include <iostream>

#include <stdlib.h>

using namespace std;

int main()

int n, d;

int number, r, i;

int found = 0;

cout<<"Enter a number: ";

cin>>n;
cout<<"Enter a digit to find (0 to 9): ";

cin>>d;

while(d < 0 || d > 9)

cout<<"Wrong entry\n";

number = n;

i = 1;

while(number) {

r = number % 10;

if(r == d) {

found = 1;

cout<<"Digit " <<d<< " found at position " <<i<<" from the left side of the number";

i++;

number = number / 10;

if(!found) cout<<"Digit " <<d<<" is not present in "<<n;

return 1;

Output

Enter a number: 1234567

Enter a digit to find (0 to 9): 3

Digit found at position 5 from the left side of the number


14. Write a C++ program to do the following: Get the marks in physics
chemistry and math (obtained out 0f 100) of a student.

Display the division obtained by the student based on the following


policy-

Percentage >= 90% :outstanding

80%<=Percentage < 90 :first class with distinction

65%<=Percentage < 80% :first class

50%<=Percentage < 65% :second class

40%<=Percentage < 50% :passed

Percentage < 40% :failed.

#include<iostream>

using namespace std;

int main(){

float physics, chemistry, maths, total, percentage;

cout<<"Enter the marks in physics from out of 100: ";

cin>>physics;

cout<<"Enter the marks in chemistry from out of 100: ";

cin>>chemistry;

cout<<"Enter thw marks in maths from out of 100: ";

cin>>maths;

total = physics + chemistry + maths;

percentage = (total/300)*100;

cout<<endl;

cout<<"Total Marks is: "<<total<<endl;

cout<<"Percentage is: "<<percentage<<"%"<<endl;


if(percentage>=90)

cout<<"Division: Outstanding"<<endl;

else if(percentage>=80 && percentage<90)

cout<<"Division: First class with distinction"<<endl;

else if(percentage>=65 && percentage<80)

cout<<"Division: First class"<<endl;

else if(percentage>=50 && percentage<65)

cout<<"Division: Second class"<<endl;

else if(percentage>=40 && percentage<50)

cout<<"Division: Passed"<<endl;

else

cout<<"Division: Failed"<<endl;

return 0;

Output

Enter the marks in physics from out of 100: 50

Enter the marks in chemistry from out of 100: 75

Enter the marks in maths from out of 100: 60

Total Marks is: 185

Percentage is: 61.6667%

Division: Second class


15. Write the above program using
i) Switch

#include <iostream>

using namespace std;

int main(){

float physics, chemistry, maths, total_marks, percentage;

cout<<"Please Enter your obtained marks out of 100 in Physics, Chemistry and maths:"<<endl;

cout<<"Enter your marks in physics: ";

cin>>physics;

cout<<"Enter your marks in chemistry: ";

cin>>chemistry;

cout<<"Enter your marks in maths: ";

cin>>maths;

total_marks = physics + chemistry + maths;

percentage = (total_marks/300)*100;

cout<<endl;

cout<<"Your Total Marks is: "<<total_marks<<endl;

cout<<"your percentage is: "<<percentage<<"%"<<endl;

int i=(int)percentage; // Here I changed percentage into an integer.

switch(i) {

case 100:

case 90:

cout<<"Division: Outstanding"<<endl;

break;
case 89:

case 80:

cout<<"Division: First class with distinction"<<endl;

break;

case 79:

case 65:

cout<<"Division: First class"<<endl;

break;

case 64:

case 50:

cout<<"Division: Second class"<<endl;

break;

case 49:

case 40:

cout<<"Division: Passed"<<endl;

break;

default:

cout<<"Division: Failed"<<endl;

return 0;

}
Output
Please Enter your obtained marks out of 100 in Physics,
Chemistry and maths:

Enter your marks in physics: 12

Enter your marks in chemistry: 23

Enter your marks in maths: 40

Your Total Marks is: 75

your percentage is: 25%

Division: Failed

ii) Conditional Operator

#include<iostream>

#include<string>

using namespace std;

int main(){

float physics, chemistry, maths, total_marks, percentage;

string result;

cout<<"Please Enter your obtained marks out of 100 in Physics, Chemistry and maths:"<<endl;

cout<<"Enter your marks in physics: ";

cin>>physics;

cout<<"Enter your marks in chemistry: ";

cin>>chemistry;

cout<<"Enter your marks in maths: ";


cin>>maths;

total_marks = physics + chemistry + maths;

percentage = (total_marks/300)*100;

cout<<endl;

cout<<"Your Total Marks is: "<<total_marks<<endl;

cout<<"your percentage is: "<<percentage<<"%"<<endl;

result = (percentage>=90)? "Outstanding" :((percentage>=80 &&

percentage<90)? "first class with distinction" :(percentage>=65 &&

percentage<80)? "First class" :(percentage>=50 && percentage<65)?

"Second class" :(percentage>=40 && percentage<50)? "Passed" : "Fail");

cout<<"Division: "<<result<<endl;

return 0;

Output

Please Enter your obtained marks out of 100 in Physics,


Chemistry and maths:

Enter your marks in physics: 50

Enter your marks in chemistry: 60

Enter your marks in maths: 70

Your Total Marks is: 180

your percentage is: 60%

Division: Second class

You might also like