Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Wednesday, February 18, 2015
Arrays in C Part 3
Read: Arrays in C - Part 2
So far I told you about some basic topics of arrays in C like initialization of arrays, accepting elements in array, printing the values from an array and so on. Today I will tell you about one advance use of arrays like passing the values of arrays to a function. It can be done in two ways, call by value and call by reference.
Output

Explanation
Explanation
Which one is better?
As you can see both the approaches are giving the same results. However I need to do little modifications in each program. Remember that the second program is better than the first one. Because it is using the pointer. Usage of pointer decreases the execution time of program. This is the reason people use to prefer pointers while using arrays in C.
So far I told you about some basic topics of arrays in C like initialization of arrays, accepting elements in array, printing the values from an array and so on. Today I will tell you about one advance use of arrays like passing the values of arrays to a function. It can be done in two ways, call by value and call by reference.
Passing Array Elements to a Function
Call by Value
Lets straight away starts with one program.#include<stdio.h>
void printarr(int);
int main()
{
int x;
int nums[]={43,54,64,56,65};
for(x=0;x<5;x++)
printarr(nums[x]);
return 0;
}
void printarr(int n)
{
printf ("
%d",n) ;
}
Output

- I have declared an integer array nums and inserted 10 elements into it.
- After that I have started one for loop. Inside the loop I have written one statement i.e. printarr(nums[x]). It will call the function printarr().
- At first the value of x is 0. Then the first element of nums array will be passed to the printarr() function.
- That value will be received in the formal argument n of the function. After that it will display the element on the screen with printf() function.
- Now the control again moves to the main() function. The printarr() function is again called and second element of array nums is passed to it. This process will continue until the loop stops executing. And at last the program will stop.
Call by Reference
Lets understand it with one program.#include<stdio.h>
void printarr(int*);
int main()
{
int x;
int nums[]={43,54,64,56,65};
for(x=0;x<5;x++)
printarr(&nums[x]);
return 0;
}
void printarr(int *n)
{
printf("
%d",*n) ;
}
Explanation
- The code of the program is almost similar to the previous one. But in this case we are passing the address of array elements to the printarr() function.
- Note that I have passed the address of array elements with the help of address of operator i.e. &.
- The address of the array element is received by an integer pointer variable n.
- And in last I have printed the value by using printf() function. The output will be same as previous program.
Which one is better?
As you can see both the approaches are giving the same results. However I need to do little modifications in each program. Remember that the second program is better than the first one. Because it is using the pointer. Usage of pointer decreases the execution time of program. This is the reason people use to prefer pointers while using arrays in C.
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();
}
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();
}
#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();
}
Top 5 Best C C IDEs
An integrated development environment (IDE) or interactive development environment is software that provides facilities to programmers for software development. An IDE consists of a source code editor and compiler. Working in IDE really make our programming more interesting and easy because it has many interesting features like syntax highlighting, code completion, tabbed interface, smart indent and many more.

In this article I am sharing the list of top 5 best C/C++ IDEs that are very popular among programmers and available on the internet for free.
Also Read: 15 Common Errors in C and C++ Programming
Also Read: 15 Common Errors in C and C++ Programming
List of Top 5 Best C/C++ IDEs
Eclipse

Eclipse IDE is mainly famous for Java but its C/C++ IDE is also cool. It has awesome features like code highlighting, auto code completion and debugger. Eclipse IDE is available for Windows, Linux and Mac OS X.
Netbeans

Netbeans is another popular IDE that is widely used by programmers. It is also mainly famous for Java same as like Eclipse IDE. Netbeans has all the features that Eclipse has and it also offers some advance features. Netbeans IDE is available for Windows, Linux and Mac OS X.
Code::Blocks

Code::Blocks IDE is open source and cross platform IDE that is being widely used by programmers for C/C++ programing. It supports multiple compilers and consists of various features like syntax highlighting, code completion, tabbed interface and smart indent.
Dev-C++

Dev-C++ is a fast IDE that can work in a computer with low configurations (Windows 95 or higher, minimum 32 MB of RAM). Some of the features of Dev-C++ are, support GCC-based compilers, integrated debugging, class Browser, code completion, debug variable browser and many more. It is available for only Windows.
CodeLite

CodeLite is an open source, free and cross platform IDE for C/C++ programming which runs on all major Platforms (Mac OS X, Windows and Linux). Some of the features of CodeLite IDE are, refactoring, code completion, code navigation, built-in SFTP.
Please do share if you liked this article!
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]!=
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();
}
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();
}
Monday, February 16, 2015
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]!=
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();
}
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
}
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
}
Friday, February 13, 2015
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();
}
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;
}
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();
}
#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();
}
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/
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))#include<conio.h>
void main()
{
char ch;
clrscr(); //to clear the screen
printf("Enter any character:");
scanf("%c",&ch);
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
}
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
}
Subscribe to:
Posts (Atom)