0% found this document useful (0 votes)
3 views5 pages

c++ All Programs

The document contains a series of C++ programs demonstrating basic programming concepts. These include calculating the factorial of a number, finding the length of a string, displaying multiplication tables, checking if a number is positive, negative, or zero, summing digits, and swapping numbers. Additional examples include finding the largest of three numbers, designing a triangle of stars, and determining the day of the week based on user input.

Uploaded by

gishnuvishnu2007
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)
3 views5 pages

c++ All Programs

The document contains a series of C++ programs demonstrating basic programming concepts. These include calculating the factorial of a number, finding the length of a string, displaying multiplication tables, checking if a number is positive, negative, or zero, summing digits, and swapping numbers. Additional examples include finding the largest of three numbers, designing a triangle of stars, and determining the day of the week based on user input.

Uploaded by

gishnuvishnu2007
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/ 5

1.

Factorial of a number with the help of a user-defined function

#include <iostream>
using namespace std;
double factorial(int n)
{
long int i,fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
return fact;
}
int main()
{
int n;
cout<<"Input a number ";
cin>>n;
cout<<" factorial of "<<n<<" = "<< factorial(n);
}

2. Find the length of a string without using strlen() function.

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int count=0,i;
char name[25];
cout<<"Length of string\n";
cout<<"Input a name\t";
cin>>name;
for(i=0;name[i]!='\0';i++)
{
count++;
}
cout<<" number of characters = "<<count<<endl;
}
3. Display the multiplication table of a number

#include <iostream>
using namespace std;
int main()
{
int n,i,p;
cout<<"Multiplication Table of a number\n";
cout<<"Input a number\t";
cin>>n;
for(i=1;i<=10;i++)
{
p=n*i;
cout<<i <<" x "<<n<<" = "<<p<<endl;
}
}

4. Program to check the given number is positive ,negative or zero

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number";
cin>>n;
if(n>0)
cout<<"The number is positive";
else if(n<0)
cout<<"The number is negative";
else
cout<<"The number is Zero";
}

5. Find the sum of the digits of an integer number.

#include<iostream>
using namespace std;
int main()
{
int n,t,d,s=0;
cout<<"Sum of digits of a number\n";
cout<<"Input a number\t";
cin>>n;
t=n;
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
cout<<"Sum of digits of "<<t<<" = "<<s;
}

6. Find the sum of the squares of the first N natural numbers

#include <iostream>
using namespace std;
int main()
{
int n,i,sum=0,sq;
cout<<"sum of First n Natural numbers\n";
cout<<"Input a number\t";
cin>>n;
for(i=1;i<=n;i++)
{
sq=i*i;
sum=sum+sq;
}
cout<<" Answer = "<<sum<<endl;
}

7. Input two numbers and swap them with the


help of a user defined function.

#include<iostream>
using namespace std;
void swap(int &A, int &B)
{
int temp;
temp=A;
A=B;
B=temp;
}
int main()
{
int X, Y;
cout<<"Enter two numbers:";
cin>>X>>Y;
cout<<"\n Before Swapping\n";
cout<<"X="<<X<<" Y="<<Y;
swap(X,Y);
cout<<"\n After Swapping\n";
cout<<"X="<<X<<" Y="<<Y;
return 0;
}

8. Input three numbers and find the largest

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers ";
cin>>a>>b>>c;
if (a>b && a>c)
cout<<"Biggest is "<<a;
else if(b>c)
cout<<"Biggest is "<<b;
else
cout<<"Biggest is "<<c;
}

9. Program to design triangle of Stars

#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
10. Program to find week days

#include<iostream>
using namespace std;
int main()
{
int day;
cout<<" Enter the day (1-7)";
cin>>day;
switch(day)
{
case 1: cout<<"Sunday \n";
break;
case 2: cout<<"Monday \n";
break;
case 3: cout<<"Tuesday \n";
break;
case 4: cout<<"Wednesday \n";
break;
case 5: cout<<"Thursday \n";
break;
case 6: cout<<"Friday \n";
break;
case 7: cout<<"Saturday \n";
break;
default:cout<<" Invalid day \n";
}
return 0;
}

You might also like