Problem Statement:-
.Write collection of production rules in OPS-5 syntax to input two numbers from user and calculate sum of all the integers between the given numbers. Also trace (i.e. show status of working memory) for any one of the sample outputs given below. (Marks 20)
Enter First Number:12
Enter Second Number:7
7 + 8 + 9 + 10 + 11 + 12 = 57
Note that output should be same if user enters 7 as first number and 12 as second number.
Enter First Number:7
Enter Second Number:12
7 + 8 + 9 + 10 + 11 + 12 = 57
Source Code:-
Python:-
x = int(input("Enter the 1st number :"))
y = int(input("Enter the 2nd number :"))
if(x > y):
temp = y
y = x
x = temp
sum = 0
for x in range(x, y):
print(x, end=" + ")
sum = sum + x
sum = sum + y
print(y, end=" = ")
print(sum)
#include<stdio.h>
int main(){
int x, y,i,temp,sum=0;
printf("Enter First Number\n");
scanf("%d",&x);
printf("Enter Second Number\n");
scanf("%d",&y);
if(x>y){
temp=x;
x=y;
y=temp;
}
printf("%d",x);
sum=x;
for(i=x+1;i<=y;i++){
printf(" + %d",i);
sum= sum + i;
}
printf(" = %d",sum);
}
Problem Statement 2:-
1.write a c++ program that accepts three numbers and prints "all numbers are equal" if all three numbers are equal, "All numbers are different" if all three numbers are different and
"Neither all are equal or different"
otherwise.
Solution:-
#include<iostream>
using namespace std;
int main(){
int x, y,z;
cout<<"Enter three Numbers\n";
cin>>x>>y>>z;
if(x==y && y==z)
cout<<"All Numbers are Equal\n";
else if(x != y && y !=z)
cout<<"All Numbers are Different\n";
else
cout<<"Neighter All are equal or Different \n";
}
Problem Statement 3:-
write a program thats accepts three numbers from the user and prints
"increasing" if the numbers are in increasing order, "decreasing" if the numbers are in decreasing order and
"Neither increasing and decreasing order"
otherwise
Solution:-
#include<iostream>
using namespace std;
int main(){
int x, y,z;
cout<<"Enter three Numbers\n";
cin>>x>>y>>z;
if(x<y && z > y){
cout<<"increasing\n";
}
else if(x>y && y > z){
cout<<"Decreasing\n";
}
else
cout<<"Neighter Increasing or Decreasing order\n";
}
No comments:
Do not add any link in the comments.
For backlink, contact us.