0% found this document useful (0 votes)
16 views

Lab Manual: Introduction To Computer Programming Subject Code: - 2CORC5

The document is a lab manual for computer programming that contains 18 questions and their solutions. Each question contains a code snippet to solve a programming problem, such as determining if a number is even or odd, finding the greatest of three numbers, calculating factorials using recursion, and more. The code examples demonstrate basic programming concepts like loops, functions, recursion, and parameter passing.

Uploaded by

svawhites
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Manual: Introduction To Computer Programming Subject Code: - 2CORC5

The document is a lab manual for computer programming that contains 18 questions and their solutions. Each question contains a code snippet to solve a programming problem, such as determining if a number is even or odd, finding the greatest of three numbers, calculating factorials using recursion, and more. The code examples demonstrate basic programming concepts like loops, functions, recursion, and parameter passing.

Uploaded by

svawhites
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

INTRODUCTION TO

COMPUTER PROGRAMMING
Subject Code: - 2CORC5

LAB MANUAL
BE Ist Year (ETC-B)

Submitted to- Submitted by –

Name: - Ms. Jasneet Kaur Monga Name: ATHARAV TRIPATHI


Roll No: 23T1112
IET, DAVV Indore

Department of Electronics & Telecommunication Engineering


Institute of Engineering and Technology, Devi Ahilya Vishwavidyalaya,
Indore (MP)

Page | 1
Q.1 Program to find the number is even or odd

Code:

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter any positive number : ";
cin >> n;
if (n % 2 == 0)
{
cout << "The number is even ";
}
else
{
cout << "The number is odd ";
}
return 0;
}

Output:

Page | 2
Q.2 Program to find out greatest of three given numbers

Code:

#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3;
cout << "enter any three number's : ";
cin >> n1 >> n2 >> n3;
if (n1 > n2 && n1 > n3)
{
cout << "The greatest number is : " << n1;
}
else if (n2 > n1 && n2 > n3)
{
cout << "The greatest number is : " << n2;
}
else
{
cout << "The greatest number is : " << n3;
}
return 0;
}

Output:

Page | 3
Q.3 Program to find whether the entered year is leap year or not.

Code:

#include <iostream>
using namespace std;
int main()
{
int y;
cout << "Enter any year : ";
cin >> y;
if (y % 4 == 0)
{
cout << "The year " << y << " is a leap year ";
}
else
{
cout << "The year " << y << " is not a leap year ";
}
return 0;
}

Output:

Page | 4
Q.4 Program to find whether the given number is prime or composite.

Code:

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter any number : ";
cin >> n;
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
cout << "The number is composite ";
break;
}
else
{
cout << "The number is prime ";
break;
}
}
return 0;
}

Output:

Page | 5
Q.5 Program to print the sum of first n natural numbers.

Code:

#include <iostream>
using namespace std;
int main()
{
int n, sum;
sum = 0;
cout << "Enter the number of terms : ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum += i;
}
cout << "The sum of first " << n << " natural numbers is : " << sum;
return 0;
}

Output:

Page | 6
Q.6 Program to find sum of digits of n digit number.

Code:

#include <iostream>
using namespace std;
int main()
{
int n, og;
og = n;
cout << "Enter any number : ";
cin >> n;
int digit;
int sum = 0;
while (n != 0)
{
digit = n % 10;
sum += digit;
n = n / 10;
}
cout << "The sum of the digits of is : " << sum;

return 0;
}

Output:

Page | 7
Q.7 Program to find reverse of a number.

Code:

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter any number : ";
cin >> n;
int rev, sum;
sum = 0;
rev = 0;
while (n != 0)
{
rev = rev * 10 + n % 10;
n = n / 10;
}
cout << "The reverse of the number is : " << rev;
return 0;
}

Output:

Page | 8
Q.8 Program to print the day of week according to day number entered by the user.

Code:

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the day number between (1-7) : ";
cin >> n;
if (n == 1)
{
cout << "Monday ";
}
if (n == 2)
{
cout << "Tuesday ";
}
if (n == 3)
{
cout << "Wednesday";
}
if (n == 4)
{
cout << "Thursday ";
}
if (n == 5)
{
cout << "Friday ";
}
if (n == 6)
{
cout << "Saturday ";
}
if (n == 7)
{
cout << "Sunday ";
}
if (n > 7 || n < 1)
{
cout << "Invalid input";
}
return 0;
}

Page | 9
Output:

Page | 10
Q.11 Program to print all the prime numbers between a given range.

Code:
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Enter the range " << endl;
cout << "Starting : ";
cin >> n1;
cout << "Ending : ";
cin >> n2;
cout << "The prime numbers are " << endl;
for (int i = n1; i <= n2; i++)
{
for (int j = 2; j <= i / 2; j++)
{ if (i % 2 == 0)
{
}
else
{ cout << i << endl;
break;
}
}
}
return 0;
}

Output:

Page | 11
Q.12 Program to print all the 3 digit Armstrong numbers.

Code:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout << "The amstrong number between 100 - 999 is " << endl;
int digit;
int sum = 0;
for (int i = 100; i <= 999; i++)
{
int og = i;
while (og != 0)
{
digit = og % 10;
sum += pow(digit, 3);
og = og / 10;
}
if (sum == i)
{
cout << i << endl;
}
sum = 0;
}
return 0;
}

Output:

Page | 12
Q.13 Program to print all the palindromes between a range.

Code:

#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Enter the range " << endl;
cout << "Starting : ";
cin >> n1;
cout << "Ending : ";
cin >> n2;
int rev;
cout<<"The palindrome numbers between the given range is : "<<endl;
for (int i = n1; i <= n2; i++)
{
int og = i;
while (og > 0)
{ rev = rev * 10 + og % 10;
og = og / 10;
}
if (rev == i)
{
cout << i << endl;
}
rev = 0;
}
return 0;
}

Output:

Page | 13
Q.14 Program to print factorial of a number using function.

Code:

#include <iostream>
using namespace std;
int factorial(int n)
{
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i;
}
return fact;
}
int main()
{
int x;
cout << "Enter any number : ";
cin >> x;
int result = factorial(x);
cout << "The value of " << x << " factorial is : " << result;
return 0;
}

Output:

Page | 14
Q.15 Program to print factors of a given number using a function.

Code:

#include <iostream>
using namespace std;
void factors(int n)
{
for (int i = 1; i <= n / 2; i++)
{
if (n % i == 0)
{
cout << i << " ";
}
}
}
int main()
{
int a;
cout << "Enter any number : ";
cin >> a;
cout << "The factors of the given number is " << endl;
factors(a);
return 0;
}

Output:

Page | 15
Q.16 Program to swap values of two variables using call by value & call by reference.

Code:

Call by value:
#include <iostream>
using namespace std;
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x, y;
cout << "Enter the value of x : ";
cin >> x;
cout << "Enter the value of y : ";
cin >> y;
swap(x, y);
cout << "The value of x is " << x << " & y is " << y << endl;
return 0;
}

Output:

Page | 16
Call by reference:

#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
cout << "Enter the value of x : ";
cin >> x;
cout << "Enter the value of y : ";
cin >> y;
swap(&x, &y);
cout << "The value of x is " << x << " & y is " << y << endl;
return 0;
}

Output:

Page | 17
Q.17 Program to print sum of first n natural numbers using recursion.

Code:

#include <iostream>
using namespace std;
int sum(int n)
{
if (n == 0)
{
return 0;
}
else
{
return n + sum(n - 1);
}
}
int main()
{
int a;
cout << "Enter any number : ";
cin >> a;
cout<<"The sum of first "<<a<<" natural numbers is = ";
cout << sum(a);
return 0;
}

Output:

Page | 18
Q.18 Program to print Fibonacci series upto n terms using recursion.

Code:

#include <iostream>
using namespace std;
void fibo(int a, int b, int i, int n)
{
cout << a << " ";
if (i >= n)
{
}
else
{
fibo(b, a + b, i + 1, n);
}
}
int main()
{
int a, b, n;
a = 0, b = 1;
cout << "Enter the number of terms : ";
cin >> n;
cout<<"The fibonacci series upto "<<n<<" terms is : ";
fibo(a, b, 1, n);
return 0;
}

Output:

Page | 19
Q.19 Program to covert decimal to binary/octal/hexadecimal using recursion.

Code:

#include <iostream>
using namespace std;
void binary(int n)
{
if (n != 0)
{
int rem = n % 2;
binary(n / 2);
cout << rem;
}
}
int main()
{
int n;
cout << "Enter any number : ";
cin >> n;
cout << "The Binary of the following number is : ";
binary(n);
return 0;
}

Output:

Page | 20
Q.20 Program to find the largest and the smallest element in the array.

Code:

#include<iostream>
using namespace std;
int main (){
int s[4] ;
cout<<"Enter the set : ";
for( int i=0; i<=3 ; i++)
{
cin>>s[i];
}
int small = s[0];
for( int i=1; i<= 3; i++)
{
if(s[i]< small)
{
small = s[i];
}
else
{
}
}
cout<<"The smallest among the following is : "<<small;
return 0;
}

Output:

Page | 21
Q.22 Program to multiply two matrices.
Code:
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,b,h;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>b>>h;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}

Page | 22
Q.23 Program to find reverse of a string.

Code:

Page | 23
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name[20];
cout << "Enter any string : ";
cin.getline(name, 20);

cout << "The reverse of the string is : " << strrev(name);


return 0;
}

Output:

Page | 24
Page | 25
Page | 26

You might also like