Physics Assignment Exercise 2
Physics Assignment Exercise 2
Physics Assignment Exercise 2
on
Computational Physics
Errors:
‘else’ is written without a previous ‘if’
‘if’ is not recognized because of ; at the end of if statement
b) #include<iostream.h>
main()
{
int a;
cout<<“Enter any number?”;
cin>>a;
switch(a>10);
}
{
case 1:
cout<<“One”; break;
case 2:
cout<<“Two”; break;
else
cout<<“value is greater than 2”;
}
}
Errors:
Else should be replaced with “default”
No if before else
semi colon after switch
c)
#include<iostream.h>
main()
{
int a;
cout<<“Enter any no.?”;
cin>>a;
if (a>100)
cout<<“Value is greater than 100”;
cout<<“Ok”;
else
cout<<“Value is less than 100”;
}
Errors:
“No Errors”
Q:2 Write the output of the following programs.
a)
#include<iostream.h>
main()
{
int a,b,c;
a=b=10;
c=13;
if(a = = b & & a > b)
cout<<“Condition is true”<<endl;
else
{
cout<<“a is equal to b”<<endl;
cout<<“a is not greater than b”<<endl;
}
Cout<<“Ok”;
}
Output:
a is equal to b
a is not greater than b
Ok
b)
#include<iostream.h>
main()
{
int ac;
ac=15;
if(ac%2==0)
cout<<“Number is even”<<endl;
else
cout<<“Number is odd”<<endl;
cout<<“Ok”;
}
Output:
Number is odd
Ok
c)
#include<iostream.h>
main()
{
int ac,b=6,c=7;
ac=15;
if(ac%2==0)
if(b>c)
cout<<“b is greater than c and ac is even”<<endl;
else
cout<<“b is less than c and ac is even”<<endl;
else
cout<<“Number is odd”<<endl;
}
Output:
Number is odd
Q:5
Define the following
“if” statement:
“The if statement is used to
execute or ignore a set of
statements after
testing a condition”
“switch statement”:
“The switch statement is used as
a substitute of “nested if-else”
statements. It is used when
multiple choices are given and one
choice is to be selected.
“Nested if statement”:
“When an “if statement” is
used within another “if
statement”, it is called the
“nested if statement”. It is
used for multi-way decision
making.
Q: 4
Write a program in C++ to input a single letter in a
char variable. If “m” is input print “You are male”
otherwise print “You are female” by using conditional
expression operator.
Program:
#include<iostream.h>
main()
{
char OP;
cout<<Enter character;
cin>>OP;
switch(OP)
{
case “m”:
cout<<“You are male”;
default:
cout<<“You are female”;
}
getch
}
Q: 5
Write a program in C++ to input four integers. Find out
the largest value and then print it on the screen by
using nested if structure.
Program:
#include<iostream>
#include<stdio.h>
using namespace std;
int main( )
{
int a,b,c,d;
cout<<"enter 4 different variables";
cin>>a>>b>>c>>d;
if(a>b&&a>c&&a>d)
cout<<"the largest value is :"<<a;
else if (b>a&&b>c&&b>d)
cout<<"the largest value is :"<<b;
else if(c>a&&c>b&&c>d)
cout<<"the largest value is :"<<c;
else
cout<<"the largest value is :"<<d;
}
Q: 6 Write a program in C++ to input marks obtained by
a student in a subject. The total marks are 100. Find out
the grade of a student by using the if-else nested
structure. The grade is
If marks are equal to or greater than 90, grade is “A+”.
If marks are equal to or greater than 70 and less than
90, grade is “A”.
If marks are equal to or greater than 50 and less than
70, grade is “B”.
If marks are less than 50, grade is “F”.
Program:
#include<iostream.h>
main()
{
Int marks;
cout<<“Enter marks”;
cin>>“Marks”;
if(Marks>=90);
cout<<“A+”;
else if(marks>=70&&marks<90);
cout<<”A”;
else if(marks>=50&&marks<70);
cout<<“B”;
else if(marks<50);
cout<<“F”
else
cout<<“You are fail”;
}
#include<iostream.h>
int main()
{
char input;
cout<<“Input a single character”;
cin>>input;
if(input==‘a’||input==‘e’||input==‘i’||input==‘o’||
input==‘u’)
cout<<“It is a vowel”;
else
cout<<“It is a consonant”;
}
Q: 8 Write program 7 in C++ by using switch statement
only.
Program:
#include<iostream.h>
main()
{
char OP;
cin<<OP;
switch(OP)
{
case‘a’||‘A’;
cout<<“It’s a vowel”;
case‘e’||‘E’;
cout<<“It’s a vowel”;
case‘i’||‘I’;
cout<<“It’s a vowel”;
case‘o’||‘O’;
cout<<“It’s a vowel”;
case‘u’||‘U’;
cout<<“It’s a vowel”;
default;
cout<<“it’s a consonant”;
}
}