Wednesday, December 31, 2014

Swapping of two numbers in C

Swapping of two numbers in c


#include <stdio.h>
#include <conio.h>
 
void main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x    = y;
   y    = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   getch();

}


Output:
Enter the value of x and y
9
7
Before Swapping x= 9
y= 7

After swapping x= 7
y=9

Swapping of two numbers without third variable

#include <stdio.h>
#include <conio.h> void main()
{
   int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   
getch();
}

Output:
Enter two integers to swap
9
7

a=7
b=9

Tuesday, December 30, 2014

C program to find factorial using Recursion

/*C program to find factorial using Recursion*/


#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}

C program to find largest number using if...else statement

/* C program to find largest number using if...else statement */


#include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf("Enter the values of a, b and c: ");
scanf("%d%d%d", &a, &b, &c);
if (a>b && a>c)
{
printf("A is largest number = %d",a);
}
if (b>a && b>c)
{
printf("B is largest number = %d",b);
}
if (c>a && c>b)
{
printf("C is largest number = %d",c);
}
getch();
}
                  OR
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, c;
printf("Enter a,b,c: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) 
{
printf("a is Greater than b and c");
}
else if (b > a && b > c) 
{
printf("b is Greater than a and c");
}
else if (c > a && c > b) 
{
printf("c is Greater than a and b");
}
else
{
printf("all are equal or any two values are equal");
}
getch();
}

Program Compares Two Strings to Check Whether They are equal or not



Program Compares Two Strings to Check Whether They are equal or not

#include <stdio.h>

#include <string.h>

int main( )

   {

    char a[20] ,  b[20];

    printf("n Enter the first string:");

    scanf("%s",a);

    printf("n Enter the second string:");

    scanf("%s",b);

    if((strcmp(a,b)==0))

      {

printf("nStrings are the same");
}
else
{
printf("nStrings are different");
}
return 0;
}

Write A Program (WAP) to Find Entered Number is Positive OR Negative


Write A Program (WAP) to Find Entered Number is Positive OR Negative

#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("n Enter a number:");
scanf("%d", &a);
if(a>0)

printf( "n The number %d is positive.",a);
}
else
{
printf("n The number %d is negative.",a);
}
getch();
}

Friday, December 26, 2014

Write a Program in C that Accept a Password



Write a Program in C that Accept a Password


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

char pw[25],ch;
int i;
void main()
{
clrscr();
puts("Enter your Password");
while(1)
{
if(i<0)
i=0;
ch=getch();
if(ch==13)
break;
if(ch==8)
{
 putch('b');
 putch(NULL);
putch('b');
-i;
continue;
}
pw[i++]=ch;
ch='*';
putch(ch);
}
printf("your passward is\n %s",pw);
getch();
}

Friday, December 19, 2014

Write a Program to Print Perfect Square of Digits


Write a Program to Print Perfect Square of Digits

#include<stdio.h>
#include<conio.h> 
void main()
{
int a[10][10]={0},i,j,low=0,top=9,n=1;
for(i=0;i<5;i++,low++,top--)
{
for(j=low;j<=top;j++,n++)
a[i][j]=n;
for(j=low+1;j<=top;j++,n++)
a[j][top]=n;
for(j=top-1;j>=low;j--,n++)
a[top][j]=n;
for(j=top-1;j>low;j--,n++)
a[j][low]=n;
}
printf("\t\t\t\tPerfect Square\n");
for(i=0;i<10;i++)
{
printf("\n\n\t");
for(j=0;j<10;j++)
{
printf("%6d",a[i][j]);
delay(300);
}
}

}

Output:

1   2   3   4   5   6   7   8   9   10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19

Write a Program to Print 12344321 123**321 12****21 1*******1

Write a Program to Print 12344321 123**321 12****21 1*******1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
for(i=4;i>=1;i--)
{
for(j=1;j<=4;j++)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
for(j=4;j>=1;j--)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
printf("\n");
}
}
Output:
12344321
123**321
12****21
1******1

Program to print pattern 12345 1234 123 12 1 in C


Program to print pattern 12345 1234 123 12 1 in C 

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


void main()
{
    int i, j;

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

}


Output:

12345
1234
123
12
1

Wednesday, December 17, 2014

Write a Program to Implement of Quick sort in C

Source code of simple quick sort implementation using array ascending order in c programming language

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
  int x[20],size,i;

  printf("Enter size of the array: ");
  scanf("%d",&size);

  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);

  quicksort(x,0,size-1);

  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);

  return 0;
}

void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;

     if(first<last){
         pivot=first;
         i=first;
         j=last;

         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }

         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);

    }
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

Thursday, December 11, 2014


//WAP TO FIND GREATEST AND SMALLEST ELEMENT IN THE ENTERED ARRAY 



#include<stdio.h>
int main(){
  int a[50],size,i,big,small;

  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
if(big<a[i])
 big=a[i];
  }
  printf("Largest element: %d\n",big);

  small=a[0];
  for(i=1;i<size;i++){
if(small>a[i])
 small=a[i];
  }
  printf("Smallest element: %d",small);

  return 0;
}

WAP TO CHANGE A STRING LOWER TO UPER CASE IN C language

//WAP TO CHANGE A  STRING  LOWER TO UPER CASE
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char str[25];
 int len;
 //clrscr();
 printf("Enter the String: ");
 gets(str);
 strupr(str);
 printf("\nYour String in Uppercase %s",str);
}

Wednesday, December 10, 2014

// WAP  TO COUNT NO. OF VOVELS AND CONSONENTS IN A ENTERED STRING


#include<stdio.h>
//#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int vovels=0,consonants=0,len,i;
printf("Enter the String:\n");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
 if((str[i]>64&&str[i]<91)||(str[i]>96&&str[i]<123)) //Firstly checking the character is alphabet or not
 {
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U') //checking if it is vowel or not
vovels++;
else
consonants++;
  }
}
printf("Number of vovels = %d and consonants = %d ", vovels , consonants);
//getch();
}

Tuesday, December 9, 2014

Data.Types in C

DATA TYPES IN C:

In the C programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
The types in C can be classified as follows:
S.N.Types and Description
1Basic Types:
They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
2Enumerated types:
They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
3The type void:
The type specifier void indicates that no value is available.
4Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
The array types and structure types are referred to collectively as the aggregate types. The type of a function specifies the type of the function's return value. We will see basic types in the following section, whereas, other types will be covered in the upcoming chapters.

Integer Types

Following table gives you details about standard integer types with its storage sizes and value ranges:
TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:
#include <stdio.h>
#include <limits.h>

int main()
{
printf("Storage size for int : %d \n", sizeof(int));

return 0;
}
When you compile and execute the above program it produces the following result on Linux:
Storage size for int : 4

Floating-Point Types

Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. Following example will print storage space taken by a float type and its range values:
#include <stdio.h>
#include <float.h>

int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );

return 0;
}
When you compile and execute the above program, it produces the following result on Linux:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6

The void Type

The void type specifies that no value is available. It is used in three kinds of situations:
S.N.Types and Description
1Function returns as void
There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);
2Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);
3Pointers to void 
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in the upcoming chapters.