Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts
Saturday, February 14, 2015
C program to print first n natural numbers and their sum
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("
%d",i);
sum+=i;
}
printf("
Sum of first n natural numbers is:%d",sum);
getch(); //to stop the screen
}
Thursday, February 12, 2015
C program to find LCM and HCF of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,hcf,lcm,max,min,r;
clrscr();
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a>b)
{
max=a;
min=b;
}
else
if(b>a)
{
max=b;
min=a;
}
if(a==b)
hcf=a;
else
{
do
{
r=max%min;
max=min;
min=r;
}while(r!=0);
hcf=max;
}
lcm=(a*b)/hcf;
printf("
LCM=%d
HCF=%d",lcm,hcf);
getch();
}
C Templates Program to Swap Two Numbers Using Function Template

What are Templates in C++?
Templates help in defining generic classes and functions and hence allow generic programming. Generic programming is an approach where generic data types are used as parameters and the same piece of code work for various data types.
Function templates are used to create family of functions with different argument types. The format of a function template is shown below:
template<class T>
return_type function_name (arguments of type T)
{
. . . . .
. . . . .
}
I have written a program below which will swap two numbers using function templates.
#include<iostream>
using namespace std;
template <class T>
void swap(T&a,T&b) //Function Template
{
T temp=a;
a=b;
b=temp;
}
int main()
{
int x1=4,y1=7;
float x2=4.5,y2=7.5;
cout<<"Before Swap:";
cout<<"
x1="<<x1<<" y1="<<y1;
x1="<<x1<<" y1="<<y1;
cout<<"
x2="<<x2<<" y2="<<y2;
x2="<<x2<<" y2="<<y2;
swap(x1,y1);
swap(x2,y2);
cout<<"
After Swap:";
After Swap:";
cout<<"
x1="<<x1<<" y1="<<y1;
x1="<<x1<<" y1="<<y1;
cout<<"
x2="<<x2<<" y2="<<y2;
x2="<<x2<<" y2="<<y2;
return 0;
}
There are so many things that I have missed in this tutorial. In below video templates are explained very nicely. I am sure that after watching this video you will understand templates very easily.
Wednesday, February 11, 2015
C Program to Print Numbers From 1 to n Without Using Loops Recursion or Goto Statement
You may have made program to print numbers from 1 to n using loops, recursion or goto statement. But have you ever thought about doing this without help of loops, recursion or goto? It can be done without using such things as explained in below program.
Also Read: C program to find factorial of any number using recursion
Also Read: C++ Templates: Program to Swap Two Numbers Using Function Template

Also Read: C program to find factorial of any number using recursion
Also Read: C++ Templates: Program to Swap Two Numbers Using Function Template
#include<iostream>
using namespace std;
class Num
{
public:
static int i;
Num()
{
cout<<i++<<" ";
}
};
int Num::i=1;
int main()
{
int n;
cout<<"Enter value on n:";
cin>>n;
Num obj[n];
return 0;
}
In this program we are using the concept of static data member and array of objects. Class Numcontains a static variable i whose value will remain till the program terminates. We are creating an array of objects of class Num. In this program we are creating n objects, value of n depends on input we give. The default constructor is called for all objects one by one. In the constructor the value of i is printed and is incremented by one in each call. In this way numbers from 1 to n are printed.
If you have any doubts or any suggestion regarding above program then you can ask it by commenting below.
Source: http://www.geeksforgeeks.org/output-of-c-program-set-18-3/
Subscribe to:
Posts (Atom)