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

Write A Program in C

The document contains code snippets for various C++ programs to calculate sums, factorials, check leap years, generate multiplication tables, calculate areas of shapes, print patterns like diamond and pyramid of stars, make a basic calculator, find Fibonacci series, check if a number is factorial, and calculate average of numbers using arrays. The programs demonstrate the use of basic concepts like loops, functions, conditional statements, arrays in C++.

Uploaded by

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

Write A Program in C

The document contains code snippets for various C++ programs to calculate sums, factorials, check leap years, generate multiplication tables, calculate areas of shapes, print patterns like diamond and pyramid of stars, make a basic calculator, find Fibonacci series, check if a number is factorial, and calculate average of numbers using arrays. The programs demonstrate the use of basic concepts like loops, functions, conditional statements, arrays in C++.

Uploaded by

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

Write a program in c++ to calculate the sum of first 10 natural number.

Coding will be:-

#include<iostream.h>

int main()
{
int i=1,sum=0;

while(i<=10)
{
sum+=i;
i++;
}

cout<<"Sum :"<<sum;

return 0;
}

Output will be:-


Write a program in C++ program to Calculate Factorial of a Number Using Recursion.

Coding will be:-


/* Source code to find factorial of a number. */

#include<iostream.h>

int factorial(int n);

int main()

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;}

int factorial(int n)

{ if(n!=1)

return n*factorial(n-1);

Output will be:-


Write a program in C++ Program to Check Leap Year.

Coding will be:-


#include <iostream.h>

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if (year%4 == 0) {

if (year%100 == 0) {

if (year%400 == 0) {

cout << year << " is a leap year."; }

else { cout << year << " is not a leap year."; }

else { cout << year << " is a leap year."; }

else { cout << year << " is not a leap year.";}

return 0;

Output will be:-


Write a program in C++ Program to Generate Multiplication Table.

Coding will be:-


#include <iostream.h>

int main() {

int n;

cout << "Enter an integer: ";

cin >> n;

for (int i = 1; i <= 10; ++i) {

cout << n << " * " << i << " = " << n*i << endl;

return 0;

Output will be:-


Write a program in C++ program to find area of square,rectangle,circle and triangle .

Coding will be:-


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

{clrscr();

float a,b,c,s,r,area; int ch;

cout<<">>>>>>>>>>Menu<<<<<<<<<<";

cout<<">>>>>>>>>>1.Area of circle<<<<<<<<<<";

cout<<">>>>>>>>>>2.Area of Rectangle<<<<<<<<<<";

cout<<">>>>>>>>>>3.Area of triangle.<<<<<<<<<< ";

cout<<" >>>>>>>>>>Enter your choice:<<<<<<<<<<";

cin>>ch;

switch(ch)

{case 1:

{cout<<"Enter radius of the circle:";

cin>>r;

area=3.14*r*r;

break;

} case 2:{cout<<"Enter length and breadth:";

cin>>a>>b; area=a*b;

break;

}case 3:{cout<<"Enter three sides of the triangle:";

cin>>a>>b>>c;

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

break;

} default: cout<<"Wrong choice!!!";

break;

} cout<<"Area="<<area;

getch();
Output will be:-
Write a program in C++ Program to Calculate Average of Numbers Using Arrays.

Coding will be:-


#include <iostream.h>

#include <conio.h>

int main()

{ clrscr ();

int i, n, t1 = 0, t2 = 1, nextTerm = 0;

cout<<"Enter the number of terms: ";

cin>>n;

cout<<"\n";

cout<<"Fibonacci Series: "<<t1;

cout<<"\n";

cout<< t2;

for (i=3; i <= n; ++i) // i = 3 because the first two terms are already dislpayed

{ nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

cout<<"\n"<<nextTerm;

} return 0;

Output will be:-


Write a program in C++ Program to Print Diamond of Star.

Coding will be:-


#include<iostream.h>

#include<conio.h>

void main()

{ clrscr ();

int i, j, k;

for(i=1;i<=5;i++)

{for(j=i;j<5;j++)

{cout<<" ";}for(k=1;k<(i*2);k++)

{cout<<"*";

}cout<<"\n";

}for(i=4;i>=1;i--)

{for(j=5;j>i;j--)

{cout<<" ";

}for(k=1;k<(i*2);k++)

{cout<<"*";

}cout<<"\n";

}getch();

Output will be:-


Write a program in C++ to Print pyramid of 1 12 123 to n terms.

Coding will be:-


#include <iostream.h>

#include<conio.h>

int main()

{ clrscr ();

int rows=0,i=0, j=0;

cout << "Enter number of rows: ";

cin >> rows;

int k=rows;

i=1;

while (i <= rows)

{ j=1;

while(j <= i)

{ cout << j;

j++;

} cout << endl ;

i++;

} return (0);

Output will be:-


Make Simple Calculator in C++ programming

Coding will be:-

/* Source code to create a simple calculator for addition, subtraction, multiplication


and division using switch...case statement in C++ programming. */

# include <iostream.h>

#include<conio.h>

int main()

{ clrscr ();

char p; float num1,num2;

cout << "Enter operator either + or - or * or /: ";

cin >> p;

cout << "Enter two operands: ";

cin >> num1 >> num2;

switch(p) {

case '+':

cout << num1+num2;

break;

case '-':

cout << num1-num2;

break;

case '*':

cout << num1*num2;

break;

case '/':

cout << num1/num2;

break;

default:

/* If operator is other than +, -, * or /, error message is shown */

cout << "Error! operator is not correct";

break;

} return 0;

}
Output will be:-
Write a program in C++ to check whether a no is factorial or not

Coding will be:-


# include <iostream.h>

#include <conio.h>

int main()

{ clrscr ();

int b[19],i, j = 0,k, l ;

/*writing factorials*/

for (i = 0; i <= 19; i++) {

k = i + 1; b[i] = k; }

cout<<"enter a number"<<"\n";

cin>>l; for (j = 0; j <= 19; j++) {

if (l == b[j]) { cout<<"given number is a factorial of"<<"\n"<< j + 1;

} if (j == 19 && l != b[j]) { cout<<"given number is not a factorial number"<<"\n";

} }} int isFactorial(int a)

{ int I, facto = 1;

for (i = 1; i <= a; i++) {

facto = facto * i;

} return facto;

Output will be:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy