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

1) Find Sum of 'N' Natural Numbers.

The document contains code snippets in C++ demonstrating different programming concepts: 1) A for loop to calculate the sum of natural numbers from 1 to N. 2) Function definitions to calculate sum of natural numbers. 3) Code to swap values of two variables using a temporary variable. 4) Code to swap values of two variables using pointers. 5) Simple example of a constructor and destructor in a class to initialize and clean up objects. 6) Code to calculate factorial of a number using a for loop. 7) Definition of a structure called 'date' to store day, month, year and taking input in its members.

Uploaded by

Karthik Sekhar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

1) Find Sum of 'N' Natural Numbers.

The document contains code snippets in C++ demonstrating different programming concepts: 1) A for loop to calculate the sum of natural numbers from 1 to N. 2) Function definitions to calculate sum of natural numbers. 3) Code to swap values of two variables using a temporary variable. 4) Code to swap values of two variables using pointers. 5) Simple example of a constructor and destructor in a class to initialize and clean up objects. 6) Code to calculate factorial of a number using a for loop. 7) Definition of a structure called 'date' to store day, month, year and taking input in its members.

Uploaded by

Karthik Sekhar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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