0% found this document useful (0 votes)
70 views14 pages

Complete All The Tasks As Per The Instructions Given. All The Programming Solutions Must Contain The Output Screen Shots of The Programs

This document provides instructions for an assignment due on May 29th, 2020. It consists of 3 tasks worth a total of 100 marks. Task 1 (50 marks) involves converting 5 C programs to C++ and including screenshots of the outputs. Task 2 (30 marks) requires writing 3 additional C++ programs with outputs. Task 3 (20 marks) contains 2 short questions about object-oriented programming and the differences between C and C++.

Uploaded by

riwa
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)
70 views14 pages

Complete All The Tasks As Per The Instructions Given. All The Programming Solutions Must Contain The Output Screen Shots of The Programs

This document provides instructions for an assignment due on May 29th, 2020. It consists of 3 tasks worth a total of 100 marks. Task 1 (50 marks) involves converting 5 C programs to C++ and including screenshots of the outputs. Task 2 (30 marks) requires writing 3 additional C++ programs with outputs. Task 3 (20 marks) contains 2 short questions about object-oriented programming and the differences between C and C++.

Uploaded by

riwa
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/ 14

Week-1: Assignment-1

Due date: 29th May 2020 – [12:00AM] Total marks : 100

Complete all the tasks as per the instructions given. All the programming solutions must
contain the output screen shots of the programs.

Task-1 [50 marks]


=====
Convert all the following C program codes into C++ programming Codes.

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:

Task-2 [30 marks]


=====

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:

Task-3 [20 marks]


=====
Answer the following questions.

1. What is Object Oriented Programming?

ANSWER:

Object-oriented programming (OOP) is a programming paradigm based on the concept of


objects, which contain data and code, data in the form of fields (often known
as attributes or properties), and code in the form of procedures (often known as methods).
For example, a car is an object which has certain properties such as color, number of
doors, and number of seats. It also has certain methods such as accelerate, brake, and so
on.
The main purpose of C++ programming was to add object orientation to the C
programming language, which is in itself one of the most powerful programming
languages. There are a few principal concepts that form the foundation of object-oriented
programming:
1. Object
This is the basic unit of object-oriented programming. That is both data and function
that operate on data are bundled as a unit called as object.

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.

2. Write difference between C language and C++ language.

ANSWER:
The difference between C language and C++ language are given below:

C language:

1. C language is a Procedural Oriented language.


2. In C, the variable should be defined at the beginning of the program.
3. The file extension of a C program is .c.
4. In C language scanf and printf are used for the standard input and output.
5. C language is a subset of C++. It cannot run C++ code.
6. C language follows Top-Down programming approach.
7. C language uses stdio.h as a header file.
8. It supports built-in data types.
9. C does not support the inheritance.
10. C supports only Pointers.

C++ language:

1. C++ language is an Object-Oriented Programming language.


2. C++ allows you to declare variables anywhere in the function.
3. The file extension of a c+ + program language is.cpp.
4. In C++ cin» and cout« are given for standard input and output operations.
5. C++ is a superset of C. C++ can run most of C code while C cannot run C++ code.
6. C++ follow bottom-up programming approach.
7. C++ uses iostream as a header file.
8. It supports built-in & user-defined data types.
9. C++ supports inheritance.
10. C++ supports both pointers and references.

You might also like