1) Find Sum of 'N' Natural Numbers.
1) Find Sum of 'N' Natural Numbers.
#include<conio.h> #include<iostream.h> void main() { clrscr(); int sum=0,i,num; cout<<"Enter a number"; cin>>num; for(i=1;i<=num;i++) { sum=sum+i; } cout<<"Sum of "<<num<<"natural numbers = " <<sum; getch(); }
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,summation=0; cout<<"How many numbers? "; cin>>a; summation=sumnatural(a); getch(); } int sumnatural(int n) { int i, n,sum=0; for(i=1;i<=n;++i) { sum+=i; cout<<"\nSUM="<<sum<<endl; }}
#include<iostream.h> #include<conio.h> void main() { clrscr(); int *a,*b,*temp; cout<<"Enter value of a and b:"; cin>>*a>>*b;
void myclass::show() { cout << "A =" << a << endl << "B =" << b << endl; cout << "Sum is " << a+b << endl; } int main() { myclass ob; ob.show(); return 0; }
OUTPUT: In constructor A =30 B =20 Sum is 50 Destructing
#include"conio.h" #include"iostream.h" void main() { clrscr(); // to clear the screen int n,fact=1; cout<<"Please enter a number to find factorial :: "; cin>>n; for(int i=n;i>=1;i--) { fact=fact*i; } cout<<"Factorial of "<<n<<" is="" ::="" "<<fact;
getch(); } #include<iostream.h> // declearation of structure struct date{ int day; int month; int year; }; int main(){
// object defination date today; cout << "A program for displaying today's information"; cout << endl; // collecting values from the user cout << "Day :"; cin >> today.day; cout << "Month:"; cin >> today.month; cout << "Year :"; cin >> today.year; cin.get(); return 0; }