Complete All The Tasks As Per The Instructions Given. All The Programming Solutions Must Contain The Output Screen Shots of The Programs
Complete All The Tasks As Per The Instructions Given. All The Programming Solutions Must Contain The Output Screen Shots of The Programs
Complete all the tasks as per the instructions given. All the programming solutions must
contain the output screen shots of the programs.
Program-1
// A first program in C language.
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Welcome to C Programming”);
getch();
}
Sample Solution: Program-1
#include<iostream>
using namespace std;
int main()
{
cout << "Welcome to C++ Programing";
return 0 ;
}
Output:
NOTE: conio.h is not required while working in Dev C++. Also calling the clrscr() function
for clearing the screen and getch() function for holding the screen are not required.
Program-2
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the first number : “ );
scanf(“%d”, &a);
printf(“Enter the second number: “);
scanf(“%d”, &b);
c=a+b;
printf(“The sum of numbers is %d” ,c);
}
Solution: Program-2:
#include<iostream>
using namespace std;
int main( )
{
int a,b,c;
cout<< "Enter the first number : " ;
cin >> a;
cout << "Enter the second number: ";
cin >> b;
c=a+b;
cout<< "The sum of numbers is " <<c;
return 0;
}
OUTPUT:
Program-3
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
printf("Enter a number");
scanf("%d",&num);
i=num%2;
if(i==0)
{
printf(“Even number!”);
}
else
{
printf(“Odd number!”);
}
getch();
}
Solution: Program-3:
#include<iostream>
using namespace std;
int main()
{
int num,i;
cout<< "Enter a number: ";
cin>> num;
i=num%2;
if(i==0)
{
cout<<"Even number!";
}
else
{
cout<<"Odd number!";
}
return 0;
}
OUTPUT:
}
Program-4
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf("Enter the days of a week:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid day number!");
break;
}
getch();
}
Solution: Program-4
#include<iostream>
using namespace std;
int main()
{
int day;
cout<<"Enter the days of a week:";
cin>>day;
switch(day)
{
case 1:
cout<<"Sunday";
break;
case 2:
cout<<"Monday";
break;
case 3:
cout<<"Tuesday";
break;
case 4:
cout<<"Wednesday";
break;
case 5:
cout<<"Thursday";
break;
case 6:
cout<<"Friday";
break;
case 7:
cout<<"Saturday";
break;
default:
cout<<"Invalid day number!";
break;
return 0;
}
}
OUTPUT:
Program-5
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
getch();
}
Solution: Program-5
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=10;i++)
{
cout<<"\n"<<i;
}
return 0;
}
OUTPUT:
Example-6
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
printf("*");
}
printf("\n");
}
getch();
}
Solution: Program-6
#include<iostream>
using namespace std;
int main()
{
int a,b;
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
cout<<"*";
}
cout<<"\n";
}
}
OUTPUT:
Write C++ programs for the following questions given. Your answer should also include
output of programs.
1. Write a program to swap value of two variables without using third variable.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int a=89, b=34;
cout<<"Before swaping a= "<<a<<" b= "<<b<< "\n";
a=a*b;
b=a/b;
a=a/b;
cout<<"After swaping a= "<<a<<" b= "<<b;
return 0;
}
OUTPUT:
2. Write a program which input three numbers and display the largest number using ternary
operator.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int n1, n2,n3,max;
cout<< "Enter first number: ";
cin>>n1;
cout<< "Enter second number: ";
cin>>n2;
cout<< "Enter third number: ";
cin>>n3;
max = (n1 > n2) ?
(n1 > n3 ? n1 : n3) :
(n2 > n3 ? n2 : n3);
cout << "Largest number among "
<< n1 << ", " << n2 << " and "
<< n3 << " is " << max << "." ;
return 0;
}
OUTPUT:
3. Write a program which accepts days as integer and display total number of
years, months and days in it.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int days,months,years,n_day;
cout<<"Enter the number of days: ";
cin>>n_day;
years=n_day/365;
n_day=n_day%365;
months=n_day/30;
days=n_day%30;
cout<<"Years:"<<years<<"\nMonths:" <<months<<"\nDays:"<<days;
return 0;
}
OUTPUT:
ANSWER:
2. Class
Class is defined as a blueprint for an object. This doesn't actually define any data, but it
does define what the class name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.
3. Abstraction
Data abstraction refers to, providing only essential information to the outside world and
hiding their background details, i.e., to represent the needed information in program without
presenting the details.
4. Encapsulation
Encapsulation is a process of combining data and function into a single unit like capsule.
This is to avoid the access of private data members from outside the class. To achieve
encapsulation, we make all data members of class private and create public functions, using them
we can get the values from these data members or set the value to these data members.
5. Inheritance
One of the most useful aspects of object-oriented programming is code reusability. As the name
suggests Inheritance is the process of forming a new class from an existing class that is from the
existing class called as base class, new class is formed called as derived class.
This is a very important concept of object-oriented programming since this feature helps to
reduce the code size.
6. Polymorphism
The ability to use an operator or function in different ways in other words giving different
meaning or functions to the operators or functions is called polymorphism. Poly refers to many.
That is a single function or an operator functioning in many ways different upon the usage is
called polymorphism.
7. Overloading
The concept of overloading is also a branch of polymorphism. When the exiting operator or
function is made to operate on new data type, it is said to be overloaded.
ANSWER:
The difference between C language and C++ language are given below:
C language:
C++ language: