Simple Programs
Simple Programs
Programs
1. Write a program to convert centigrade to Fahrenheit or vice versa based
on user choice.
2. Write a program to calculate the factorial of a number
3. Write a program to find the largest of two numbers using functions
4. Write a program to calculate square and cube of a number using functions
5. Write a program to declare a struct item. Initialize and display the contents
of member variables (codeno, prize, qty).
Viva Questions
1. What are the basic data types available in C/C++?
2. What is the basic header file to be included for writing a simple C++
program?
3. What is the input and output statements in C++?
4. What is the prototype of the function?
5. What values a function can return?
6. What is a structure?
7. How to access structure members?
8. How to give comment line in C++?
9. In what extension the C++ program files have to be stored?
10.What is different meaning of << operator?
Programs
1.
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int choice;
float ctemp,ftemp;
cout << "1.Celsius to Fahrenheit" << endl;
cout << "2.Fahrenheit to Celsius" << endl;
cout << "Choose between 1 & 2 : " << endl;
cin>>choice;
if (choice==1)
{
cout << "Enter the temperature in Celsius : " << endl;
cin>>ctemp;
ftemp=(1.8*ctemp)+32;
cout << "Temperature in Fahrenheit = " << ftemp << endl;
}
else
{
cout << "Enter the temperature in Fahrenheit : " << endl;
cin>>ftemp;
ctemp=(ftemp-32)/1.8;
cout << "Temperature in Celsius = " << ctemp << endl;
}
return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
int n,i,fact=1;
cin>>n;
for(i=1;i<=n;i++)
fact=fact*i;
cout<<"\nFactorial of"<<n<<"is"<<fact;
return 0;
}
3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,b;
int large(int,int);
cout<<"Enter two numbers a and b:";
cin>>a>>b;
cout<<"The Largest number is:" <<large(a,b)<< endl;
return 0;
}
int large(int x,int y)
{
if (x>y)
{
return x;
}
else
{
return y;
}
}
4.
#include <iostream>
using namespace std;
int n;
int main()
{
int square();
int cube();
cout<<"Enter the number:";
cin>>n;
cout<<"\nSquare of the number is:" << square();
cout<<"\nCube of the number is:" << cube();
return 0;
}
int square()
{
return n*n;
}
int cube()
{
return n*n*n;
}
5.
#include<iostream>
using namespace std;
struct item
{
int codeno;
float prize;
int qty;
};
int main()
{
item a,*b;
a.codeno=123;
a.prize=150.75;
a.qty=150;
cout<<\n With simple structure variable;
cout<\n Codeno: <<a.codeno;
cout<<\n Prize:<<a.prize;
cout<<\n Qty:<<a.qty;
b->codeno=124;
b->prize=200.75;
b->qty=75;
cout<<\n With pointer to structure;
cout<<\n Codeno:<<b->codeno;
cout<<\n Prize:<<b->prize;
cout<<\n Qty:<<b->qty;
return 0;
}