9.4.20

string sorting


STRING SORTING
#include<stdio.h>
#include<strings.h>
int main()
{

    char st[10][30],t[30];
    int n,i,j,flag;
    printf("How many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

    fflush(stdin);
    gets(st[i]);

    }
for(i=0;i<n-1;i++)
{

    flag=1;
    for(j=0;j<n-1-i;j++)
{
    if(strcmp(st[j],st[j+1])>0)
    {

        strcpy(t,st[j]);
        strcpy(st[j],st[j+1]);
        strcpy(st[j+1],t);
        flag=0;
    }
}
if(flag==1)
    break;
}
printf("In alphabetical order\n");
for(i=0;i<n;i++)
    printf("%s\n",st[i]);
return 0;
}
OUTPUT
How many strings do u want to enter 4
Sirisha
Dali
Deepthi
Rama
In alphabetical order
Dali
Deepthi
Rama
Sirisha

finding the string which occurs first in alphabetical order without modifying the array


FINDING THE STRING WHICH OCCURS FIRST IN ALPHABTICAL ORDER WITHOUT MODIFYING THE ARRAY
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i;
    printf("how many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

        fflush(stdin);
        gets(st[i]);

    }
for(i=0;i<n;i++)
    if(i==0||strcmp(t,st[i]>0))2

strcpy(t,st[i]);
printf("The string which occurs first in alphabetical order is %s",t);
}
OUPUT
How many strings do u want to enter :1
Enter strings: abcde
The string which occurs first in alphabetical order is abcde

finding the longest string


FINDING THE LONGEST STRING
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i,l,max=0;
    printf("How many strings do u want ot enter:");
    scanf("%d",&n);
    printf("enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
        l=strlen(st[i]);
        if(l>max)
        {
            max=1;
            strcpy(t,st[i]);

        }
    }
    printf("Longest string is : %s",t);
    }
OUTPUT
How many strings do u want to enter 2
Enter strings: sirisha
Sanapathi
Longest string sanapathi

reading and printing a set of strings


READING /PRINTING A SET OF STRINGS
#include <stdio.h>
void main()
{

    char st[10][30];
    int n,i;
    printf("how many strings do u want to enter:");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
    }
printf("The entered strings are\n");
for(i=0;i<n;i++)
printf("%s\n",st[i]);
}
OUTPUT
How mant strings do u want to enter : 5
Enter strings:Dwarakanagar
MVP
Gajuwaka
Scindia
NAD
The entered strings are : Dwarakanagar
MVP
Gajuwaka
Scindia
NAD

transpose matrix


TRANSPOSE MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],t[5][5],i,j,r,c;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter the matrix:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        t[j][i]=m[i][j];
    printf("original matrix:\n");
    for(i=0;i<r;i++)
    {

        for(j=0;j<c;j++)
            printf("%5d",m[i][j]);
        printf("\n");

    }
printf("\n its transpose:\n");
for(i=0;i<c;i++)
{

    for(j=0;j<r;j++)
        printf("%5d",t[i][j]);
    printf("\n");
}
}
OUTPUT
How many rows and cols 2
2
Enter the matrix: 1
2
3
4
Orginal matrix:
1               2
2               4
Its transpose
1                 3
2                 4

product of two matrices


PRODUCT OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5]={0},r1,r2,c1,c2,i,j,k;
    printf("how many rows and cols for matrix 1");
    scanf("%d%d",&r1,&c1);
    printf("how many rows and cols for matrix 2");
    scanf("%d%d",&r2,&c2);
    if(c1!=r2)
    {

        printf("Multiplication rule violated -cannot be multiplied");
        exit(0);
    }

    printf("Enter elements into matrix 1:");
    for(i=0;i<r1;i++)
        for(j=0;j<c1;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r2;i++)
        for(j=0;j<c2;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r1;i++)
            {


            for(j=0;j<c2;j++)
            {
                for(k=0;k<c1;k++)
                    m3[i][j]=m3[i][j]+m1[i][j]*m2[i][j];
            }
}
        printf("Product of two matrices:\n");
        for(i=0;i<r1;i++)
        {

            for(j=0;j<c2;j++)
                printf("%5d",m3[i][j]);
            printf("\n");




        }

}
OUTPUT
How many rows and cols for matrix 1 2
2
How may rows and cols for matrix 2  2
2
Enter element into matrix 1:1
2
3
4

Enter element into matrix 2:1
2
3
4

Product of two matrices:
2         8
18    32

sum of two matrices


SUM OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5],r,c,i,j;
    printf("how many rows and cols ");
    scanf("%d%d",&r,&c);
    printf("Enter elements into matrix 1:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r;i++)
            for(j=0;j<c;j++)
            m3[i][j]=m1[i][j]+m2[i][j];
        printf("sum of two matrices:\n");
        for(i=0;i<r;i++)
        {

            for(j=0;j<c;j++)
                printf("%3d",m3[i][j]);
            printf("\n");



           
        }

}
OUTPUT
How many rows and cols 2
2
Enter elements into matrix 1:1
2
3
4
Enter elements into matrix 2:1
2
3
4
Sum of matrices:
2  4
6   8

sum of elements of a matrix


SUM OF ELEMENTS OF A MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],r,c,i,j,s=0;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter elements");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
    {
        scanf("%d",&m[i][j]);
        s=s+m[i][j];
    }
printf("sum of elements =%d",s);
    22
   
}
OUTPUT
How many rows and cols 2
4
Enter elements 1
2
3
4
5
6
7
8
Sum of elements 36

reading and printing a matrix


READING AND PRINTING A MATRIX
#include <stdio.h>
int main()
{

    int m[5][5],r,c,i,j;
    printf("How many rows and cols ");
    scanf("%d%d",&r,&c);
    printf("Enter elements");
    for(i=0;i<r;i++)
       for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);

    printf(" The entered elements are \n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
       printf("%3d",m[i][j]);
       printf("\n");

    }
return 0;
}
OUTPUT
How many rows and cols 2 3
Enter elements 1
1
3
5
6
7
The enetered elements are
113
567

finding number of words in a sentence example 2


FINDING NUMBER OF WORDS IN A SENTENCE EXAMPLE 2
#include <stdio.h>
void main()
{

    char st[128];
    int i,nwords=0;
    puts("enter a sentence:");
    gets(st);
    for(i=0;st[i]!=0;i++)
        if(st[i]==' ' && st[i+1]!=' ')
        nwords++;
    printf("number of words=%d",nwords+1);

}
Output:
Enter a sentence
Incredible india
Number of words =2

finding number of word in a sentence example 1


FINDING NUMBER OF WORDS IN A SENTENCE EXAMPLE 1
#include <stdio.h>
int main()
{
    char st[128];
    int i,nwords=0;
    puts("enter a sentence");
    gets(st);
    for(i=0;st[i]!=0;i++)
         if(st[i]=='')
        nwords++;
    printf("numbers of words=%d",nwords+1);
return 0;
}
OUTPUT
Enter a sentence
Incredible india
Number of words =2


string concatenation without using strcat()

STRING CONCATENATION WITHOUT USING STRCAT()
#include <stdio.h>
void main()
{

    char st1[30],st2[30],st3[60];
    int i,j;
    puts("enter a string 1");
    gets(st1);
    puts("enter a string 2");
    gets(st2);
    for(i=0;st1[i]!=0;i++)
        st3[i]=st1[i];
    st3[i]='*';
    j=i+1;
    for(i=0;st2[i]!=0;i++,j++)
        st3[j]=st2[i];
    st3[j]=0;
    printf("%s\n%s\n%s",st1,st2,st3);

}
OUTPUT
Enter a string 1 siri
Enter a string 2 sha
Siri
Sha
Siri*sha

string concatenation wihtout using strcat()


STRING CONCATENATION WITHOUT USING STRCAT()
#include <stdio.h>
int main()
{
    char st1[30],st2[30];
    int l1,l2,i;
    puts("enter string 1:");
    gets(st1);
    puts("enter string 2:");
    gets(st2);
    for(l1=0;st1[l1]!=0;l1++)
        for(l2=0;st1[l2]!=0;l2++)
    if(l1!=l2)
    {

        printf("not same");
        exit(0);

    }
    for(i=0;i<l1;i++)
        if(st1[i]!=st2[i])
        break;
    if(i==l1)
        printf("same");
    else
        printf("not same");
return 0;
}
OUTPUT
               Enter a string 1: siri
Enter a string 2: sana
Not same


string reverse without using strrev()


STRING REVERSE WITHOUT USING STRREV()
#include <stdio.h>
void main()
{

char st1[30],st2[30];
int l,i,j;
puts("enter a string");
gets(st1);
for(l=0;st1[l]!=0;l++)
j=l-1;
for(i=0;i<l;i++,j--)
    st2[i]=st1[j];
st2[i]=0;
printf("original : %s\nReverse:%s",st1,st2);
}
OUTPUT
Enter a string sirisha
Original sirisha
Reverse ahsiris

string copy without using strcpy()


STRING COPY WITHOUT USING STRCPY()
#include<stdio.h>
#include<stdlib.h>

int main()
{
    char st1[30],st2[30];
    int i;
    puts("Enter a string");
    gets(st1);
    for(i=0;st1[i]!='\0';i++)
        st2[i]=st1[i];
    st2[i]='\0';
    printf("%s\n%s",st1,st2);
return 0;
}
OUTPUT
Enter a string sirisha
Sirisha
Sirisha

string length wihtout using strlen()


STRING LENGTH WITHOUT USING STRLEN()
#include <stdio.h>
void main()
{

    char st[30];
    int i;
    puts("Enter a string");
    gets(st);
    for(i=0;st[i]!='\0';i++);sirisha
    printf("Length %d",i);

}
OUTPUT
Enter a string :sirisha sanapathi
Length:17

string concatenation


STRING CONCATENATION
#include <stdio.h>
void main()
{

    char st1[30],st2[30],st3[60];
    int n;
    printf("enter atring 1:");
    gets(st1);
    printf("enter string 2");
    gets(st2);
    strcpy(st3,st2);
strcat(st3,st2);
    printf(" %s\n%s\n%s",st1,st2,st3);
}
OUTPUT
Enter string1: shiva
Enter string 2: rama
Shiva
Rama
Shivarama          


string comparison


STRING COMPARISON
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    int n;
    printf("enter atring 1:");
    gets(st1);
    printf("enter string 2");
    gets(st2);
n=strcmp(st1,st2);
if(n==0)
    printf("same");
else
    printf("not same");
}
OUTPUT:
Enter string 1: sachin
Enter string 2:SAchin
Not same

string reverse


STRING REVERSE
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    printf("enter a string");
    gets(st1);
    strcpy(st2,st1);
    strrev(st2);
    printf("orginal:%s\nReverse:%s",st1,st2);
}
OUTPUT:
Enter a string: Visakhapatnam
Original:Visakhapatnam
Reverse :mantapahkasiv

string copy


STRING COPY
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    printf("enter a string ");
    gets(st1);
    strcpy(st2,st1);
    printf("%s\n%s",st1,st2);in
}
OUTPUT
Enter a string : india
India
India

finding a string length


FINDING A STRING LENGTH
#include <stdio.h>
void main()
{
    char st[30];
    int l;
    puts("enter a string");
    gets(st);
    l=strlen(st);
    printf("Length=%d",l);
}
OUTPUT
Enter a string sirisha sanapathi
Length =16

reading multi word names using gets()


READING MULTI-WORD NAMES USING gets()
#include <stdio.h>
void main()
{

    char na[30];
    printf("enter a name");
    gets(na);
    printf("%s",na);
}
OUTPUT
Enter a name sirisha sanapathi
Sirisha sanapathi

reading and printing a group of characters (method 1)(fflush())


READING/PRINTING A GROUP OF CHARACTERS (METHOD 1)(fflush())
#include <stdio.h>
void main()
{

    char st[30];
    int n,i;
    printf("How many characters do u want to enter");
    scanf("%d",&n);
    printf("enter the characters(name):");
    fflush(stdin);
    for(i=0;i<n;i++)
        scanf("%c",&st[i]);
    printf("the entered name is :");
    for(i=0;i<n;i++)
        printf("%c",st[i]);
}
OUTPUT
How many characters do u want to enter 13
Enter the character(name):incredible india
The entered name is :incredible in
READING/PRINTING A GROUP OF CHARACTERS (METHOD 2)
#include <stdio.h>
void main()
{

    char st[30];
    printf("enter the characters(name)");
    scanf("%s",st);
    printf(" the entered name is %s",st);

}
OUTPUT
Enter the characters(name) sirisha sanapathi
The entered name is sirisha

array elements in ascending order


Ascending order array elements
#include <stdio.h>
void main()
{

    int a[10],n,i,j,t,flag;
    printf("How many numbers do u want to enter");
    scanf("%d",&n);
    printf("enter the numbers");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)
    {

        flag=1;
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
                flag=0;

            }
        }
        if(flag==1)
            break;

    } printf("in ascending order\n");
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
}
Output
How many numbers do u want enter 5
Enter the numbers:2
8
6
3
1
In ascending order
1
2
3
6
8


results sheet


Results sheet:
#include <stdio.h>
#include <stdlib.h>

void main()
{

    int rno[10],marks[10],n,i;
    printf("How many students details do u want to enter ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter id and marks:");
        scanf("%d%d",&rno[i],&marks[i]);
    }
printf("Results sheet\n");
printf("--------------\n\n");
printf("First class \n");
for(i=0;i<n;i++)
    if(marks[i]>=60)
    printf("%d->%d\n",rno[i],marks[i]);
printf("Second class\n");
for(i=0;i<n;i++)
    if(marks[i]>=50&&marks[i]<60)
    printf("%d->%d\n",rno[i],marks[i]);
printf("Third  class\n");
for(i=0;i<n;i++)
if(marks[i]>=35&&marks[i]<50)
    printf("%d->%d\n",rno[i],marks[i]);

}
Output:
How many students details do u want to enter 2
Enter id and marks: 1
36
Enter id and marks:2
56
Results sheet
-----------------
First class
Second class
2->56
Third class
1->35

Sum of array


Sum of array :
#include <stdio.h>
void main()
{

    int a[10],n,i,s=0;
    printf("How many numbers do u want to enter");
    scanf("%d",&n);
    printf("enter numbers");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        s=s+a[i];
    }
    printf("sum =%d",s);
}
Output:
How many numbers do u want to enter 5
20
10
50
22
90
Sum=192

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


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