25.12.19

array 1-d program in c

#include<stdio.h>

void main()
{

int a[10],n,i;

printf("How many numbers  do u want to enter");

scanf("%d",&n);

printf("enter elements ");
for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("enter numbers are \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);

}


OUTPUT

How many numbers  do u want to enter 3

enter elements 56 
78
 90
enter elements are 56
 78 
90

example for pyramid 7 program in c

#include<stdio.h>

void main()
{

int n,i,j,k;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=2*n-1;i++)
{
    k=(i<=n)?i:n-i%n;
     
  for(j=1;j<=k;j++)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5
      
1
12
123
12345
1234
123
12
1

24.12.19

example for pyramid 6 program in c

#include<stdio.h>

void main()
{

int n,i,j,k;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
    for(k=1;k<=n-i;k++)
         
          printf(" ");

  for(j=1;j<=2*i-1;j++)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5
         1
       123
     12345
   1234567
 123456789

Example for pyramid 5 program in c

#include<stdio.h>

void main()
{

int n,i,j,k;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
    for(k=1;k<=n-i;k++)
         
          printf(" ");

  for(j=1;j<=i;j++)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5
         1
       12
     123
   1234
 12345

Example for pyramid 4 program in c

#include<stdio.h>

void main()
{

int n,i,j;

printf("Enter a number");
scanf("%d",&n);

for(i=n;i>=1;i--)
{

  for(j=1;j>=i;j--)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5

54321
4321
321
21
1

example for pyramid 3 program in c


#include<stdio.h>

void main()
{

int n,i,j;

printf("Enter a number");
scanf("%d",&n);

for(i=n;i>=1;i--)
{

  for(j=1;j<=i;j++)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5

12345
1234
123
12
1

example for pyramid 2 program in c

#include<stdio.h>

void main()
{

int n,i,j;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=n;i++)
{

  for(j=1;j<=i;j++)
          
    printf("%d",i);

printf("\n");

}

}

OUTPUT:
Enter a number 5
1
22
333
4444
55555

example for Pyramid 1 program in c

#include<stdio.h>

void main()
{

int n,i,j;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=n;i++)
{

  for(j=1;j<=i;j++)
          
    printf("%d",j);

printf("\n");

}

}

OUTPUT:
Enter a number 5
1
12
123
1234
12345

Generating random numbers program in c

#include<stdio.h>

void main()
{

int i,r;

randomize();

for(i=1;i<=10;i++)
{

r=random(100)

printf("%d\t",r);
}

}

OUTPUT:

82 86 72 23 45 48 88 85 48 21



NOTE:

a)  random() takes a number and returns a random number in the range 0 to one less than the given                         number.
b)  randomize() intializes the random number generator with a random value.

example for switch case program in c

#include<stdio.h>

void main()
{
char ch;

printf("Enter an alphabet:");
scanf("%c",&ch);

switch(ch)
{

 case 'c' : printf("Chandigarh");
                   break;
case 'b' : printf("Baroda");
                   break;
case 'd' : printf("Dehradun");
                   break;
case 'v' : printf("Vizag");
                   break;
default: printf(" Guess where");

}

}

OUTPUT

Enter an alphabet c
Chandigarh

Logical operators(!!) program in c

#include<stdio.h>

void main()
{

char ch;

printf(" enter an alphabet");
scanf("%c",&ch);

if(ch=='a'!!ch=='e!!ch=='i'!!ch=='o'!!ch=='u')
printf("vowel");
else
printf("Consonant);

}

OUTPUT

enter an alphabet  o
vowel

conditional operator(? :) program in c

#include<stdio.h>

void main()
{

int m;

printf("Enter marks ");
scanf("%d",&m);

(m>=35)?printf("passed):printf("Failed");

}

OUTPUT:

Enter marks 56
passed


Prime or not program in c

#include<stdio.h>

void main()
{

int i,n,count=0;

printf("Enter a number");
scanf("%d",&n);

for(i=1;i<=n;i++)
   if(n%i==0)
          count++;

if(count==2)

printf("prime");

else

printf(" not prime");

}

OUTPUT

Enter a number 3
prime


Reverse of number program in c

#include<stdio.h>

void main()
{

int n,r,rev=0;

printf("Enter a number");
scanf("%d",&n);

while(n!=0)
{

r=n%10;

rev=rev*10+r;

n=n/10;

}

printf("Reverse %d",rev);

}


Enter a number 786
Reverse 687

Generates that many multipication tables program in c

#include<stdio.h>

void main()
{
int n,i,j;

printf(" Enter a number ");
scanf("%d",&n);

printf("\t");
for(i=1;i<=n;i++)
   printf("%5d",i);

printf("\n\n");

for(i=1;i<=5;i++)
{
     printf("%d\t",i);
for(j=1;j<=n;j++)
{
   printf("%5d",1*j);

}
printf("\n");

    }

}



OUTPUT

Enter a number 5

1 2 3 4 5

1            1       2          3           4        5
2            2       4          6           8       10
3            3       6          9          12      15
4            4       8          12        16      20        
5            5       10        15        20      25

example for nested loop program in c

#include<stdio.h>
{
int i,j;

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


OUTPUT
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
4,1
4,2
4,3
5,1
5,2
5,3

Generating multiplication table program in c

#include<stdio.h>

void main()
{
         int n,i;

printf("Enter a number");
scanf("%d",&n);

for(i=0;i<=20;i++)
    printf("%3d*%2d=%5d\n",n,i,n*i);

}

OUTPUT

9*1= 9
9*2= 18
9*1= 27
9*1= 36
9*1= 45
9*1= 54
9*1= 63
9*1= 72
9*1= 81
9*1= 90
9*1= 99
9*1= 108
9*1= 117
9*1= 126
9*1= 135
9*1= 144
9*1= 153
9*1= 162
9*1= 171
9*1= 180

To print all the even numbers (using for )b/w 1 to 10 program inc

#include<stdio.h>

void main()
{
int i;

for(i=0;i<=10;i++)
{

if(i%2==0)

printf("%d\t",i);

 }
}

OUTPUT

2 4 6  8 

Example of (multi way control structure) program in c

#include<stdio.h>

void main()
{

int m;

printf("Enter marks");
scanf("%d",&m);

if(m>=60)
printf("First class");

else if(m>=50)
printf("Second class");

else if(m>=35)
printf("Third class");

else
printf("Failed");

}

OUTPUT
Enter marks 46
Third class

Example of (if else) program in c

#include<stdio.h>

void main()
{

int n;

printf("Enter a number:");
scanf("%d",&n);

if(n%2==0)

printf("Even");

else

printf("Odd");

}

Example of (if) program in c

#include<stdio.h>

void main()
{

int n;

printf("Enter a number");
scanf("%d",&n);

printf("%d\n",n);

if(n==786)

printf("sanapathi");

}

OUTPUT:

Enter a number 786
sanapathi

Constant keyword program in c

If we want that the value of a variable is fixed & shold not be changed even accidentally, then such a variable is defined as const

#include<stdio.h>

void main()
{

const int a=512;

printf("%d",a);

}

OUTPUT:

512

sizeof keyword program in c

It is used to get the size of a variable or data type i.e., number of btes allocated.

#include<stdio.h>

void main()
{

int x;
char y;
float z;

printf("x=%d\n",sizeof(x));
printf("int =%d\n",sizeof(int));

printf("y=%d\n",sizeof(y));
printf("char =%d\n",sizeof(char));


printf("z=%d\n",sizeof(z));
printf("float =%d\n",sizeof(float));

}


OUTPUT:

x=2
int =2

y=1
char=1

z=4
float=4

Temperature value from centigrade to Fahrenheit prrogram in c

#include<stdio.h>
void main()
{
float cen,fah;
printf("Enter temperature in  centigrade scale:");
scanf("%f",&cen);

fah=9/5.0*cen+32;

printf("Equivalent Fahrenheit scale value is %f",fah);

}


OUTPUT:

Enter temperature in  centigrade scale: 27.5

Equivalent Fahrenheit scale value is 81.5

Palindrome of string program in c

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

void main()
{
char str1[30],str2[30];
int n;
clrscr();
printf("Enter a string");
gets(str1);
strcpy(str2,str2);
strrev(str1);
n=strcmp(str,str);
if(n==0)
printf("\n The given string is palindrome");
else
printf("\n The given string is not a palindrome");
getch();
}


OUTPUT:

Enter a string mom

The given string is palindrome

Enter a string mad

The given string is not a palindrome

23.12.19

FInding largest and smallest number from a list program in c

#include<stdio.h>

void main()

{

int a[50],i,n,large,small;

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

printf("Enter the elements into array to find largest and smallest number");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

large=small=a[0];

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

small=a[i];

}

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

}


OUTPUT

Enter the size of array 10
Enter the elements into array to find largest and smallest number 12  3  67  90  9  56  34  6  8 12
 The largest number is   90
The smallest number is 3

Swapping of Numbers by call-by-value program in c

#include<stdio.h>

void main()

{
int a=100,b=200;

void swap(int,int);

printf("Before swap a=%d, b=%d",a,b);

swap(a,b);

}

void swap(int m,int n)
{
int temp ;

temp=m;
m=n;
n=temp;

printf("After swap a=%d,b=%d",m,n);
}



OUTPUT:

Before swap a=100 and b=200
After swap a=200 and b=100

Swapping of numbers using call-by-reference program in c

#include<stdio.h>
 

void main()


{


  int a=10,b=50;


  void swap(int *,int *);


  clrscr();


  printf("Before Swapping: %d %d\n",a,b);


  swap(&a,&b);


  printf("After Swapping: %d %d\n",a,b);


}


void swap(int *a,int *b)


{
  int t;

  t=*a;


  *a=*b;


  *b=t;


}



Output :
Before Swapping: 10 50
After Swapping: 50 10

Factorial ( Recursion Method) program in c

#include<stdio.h>

#include<conio.h>

int fact(int n)
{

int f;

if(n==0)

return 1;
else

f=n*fact(n-1);

return f;
}




void main()
{
int n,f;

clrscr();

printf("\n Enter the value of n");

scanf("%d",&n);

f=fact(n);

printf("\n Factorial of %d is %d ",n.f);

getch();

}



OUTPUT
Enter the value of n 5
Factorial of 5 is 120


Bubble Sort program in c

#include<stdio.h>
void main()
{
 int n,c,d,swap,a[10];
 printf("Input number of integers to sort:\n");
 scanf("%d",&n);
 printf("Enter %d integers:\n",n);
 for(c=0;c<n;c++)
    scanf("%d",&a[c]);
 for(c=0;c<(n-1);c++)
 {
  for(d=0;d<n-c-1;d++)
     {
      if(a[d]>a[d+1]) /* For descending order use < */
        {
          swap = a[d];
          a[d] = a[d+1];
          a[d+1] = swap;
        }
      }
 }
    printf("Sorted list of numbers:\n");
    for(c=0;c<n;c++)
        printf("%d\n",a[c]);
}


Output:


Input number of integers to sort:
4
Enter 4 integers:
23
5
225
1
Sorted list of numbers:

1
5
23
225


Binary Search program in c

#include<stdio.h>
void main()
{
 int n,i,l=0,h,m=0,f=0,sk,a[10];
 printf("Enter number of elements: ");
 scanf("%d",&n);
 h=n-1;
 printf("Enter %d integers :\n",n);
 for(i=0;i<n;i++)

     scanf("%d",&a[i]);
 printf("Enter the search value: ");

 scanf("%d",&sk);
 while(l<=h)
      {
    m=(l+h)/2;
    if(sk==a[m])
    {
     f=1;break;
    }
    else if(sk<a[m])
         h=m-1;
    else
             l=m+1;
      }

 if(f==1)
   printf("%d Found at Index %d",sk,m);
 else
  printf("Search Key Not Found");
}

Output 2:

Enter number of elements: 4
Enter 4 integers :
12
34
56
78
Enter the search value: 78
78 Found at Index 3

Output 3:
Enter number of elements: 4

Enter 4 integers :
12
354
567
2345
Enter the search value: 5
Search Key Not Found

Linear Search program in c

#include<stdio.h>

void main()

{

int a[10],i,e,n;

printf("Enter the size of array");

scanf("%d",&n);

printf("enter elements");

for(i=0;i<n;i++) 

scanf("%d",&a[i]);

printf("enter the  elements to be search  ");

scanf("%d",&e);

for(i=0;i<n;i++)

{
if(a[i]==e)

printf("The element is found");

break;

if(i==n)
printf(" The element not found");

}
}


OUTPUT:

1. Enter the size of array 7
 enter elements 11
 21
 31
 41
 51
 61
 71
 enter the  elements to be search  51
 The element is found


2. Enter the size of array 7
 enter elements 11
 21
 31
 41
 51
 61
 71
 enter the  elements to be search  40
 The element is not found

String Operations program in c

#include <stdio.h>


#include <string.h>


void main()


{


    char str1[30],str2[30];


    int choice,result=0;


    int len1=0,len2=0;


    printf("\n Enter First String : ");


    scanf("%[^\n]",str1);


    printf("\n Enter Second String : ");


    scanf("%s",str2);


    printf("\n Enter Your Choice : \n");


    printf("1. Copy Strings. \n");


    printf("2. Compare Strings. \n");


    printf("3. Concatenate Strings. \n");


    printf("4. Find Lengths. \n");


    printf("\n Enter Your Choice :");


    scanf("%d",&choice);


    switch(choice)


    {


       case 1 :


         {
        strcpy(str1,str2);


        printf("\n %s %s \n",str1,str2);

        break;
         }


       case 2 :


         {


        result = strcmp(str2,str1);


        if(result == 0)


           printf("\n Both Strings are Same. ");


        else


           printf("\n Both are different strings.");




        break;


         }


       case 3 :


         {


        strcat(str1,str2);



        printf("\n %s %s \n",str1,str2);


        break;


         }


       case 4 :
         {


        len1 = strlen(str1);


        printf("\n Legth of First String is %d \n",len1);


        len2 = strlen(str2);


        printf("\n Legth of Second String is %d \n",len2);


        break;


         }


     default : printf("\n Wrong Choice .");


    }


   


}




Output1:
Enter First String : sana


Enter Second String : siri


 Enter Your Choice :

1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :1
siri siri



Output 2:



Enter First String : sana


Enter Second String : siri


Enter Your Choice :


1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
 Enter Your Choice :2


 Both are different strings.

Enter First String : sana
Enter Second String : sana


Enter Your Choice :
1. Copy Strings.

2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :2


Both Strings are Same.

Output 3:


Enter First String : siri

Enter Second String : sana


Enter Your Choice :


1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :3
sanasiri siri



 Note : strcat() function string in position1 will be concatinated with string in position2

Output 4:


Enter First String : sana


Enter Second String : deepu


Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :4


Legth of First String is 4
Legth of Second String is 5

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...