0% found this document useful (0 votes)
23 views8 pages

(Lab - Final) C++ Programs : Program # 01

This document contains 17 C++ programs. Program 2 calculates the sum of digits of a number input by the user. Program 4 reverses a given number. Program 5 checks if a number is a palindrome. Program 8 calculates the greatest common divisor of two numbers using Euclid's algorithm. Program 10 checks if a given number is prime.

Uploaded by

CH Naveed Ilyas
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)
23 views8 pages

(Lab - Final) C++ Programs : Program # 01

This document contains 17 C++ programs. Program 2 calculates the sum of digits of a number input by the user. Program 4 reverses a given number. Program 5 checks if a number is a palindrome. Program 8 calculates the greatest common divisor of two numbers using Euclid's algorithm. Program 10 checks if a given number is prime.

Uploaded by

CH Naveed Ilyas
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/ 8

( LAB.

FINAL )

* C++ PROGRAMS *
Program # 01:

Program # 02:
1. #include <iostream>  
2. using namespace std;  
3. int main()  
4. {  
5. int n,sum=0,m;    
6. cout<<"Enter a number: ";    
7. cin>>n;    
8. while(n>0)    
9. {    
10. m=n%10;    
11. sum=sum+m;    
12. n=n/10;    
13. }    
14. cout<<"Sum is= "<<sum<<endl;    
15. return 0;  
16. } 

Program # 03:

Program # 04:
1. #include <iostream>  
2. using namespace std;  
3. int main()  
4. {  
5. int n, reverse=0, rem;    
6. cout<<"Enter a number: ";    
7. cin>>n;    
8.   while(n!=0)    
9.   {    
10.      rem=n%10;      
11.      reverse=reverse*10+rem;    
12.      n/=10;    
13.   }    
14.  cout<<"Reversed Number: "<<reverse<<endl;     
15. return 0;  
16. } 

Program # 05:
1. #include <iostream>  
2. using namespace std;  
3. int main()  
4. {  
5.   int n,r,sum=0,temp;    
6.   cout<<"Enter the Number=";    
7.   cin>>n;    
8.  temp=n;    
9.  while(n>0)    
10. {    
11.  r=n%10;    
12.  sum=(sum*10)+r;    
13.  n=n/10;    
14. }    
15. if(temp==sum)    
16. cout<<"Number is Palindrome.";    
17. else    
18. cout<<"Number is not Palindrome.";   
19.   return 0;  
20. } 

Program # 06:

Program # 07:
1. #include <iostream>  
2. using namespace std;  
3. int main()  
4. {  
5.    int i,fact=1,number;    
6.   cout<<"Enter any Number: ";    
7.  cin>>number;    
8.   for(i=1;i<=number;i++){    
9.       fact=fact*i;    
10.   }    
11.   cout<<"Factorial of " <<number<<" is: "<<fact<<endl;  
12.   return 0;  
13. } 

Program # 08:
#include <stdio.h>
int main()
{
int n1, n2;

printf("Enter two positive integers: ");


scanf("%d %d",&n1,&n2);

while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);

return 0;
}
Program # 09:
#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

minMultiple = (n1>n2) ? n1 : n2;


while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}
Program # 10:
1. #include <iostream>  
2. using namespace std;  
3. int main()  
4. {  
5.   int n, i, m=0, flag=0;  
6.   cout << "Enter the Number to check Prime: ";  
7.   cin >> n;  
8.   m=n/2;  
9.   for(i = 2; i <= m; i++)  
10.   {  
11.       if(n % i == 0)  
12.       {  
13.           cout<<"Number is not Prime."<<endl;  
14.           flag=1;  
15.           break;  
16.       }  
17.   }  
18.   if (flag==0)  
19.       cout << "Number is Prime."<<endl;  
20.   return 0;  
21. }

Program # 11:

Program # 12:

Program # 13:

Program # 14:
#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=0; i<rows; i++)


{
for(space=1; space <= rows-i; space++)
printf(" ");

for(j=0; j <= i; j++)


{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}

return 0;
}
Program # 15:
#include<iostream>
using namespace std;
main()
{
for(int i=11;i>=1;i-=2){
for(int j=1;j<=9;j++){
if(j>=i)
cout<<"* ";
else
cout<<" ";
}
cout<<" "<<"\n";
}
}
Program # 16:

Program # 17:

You might also like