Assignment 2
Study Of Input Output Operations In c++
Name:- Pratham Singh FY- CS
Registration Number- 251070044
AIM:- Write a program for entering the detailed information of student and print all the
details
THEORY:- 1) Header files in c and predefined functions
2) Header files in c++ and predefined function
3) Program to display a string “welcome to cpps class” on screen in c and c+
4) Program in c++ to accept and display a value of a character variable.
5) Program to display a character value on screen in c
Header files are those files in c and c++ that contains predefined input and output functions
The definition of such functions can be included or imported in out programs with the help of
preprocessor directive #include
For e.g. #include<studio.h> in c program→ studio is a header that contains
standard input and output functions print() and scanf().
For e.g. #include<iostream> in c ++
program → iostream is a header file that contains standard input and output objects
cout and cin.
iostream
1. Istream class→ cin is an object from istream class. Cin is used in association
with >> (extraction operator extracts data from input devices)
2. Ostream class→ cout is an object from ostream class. Cout is used in
association with << (insertion operator inserts data on screen )
—------------------------------------
3. Program to display a string “welcome to cpps class” on screen in c and c+
—C Program—
#include<studio.h>
int main()
{
printf(“WELCOME TO CPPS CLASS”);
return (0);
}
Output:- WELCOME TO CPPS CLASS
—-----c++ Program—----
Header files in C++ and Predefined functions
#include<iostream>
Using namespace std;
Int main()
{
count<<”WELCOME TO CPPS CLASS”:
Return ); // 0 indicated true value, program exe sucessful
}
output :-WELCOME TO CPPS CLASS
Program in c++ to accept and display a value of a character variable.
#include <iostream>
using namespace std;
int main ( ) {
char a ;
cout<< “Please enter the value of character variable:”;
cin>>a;
cout<<”The value entered by user :”<<a ;
return 0;
}
Output : Please enter the value of character variable b
The value entered by user :b
C program by using character variable
#include <stdio.h>
int main( ) {
char a ;
printf(“Please enter the value of character variable : ”);
scanf(“%c”,&a);
printf(“The value of variable entered is %c",a);
return 0;
}
C program by using string variable
#include<stdio.h>
int main( ){
char a[10];
printf(“Please enter the value of string: “);
scanf("%s",&a);
printf("The value of variable is %s",a);
return 0;
}
Program to display string value in C++
#include <iostream>
using namespace std;
int main() {
char a[10];
cout<<"Welcome to VJTI ";
cout<<endl;
cout<<"Please enter the value of string:";
cin>>a;
cout<<"The value of string is "<<a;
return 0;
}
Program in C to accept and display the hardcoded string values
#include <stdio.h>
int main( ){
char a[10]= "hello";
printf("The value of a is %s",a);
return 0;
}
Program in C++ to accept and display the hardcoded string values
#include <iostream>
using namespace std;
int main(){
char a[10]="Hello";
cout<<"The value entered is "<<a;
return 0;
}
Use of \n, \t and endl in C
#include<stdio.h>
int main(){
printf("Welcome to CPPS class \n"); // here \n is used to move to new line
printf("CPPS is quite intresting.\n");
printf("\tCPPS lectures are on tuesday"); //here \t is used to create tab space
return 0;
}
Use of \n, \t and endl in C ++
#include <iostream>
using namespace std;
int main(){
cout<<"Welcome to CPPS class"<<endl; //here \n and endl is used to move to new line
cout<<"CPPS class is quite interesting \n";
cout<<"\t CPPS class is on tuesday";// here \t is used to create tab space
return 0;
}
Program in C to accept and display student details
#include <iostream>
int main(){
// variable declaration
int id, age;
char name[20], add[40];
printf("\n Please enter the value of Student id:");
scanf("%d",&id);
printf("The entered value of student id is %d",id);
printf("\n\n Please enter the value of Student name:");
scanf("%s",&name);
printf("The entered value of student name is %s",name);
printf("\n\n Please enter the value of Student age:");
scanf("%d",&age);
printf("The entered value of student age is %d",age);
printf("\n \nPlease enter the value of Student address:");
scanf("%s",&add);
printf("The entered value of student address is %s",add);
return 0;
}
Program in C++ to accept and display student details
#include <iostream>
using namespace std;
int main(){
int id, age;
string name, address;
cout<<"Please enter the value of Student id:";cin>>id;
cout<<"Please enter the value of Student name:";cin>>name;
cout<<"Please enter the value of Student age:";cin>>age;
cout<<"Please enter the value of Student address:";cin>>address;
cout<<endl;
cout<<"The entered value of student id is "<<id<<endl;
cout<<"The entered value of student name is "<<name<<endl;
cout<<"The entered value of student age is "<<age<<endl;
cout<<"The entered value of student address is "<<address<<endl;
return 0;
}
Study of different data types in C++
#include <iostream>
using namespace std;
int main(){
int a = 1; //declaration and initialization
char b = 'B'; //single character in single inverted comma
float c = 1.1f;
double d = 1.1d;
string e = "hello world";
char f[20] = "hello world";
bool g = true;
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f<<endl<<g;
return 0;
}
A program in C++ to accept 2 numbers as input, display their sum, average as
output
#include <iostream>
using namespace std;
int main(){
float a,b;
cout<<"Please enter the value of a and b: ";
cin>>a>>b;
cout<<"Sum="<<a+b<<endl;
cout<<"Average="<<(a+b)/2;
return 0;
}