0% found this document useful (0 votes)
10 views7 pages

Programming For Problem Solving: Experiment 2 - 1

The document outlines various programming tasks in C++ and C, including creating sample programs, validating variable names, and implementing functions to display employee details and evaluate mathematical expressions. It also includes error identification in code and examples of swapping integers. Each task is accompanied by sample code and expected output.
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)
10 views7 pages

Programming For Problem Solving: Experiment 2 - 1

The document outlines various programming tasks in C++ and C, including creating sample programs, validating variable names, and implementing functions to display employee details and evaluate mathematical expressions. It also includes error identification in code and examples of swapping integers. Each task is accompanied by sample code and expected output.
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/ 7

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT 2 – 1
TASK 1: Create and run a sample program
// My first C++ program
#include<iostream.h>
#include<conio.h>
int main
{
cout<<”Welcome to C++ programming”;
getch()
}
Output: Welcome to C++ programming

Task 2: Following names are chosen by programmer for using them as variable names.
Identify whether these are valid or invalid. If invalid justify reason.
i. 100K
ii. floatTotal
iii. in1+n2
iv. case
v. wow!
vi. While
vii. Intwidth?
ANS:
We cannot use any variable given below:
i. Any variable starting with a number cannot be taken
ii. Float is main function so we cannot use float Total
iii. We have a function + in n1+n2 so we cannot use
iv. Case is main function so we can’t use it as variable
v. wow! Cannot be used because it has uppercase letter
vi. While is main function so we can’t use it as variable
vii. We can use int width, but we cannot use intwidth? Which have ? Expression
Task 3: Write a program to print employee details like employee number , name, address and
phone number.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<"\nSelect any employee to get their details:";
cout<<"\n1.SNEHIL:";
cout<<"\n2.SHASHANK:";
cout<<"\n3.SAI:";
cin>>a;
switch(a)
{
case 1:
{
cout<<"\n The details of employee is:";
cout<<"\n Employee number is: 9502605943";
cout<<"\n Full name: SNEHIL";
cout<<"\n Address: Jadcherla";
break;
}
case 2:
{
cout<<"\nThe details of employee is";
cout<<"\nEmployee number is:9000018235";
cout<<"\nFull name: SHASHANK";
cout<<"\nAddress: Hastinapuram,Hyderabad";
break;
}
case 3:
{
cout<<"\nThe details of employee is:";
cout<<"\nEmployee number is:9000018555";
cout<<"\nFull name: SAI";
cout<<"\nAdress:Rajapur";
break;
}
default:cout<<"wrong choice....!!!!";
}
getch();
}
Output:
Select any employee to get their details:
1.SNEHIL:
2.SHASHANK:
3.SAI:
1.The details of employee is
Employee number is:9502605943
Full name: SNEHIL
Address: Jadcherla.
Task 4: Write a program to evaluate the following expression and display their result.
Input
Integers x, y, z from user
a. x² + 2x³* (2*x)
b. x + y²+z³
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,x,y,z;
cout<<”Give the values for x, y, z: “;
cin>>x>>y>>z;
a=( (x*x) + ( 2*(x*x*x) ) * ( (2*x) ) );
b=( x + ( y*y ) + ( z*z*z ) );
cout<<”a. “<<x<<”^2 + 2( “<<x<<”^3 ) * (2 * “<<x<<” ) = “<<a;
cout<<”\nb. “<<x<<”^1 + “<<y<<”^2 + “<<z<<”^3 = “<<b;
getch();
}
Output:
Give the values for x,y,z : 3,2,1
a. 3^2 + 2(3^3)*(2*3) = 69
b. 3^1 + 2^2 + 1^3 = 8
Task 5:
Convert the following steps into a C program
1. Start
2. Define variables: rollno, msub1, msub2, msub3, msub4, msub5, msum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 5 subjects and also calculate the percentage score as:
msum = msub1 + msub2 + msub3 + msub4 + msub5; Score = msum/500 × 100
5. Display roll number and percentage score.
6. Stop
Program:
#include<stdio.h>
void main()
{
int msub1, msub2, msub3, msub4, msub5, msum, score, rollnumber;
float A;
printf("\n Enter roll number:");
scanf("%d",&rollnumber);
printf("Enter marks of msub1=");
scanf("%d",&msub1);
printf("Enter marks of msub2=");
scanf("%d",&msub2);
printf("Enter marks of msub3=");
scanf("%d",&msub3);
printf("Enter marks of msub4=");
scanf("%d",&msub4);
printf("Enter marks of msub5=");
scanf("%d",&msub5);
msum=msub1+msub2+msub3+msub4+msub5;
printf("\n Sum of marks=%d",msum);
A=(float)msum/5;
printf("\n Percentage of marks=%f",A);
}
Output:
Enter roll number:70572200011
Enter marks of msub1=91
Enter marks of msub2=89
Enter marks of msub3=93
Enter marks of msub4=93
Enter marks of msub5=97
Sum of Marks= 463 Percentage of Marks= 92.6

Task 6:
Find errors in the program given below: (Do not type the code. Find error without executing)
#include<iostream>
void main()
{
float speed, time, distance;
cout<<"Enter speed and time";
cin>> speed>>time
distance = speed * time
cout<<"\n The distance traversed is:”<<distance);
}
Solution:
#include<iostream>
void main()
{
int float speed, time, distance;
cout<<”Enter speed and time”;
cin>>speed>>time;
distance = speed*time;
cout<<”\n The distance traversed is:”<<distance;
return 0;
}
Task 7:
Write a program to swap two integers with and without using temporary variables.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<”Enter a & b values “;
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<”The values after swaping is::”<<a<<” “<<b;
getch();
}
Output:
Enter a & b values:: 9 7
The values after swaping is::7 9

You might also like