Pages

Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, February 18, 2015

C Program to delete an element from an Array

C++ Program to delete an element from an Array

#include<iostream.h>
#include<process.h>
#include<conio.h>

void main()
{
clrscr();
int a[50],x,n,i,j,b[50];
cout<<"How many elements of Array you want to create?";
cin>>n;
cout<<"Enter elements of Array
";

for(i=0;i<n;++i)
cin>>a[i];
cout<<"Enter element to delete:";
cin>>x;

for(i=0,j=0;i<n;++i)
{
if(a[i]!=x)
b[j++]=a[i];
}

if(j==n)
{
cout<<"
Soory!!!Element is not in the Array";

exit(0);
}
else
{
cout<<"
New Array is ";

for(i=0;i<j;i++)
cout<<b[i]<<" ";
}
getch();
}
Read more »

Tuesday, February 17, 2015

C Program to Convert given no of days into years weeks and days

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int y,d,w;

cout<<"Enter No. of days:";
cin>>d;

y=d/365;
d=d%365;
w=d/7;
d=d%7;

cout<<"
Years: "<<y<<"
Weeks: "<<w<<"
Days: "<<d;

getch();
}
Read more »

C Program to Count no of words in a string

C++ Program to Count no. of words in a string

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
char a[20];
int i,count=0;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!=
Read more »

C program to read a string and print it in alphabetical order


C program to read a string and print it in alphabetical order

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i,j,n,ch1,ch2;
char a[50],temp;
clrscr();
printf("Enter any string:");
scanf("%s",a);
n=strlen(a);

for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
{
ch1=a[j];
ch2=a[j+1];
if(ch1>ch2)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

printf("String after arranging %s",a);
getch();
}
Read more »

C Program check whether a number is palindrome or not


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long n,num,d,rev=0;
cout<<"Enter any number: ";
cin>>n;
num=n;
do
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}while(n!=0);
if(num==rev)
cout<<endl<<"Number is Palindrome";
else
cout<<endl<<"Number is not Palindrome";
getch();
}

Read more »

Monday, February 16, 2015

C Program to check whether a String is Palindrome or not

C++ Program to check whether a String is Palindrome or not

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int i,j,len,flag=1;
char a[20];
cout<<"Enter a string:";
cin>>a;

for(len=0;a[len]!=
Read more »

C program to find sum of elements above and below the main digonal of a matrix


C program to find sum of elements above and below the main digonal of a matrix

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,m,n,d1=0,d2=0,a[5][5];
clrscr();
printf("How many rows and columns:");
scanf("%d%d",&m,&n);
printf("Enter matrix elements:
");


for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
if(j>i)
d1+=a[i][j];
else
if(i>j)
d2+=a[i][j];
}

printf("Sum of elements above the digonal=%d
",d1);

printf("Sum of elements below the digonal=%d",d2);
getch();
}
Read more »

Sunday, February 15, 2015

C program to calculate area of a circle a rectangle or a triangle depending upon users choice




#include<iostream.h>
#include<conio.h>
#include<math.h>


void main()
{
clrscr(); //to clear the screen
float a,b,c,s,r,area;
int ch;
cout<<"***Menu***
1.Area of circle
2.Area of Rectangle";

cout<<"
3.Area of triangle
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(); //to stop the screen
}
Read more »

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
}
Read more »

Friday, February 13, 2015

C program to find largest and smallest element from a 1D array


C program to find largest and smallest element from a 1D array

#include<stdio.h>
#include<conio.h>

void main()
{
int a[50],i,n,large,small;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);

large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}

printf("The largest element is %d",large);
printf("
The smallest element is %d",small);

getch();
}
Read more »

Thursday, February 12, 2015

C program to check whether the given number is palindrome or not


#include<stdio.h>
#include<conio.h>

void main()
{
int n,a,b=0,num;
clrscr(); //to clear the screen
printf("Enter any number:");
scanf("%d",&n);
num=n;

while(n!=0)
{
a=n%10;
b=a+(b*10);
n=n/10;
}
if(num==b)
printf("
The given number is palindrome");

else
printf("
The given number is not palindrome");

getch(); //to clear the screen;
}
Read more »

C Program to do arithmetic operations according to user choice using switch case

include<iostream.h>
#include<conio.h>

void main()
{       clrscr();
int a,b;
char c;
cout<<"Enter any expression(ex:3*7):";
cin>>a>>c>>b;

switch(c)
{
case+: cout<<"
Result:"<<a+b;

break;

case-: cout<<"
Result:"<<a-b;

break;

case*: cout<<"
Result:"<<a*b;

break;

case/: cout<<"
Result:"<<a/b;

break;

case%:  cout<<"
Result:"<<a%b;

break;
}

getch();
}
Read more »

C program to find LCM and HCF of two numbers

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();
}
Read more »

C Templates Program to Swap Two Numbers Using Function Template

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;
    cout<<"
x2="<<x2<<" y2="<<y2;

    swap(x1,y1);
    swap(x2,y2);

    cout<<"

After Swap:";
    cout<<"
x1="<<x1<<" y1="<<y1;
    cout<<"
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.

Read more »

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

#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.

C++ Program to Print Numbers From 1 to n Without Using Loops, Recursion or Goto Statement

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/
Read more »

C program to find whether a given character is an alphabet digit or any special character using ASCII values


#include<stdio.h>
#include<conio.h>


void main()
{
    char ch;
    clrscr();    //to clear the screen
    printf("Enter any character:");
    scanf("%c",&ch);
    if((ch>=A&&ch<=Z)||(ch>=a&&ch<=z))
        printf("
You have entered an alphabet");

    else
        if(ch>=0&&ch<=9)
            printf("
You have entered a digit");

        else
            printf("
You have entered a special character");



    getch();    //to stop the screen
}
Read more »

C program to print fibonacci series i e 0 1 1 2 3 5 8


#include<stdio.h>
#include<conio.h>

void main()
{
int first=0,second=1,third,i,n;
clrscr();  //to clear the screen

printf("Enter how many elements?");
scanf("%d",&n);
printf("
%d %d",first,second);


for(i=2;i<n;++i)
{
third=first+second;
printf(" %d",third);
first=second;
second=third;
}
getch();  //to stop the screen
}
Read more »