You are on page 1of 4

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;

temp=a; a=b; b=temp; cout<<"\nAfter swaping\na="<<*a<<"\nb="<<*b; getch(); }


#include<stdio.h> #include<conio.h> main() { int *x,*y,a=20,b=10,c; printf("\nEnter the value of a and b:"); scanf("%d\n%d",&a,&b); x=&a; y=&b; c=*x; *x=*y; *y=c; printf("a=%d\nb=%d",a,b); }

Simple Constructor and Destructor


#include <iostream> #include <iostream> using namespace std; class myclass { int a,b; public: myclass(); // constructor ~myclass(); // destructor - no need to call the destructor void show(); }; myclass::myclass() { cout << "In constructor\n"; a = 30; b = 20; } myclass::~myclass() { //invoked before removing objects from memory cout << "Destructing...\n"; }

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; }

You might also like