Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
391 views

Cocubes Coding Assignment 2

1) The document contains 5 C++ programs to solve different programming problems: swapping two numbers, finding prime factors of a number, checking if a number is perfect, calculating LCM of two numbers, and printing the first n prime numbers. 2) Each program is presented with the source code and sample output. 3) The programs cover basic programming concepts like loops, conditional statements, functions, and input/output operations.

Uploaded by

Amanpreet Dutta
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)
391 views

Cocubes Coding Assignment 2

1) The document contains 5 C++ programs to solve different programming problems: swapping two numbers, finding prime factors of a number, checking if a number is perfect, calculating LCM of two numbers, and printing the first n prime numbers. 2) Each program is presented with the source code and sample output. 3) The programs cover basic programming concepts like loops, conditional statements, functions, and input/output operations.

Uploaded by

Amanpreet Dutta
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/ 4

Prestige Institute of Engineering Management and Research, Indore

B.Tech. (Computer Science) III Year/V Semester Section A


Submitted By: Amanpreet Dutta
Enrollment no:0863CS201019
Session 2
Cocubes Assignment 2
Programming Concepts
Department : CSE Session: 2020-2024
Name of Faculty: Prof. Prashant Shakyawar Semester: V
Subject: Cocubes Coding
Program to swap two numbers using third variable.
#include <iostream>
using namespace std;

int main()
{
    int a, b, temp;
    cout << "Enter two Numbers:";
    cin >> a >> b ;
    cout << "Before swapping the numbers are:";
    cout << a << "," << b << endl;
    temp = a;
    a = b;
Q.1.     b = temp;
    cout << "After swapping the numbers are:";
    cout << a << "," << b << endl;
    return 0;
}
OUTPUT:

Q.2. Program to find prime factors of a given integer.


#include<iostream>

using namespace std;

int main()
{
    int number, i, j, count;
   
    cout << "\nPlease Enter the Number to find the Prime Factors =  ";
    cin >> number;
   
    for(i = 1; i <= number; i++)
    {
        count = 0;
        if(number % i == 0)
        {          
            for(j = 1; j <= i; j++)
            {
                if(i % j == 0)
                {
                    count++;
                }              
            }
            if(count == 2)
            {
                cout << i << " is a Prime Factor\n";
            }
        }
    }
       
    return 0;
}
OUTPUT:

Q.3. Program to check given number is perfect or not.


#include<iostream>
using namespace std;
int main ()
{  
    int i, num, div, sum=0;
    cout << "Enter the number to be checked : ";
    cin >> num;
    for (i=1; i < num; i++)
    {
        div = num % i;
        if (div == 0)
            sum = sum + i;
    }
    if (sum == num)
        cout << "\n" << num <<" is a perfect number.";
    else
        cout << "\n" << num <<" is not a perfect number.";
    return 0;
}
OUTPUT:

Program to calculate LCM of given two numbers.


#include<iostream>
using namespace std;
int main()
{
  int num1, num2, max;
  cout << "Enter two Integers: ";
  cin >> num1 >> num2;
  max = (num1 > num2) ? num1 : num2;
  while(true)
  {
    if(max % num1 == 0 && max % num2 ==0)
    {
      cout << "LCM = " << max << endl;
Q.4.
      break;
    }
    else
      max++;
  }
  return 0;
}
OUTPUT:

Q.5. Program to print first n Prime Number.


#include <iostream>
using namespace std;
int main()
{
int n;
int num = 1;
int primeCount = 0;
cout<< "Enter how many prime numbers you want: ";
cin>> n;
cout<< 1 << '\n';
for(int j = 1; j <= n; j++)
{
    num++;
    primeCount = 0;
    for(int i = 1; i <= num; i++)
    {
        if(num % i == 0)
        {
            primeCount++;
        }
    }
        if(primeCount == 2)
        {
            cout<< num << '\n';
        }
}
return 0;
}
OUTPUT:

You might also like