Friday, 28 October 2011

User defined Function in C

/* There are 4 types of functions a programmer can make
1: No Arguments No Returning type function
2: No Arguments but Returning type fuction
3: Argument but  No Returning type function
4: Arguments and Returning types function

A simple program to understand the No Argument and No Returning type function.....
1: No Arguments No Returning Type function  */



#include<stdio.h>
#include<conio.h>
void sum();      /* Function declaration  part and type of the function is void i.e. no returning type */
void main()
{
clrscr();
sum();   // Calling of the function  //
getch();
}


// Definition of the function //
void sum()
{
int a,b,s=0;
printf("Enter the numbers: ");
scanf("%d %d",&a,&b);
s=a+b;
printf("The sum of the numbers= %d",s);
}

Saturday, 15 October 2011

Strong number in C


/* Prgram to generate strong numbers. */


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,r,s=0,f=1,j=0,p;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(p=1;p<=n;p++)
{
s=0;
   for(i=p;i>0;i=i/10)
   {
r=i%10;
f=1;
for(j=r;j>0;j--)
{
f=f*j;
}
s=s+f;
   }
if(s==p)
printf(" %d ",p);

}
getch();
}


Explanation: A strong no. (n) is a number which is obtained by the sum of factorial of all digit of that no.(n) and equal to the original no.(n). Eg. 145=1!+4!+5!=145 or 1=1!

Thursday, 13 October 2011

program of Greatest number among 3 in c


Method 1:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three numbers :");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is greatest.",a);
else
printf("%d is greatest.",c);


else if(b>c)
printf("%d is greatest .",b);
else
printf("%d is greatest.");
getch();
}




Method 2:



#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three numbers :");
scanf("%d %d %d",&a,&b,&c);
if(a>b?(a>c?printf("%d is greatest",a):printf("%d is greatest",c)):(b>c?printf("%d is greatest",b):printf("%d is greatest",c)))
getch();
}




Explanation:Method 2:
if(conidition?TRUE:FALSE)
we can place another condition at TRUE.


Pattern in C

Pattern 4:


 * * * * *
  * * * *
    * * *
     * *
      *
     * * 
   * * *
  * * * * 
 * * * * *





#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,m,s=0,n,p;
clrscr();
printf("Enter the value of n= ");
scanf("%d",&n);
for(x=0;x<n;x++)
{
     for(s=x;s>0;s--)
    {
         printf(" ");
     }
      for(y=x;y<n;y++)
     {
           printf("* ");
      }
    printf("\n");

 m=n-1;;
 p=1;
for(y=x;y<(n*2);y++)
{
          for(s=m;s>=1;s--)
          printf(" ");
    for(x=1;x<=p;x++)
   {
         printf("* ");
    }
 m--;
 p++;
   printf("\n");
 }


 getch();
 }

Tuesday, 11 October 2011

Array in C


/* Array: Suppose if we have to input 10 integer type of data then
10 separate integer variable have to declare before we use or input them.
But what will be the scene if the number of input increase upto 100 or 200
or more. The ARRAY reduces the complexity of the above problem.
An array is collection of similar types of data to be stored in memory and
which allows the different operations on the element of the array.
Array is stored in contiguous block of memory and it works on index.
Let see a simple program which implement declaration and input of array.
A program to input 10 integer types of data and to print the total. */


#include<stdio.h>
#include<conio.h>
void main()
{
int n[10],i=0,sum=0;
clrscr();
printf("Enter the elements of array: ");
for(i=0;i<10;i++)               //loop to input 10 integers//
scanf("%d",&n[i]);             //&n[i] means element is saved at (i)th position of the array
for(i=0;i<10;i++)
{
printf(" %d ",n[i]);
sum=sum+n[i];
}
printf("\nTotal is = %d ",sum);
getch();
}

Print the Grade of a student using Switch...Case in C language.

/* A prgram to print the grade of a student accoring to the %age of marks he/she obtained. The condotions are as follows:
if %age is between 1 to 30 then Fail
30 to 40 then E
40 to 50 then D

50 to 60 then C
60 to 70 then B
70 to 80 then B+
80 to 90 then  A
90 to 100 then A+ */




#include<stdio.h>
#include<conio.h>
void main()
{
int m1,total,m2,m3,m4,m5,total=0,avg=0;
char ch;
clrscr();
m:
printf("Enter the marks in subjects : ");
scanf(" %d %d %d %d %d ",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
printf("\nTotal is =%d",total);
avg=(m1+m2+m3+m4+m5)/5;
switch(avg/10)
{
case 1:
case 2:
{   printf(" Fail ");
          break;
}
case 3:
{    printf("Grade  E");
   break;
}
case 4:
{
   printf("GRade  D");
   break;
}
case 5:
{
 printf("Grade  C");
 break;
}
case 6:
{
 printf("Grade  B");
 break;
}
case 7:
{
 printf("Grade  B+");
 break;
}
case 8:
{
 printf("Grade  A");
 break;
}
case 9:
case 10:
{
 printf("Grade  A+");
 break;
}
default:
   printf("Input marks of atleast one subject is invalid ");
           printf("\n\nWant to input again y/n ?");
           ch=getch();
           if((ch=='y') || (ch=='Y') || (ch==13))
           goto m; 
}
getch();
}


Note: Program seems to be very simple as it is. But what will happen if we do not write the code in switch..." avg/10"
As the program execute avg will give two digit number and if write switch(avg) as CONDITIONAL OPERATIONS ARE NOT VALID IN SWITCH. IT ALLOWS ONLY INTEGER OR CHARACTER TYPES OF DATA  then we will have to write 

case 1:
case 2:
case3:
.
.
.
.upto 
case 29:

printf("Fail");
similarly for Grade E
case 30:
case 31:
.
.
.
upto case 39: 
{
printf("Grade E");
program become very long.... thats why in switch statement there is applied a code "avg/10" which gives an integer between 1 to 10 as avg is declared int type of data.

Monday, 10 October 2011

GCD of two numbers in C


Method 1:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,r=0,LCM,m,n;
clrscr();
printf("Enter the two numbers : ");
scanf("%d %d",&a,&b);
m=a;
n=b;
/*if(a>b)
{
c=b;
b=a;
a=c;
}
else
goto loop;
loop:
*/
do
{
r=b%a;
d=a;
a=r;
b=d;
}while(r!=0);
LCM=(m*n)/d;
printf("\n\nLCM= %d",LCM);
printf("\n\nGCD is= %d",d);
getch();
}


Method 2:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,i=0;
clrscr();
printf("Enter the two numbers :");
scanf("%d %d",&a,&b);
if(a<b)
for(i=a;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
printf(" %d ",i);
break;
}
}
if(b<a)
for(i=0;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
printf(" %d ",i);
break;
}
}
getch();
}

Method 3:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two numbers: ");
scanf("%d %d",&a,&b);
m:
if(b==0)
{
printf("gcd is=%d",a);
goto n;
}
if(a>b)
{
a=a-b;
goto m;
}
else
{
b=b-a;
goto m;
}
n:
getch();
}



Method 4:
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b)
{
  return (b==0)?a:gcd(b,a%b);
}
void main()
{
  int a,b;
  clrscr();
  printf("Enter the two numbers: ");
  scanf("%d %d",&a,&b);
  printf("gcd= %d ",gcd(a,b));
  getch();
}

Saturday, 8 October 2011

ASCII in C

//ASCII : American Standard Code for Information Interchange//




#include<stdio.h>
#include<conio.h>
void main()
{
int a=0;
clrscr();
while(a<=255)
{
printf(" %d->%c ",a,a);
a++;
}
getch();
}




Note: Observe the all ASCII and their symbol carefully after executing the program. You will be very happy to know about ASCII 9 10 11 12.

Thursday, 6 October 2011

Vowel in C

/* Simple program to check the entered charcter is vowel or not*/



#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter the character: ");
scanf("%c",&ch);
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
printf("Entered charcter is a vowel.");
else
printf("Entered character is not a vowel.");
getch();
}

Diamond in C


/* A Diamond in C
     *
    * *
   * * *
  * * * *
 * * * * *               */


 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int s=5;x,y;
 clrscr();
 for(x=1;x<=5;x++)
 {
 for(s=5;s>=x;s--)
 printf(" ");
 for(y=1;y<=x;y++)
 {
 printf(" *");
 }
 printf("\n");
 }
 getch();
 }


Note: There is a space in second last printf("_*");

Wednesday, 5 October 2011

Patterns in C language



/* Pattern 6:


1 2 3 4 5
   2 3 4 5
      3 4 5
         4 5
           5           */






#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,s=0;
clrscr();
printf(" \n\nPattern 6:\n\n");
for(x=1;x<=5;x++)
{
  s=0;
     do
     {
      printf(" ");
      s++;
      }while(s<x);
  for(y=x;y<=5;y++)
     {
      printf("%d",y);
      }
 printf("\n");
 }
getch();
}






/* Now print the following pattern for your own practice.....


        1
      21
    321
  4321
54321     easy code .....*/

Patterns in C language



/* Pattern 3:
0
1 0
0 1 0
1 0 1 0
0 1 0 1 0     */


#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
for(x=1;x<=5;x++)
{
for(y=x;y>=1;y--)
{
if(y%2==1)
printf("0 ");
else
printf("1 ");
}
printf("\n");
}
getch();
}






//Now one can try to print the following patterns
/* Pattern 4:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1






Pattern 5:   FLOYD'S TRIANGLE
1
2  3
4  5  6
7  8  9  10
11 12 13 14 15






*/

patterns in C language


/*   Pattern 1:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5*/




#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\nPattern 1:\n\n");
for(x=1;x<=5;x++)
{
for(y=1;y<=x;y++)
{
printf("%d ",y);
}
printf("\n");
}
getch();
printf("\n\n\n\n\nPattern 2:\n\n");




/* Pattern 2:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1    */


for(x=1;x<=5;x++)
{
for(y=x;y>=1;y--)
{
printf("%d ",y);
}
printf("\n");
}
getch();
}