30.12.20

C PROGRAM TO PERFORM ALL RELATIONAL OPERATORS AVAILABLE IN C

    #include<stdio.h>

int main()

{

int m,n;

printf("Enter any two values");

scanf("%d%d",&m,&n);

if(m<n)

{

printf("%d is greater than %d",m,n);

}

else if(m<n)

{

printf("%d is less than %d",m,n);

}

else if(m==n)

{

printf("%d is equal to %d",m,n);

}


return 0;

OUTPUT:

Enter any two values 40 20

40 is greater than 20


C PROGRAM TO CHECK THE GIVEN YEAR IS LEAP YEAR OR NOT

 

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int year;

printf("enter a year");

scanf("%d",&year);

if(year%400==0)

    {

    printf("%d is a leap year",year);

}

else if(year%100==0)

{

    printf("%d is not leap year",year);

    }

    else if(year%4==0)

    {

 

        printf("%d is a leap year",year);

    }

    else

    {

        printf("%d is not leap year");

    }

return 0;

}

OUTPUT

Enter a year 2032

2032 is a leap year

C PROGRAM TO PERFORM ALL BITWISE OPERATORS AVAILABLE IN C

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int a,b;

int c=0;

printf("Enter a and b value ");

scanf("%d%d",&a,&b);

c=a&b;

printf("value of c is %d\n",c);

 

c=a|b;

printf("value of c is %d\n",c);

c=a^b;

printf("value of c is %d\n",c);

c=a<<2;

printf("value of c is %d\n",c);

c=a>>2;

printf("value of c is %d\n",c);

    return 0;

}

OUTPUT

Enter a and b value 60 13

Value of c is 12

Value of c is 61

Value of c is 49

Value of c is 240

Value of c is 15

c program to perform all increment and decrement operators available in c

 

INCREMENT OPERATOR

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int i=1;

while(i<10)

{

    printf("%d",i);

    i++;

    }

    return 0;

}

OUTPUT

123456789

DECREMENT OPERATOR

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int i=20;

while(i>10)

{

    printf("%d",i);

    i--;

    }

    return 0;

}

OUTPUT

20191817161514131211

C PROGRAM TO PERFORM ALL ASSIGNMENT OPERATORS AVAILABLE in c

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

int a,c;

printf("enter a  value");

scanf("%d",&a);

c=a;

printf("value of c with operator = is %d\n",c);

c += a;

printf("value of c with operator += is %d\n",c);

c-=a;

printf("value of c with operator -= is %d\n",c);

c*=a;

printf("value of c with operator *= is %d\n",c);

c/=a;

printf("value of c with operator /= is %d\n",c);

c=200;

c%=a;

printf("value of c with operator %= is %d\n",c);

c<<=a;

printf("value of c with operator <<= is %d\n",c);

c>>=a;

printf("value of c with operator >>= is %d\n",c);

c&=a;

printf("value of c with operator &=  is %d\n",c);

c^=a;

printf("value of c with operator ^= is %d\n",c);

c|=a;

printf("value of c with operator |= = is %d\n",c);

 

    return 0;

}

OUTPUT

Enter a value 21

Value of c with operator = is 21

Value of c with operator += is 42

Value of c with operator -= is 21

Value of c with operator *= is 441

Value of c with operator /= is 21

Value of c with operator %= is 11

Value of c with operator <<= is 23068672

Value of c with operator >>= is 11

Value of c with operator &= is 1

Value of c with operator ^= is 20

Value of c with operator |= is 21

 


C PROGRAM TO PERFORM ALL LOGICAL OPERATORS AVAILABLE IN C

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int a,b,c;

printf("enter a and b values");

scanf("%d%d",&a,&b);

if(a&&b)

{

    printf("condition 1 is true ");

}

if(a||b)

{

    printf("condition 2 is true");

}

  return 0;

}

 

OUTPUT

Enter a and b values 0 10

Condition 2 is true

 


C PROGRAM TO PRINT SIZE ,MINIMUM VALUE,MAXIMUM VALUE AND PRECISION VALUE OF THE FLOAT USING SIZEOF(),FLT_MIN,FLT_MAX AND FLT_DIG.

 

#include <stdio.h>

#include <float.h>

 

int main()

{

    float a;

printf("the min value of float is %e\n",FLT_MIN);

printf("the max value of float is %e\n",FLT_MAX);

printf("the number of digits in the number is %e\n",FLT_DIG);

printf("the size of float is %lu\n",sizeof f(a));

 

    return 0;

}

OUTPUT

the min value of float is 1.175494e-38

the max value of float is 3.402823e+38

the number of digits in the number is 3.402823e+38

the size of float is 4

c program to the largest of three numbers

#include<stdio.h>

int main()

{

 int a,b,c;

printf("Enter three numbers");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

{

if(a>b)

printf("%d is largest number",a);

else

printf("%d is largest number",c);

}

else

{

if(b>c)

printf("%d is the largest number",b);

else

printf("%d is the largest number",c);

}

return 0;

}

OUTPUT

enter three numbers 10 20 30

30 is the largest number

c program to check the given is positive or negative number

#include<stdio.h>

int main()

{

 int a;

printf("Enter a number");

scanf("%d",&a);

if(a>0)

{

printf("Positive number");

}

else 

{

printf("Negative numbers");

}

return 0;

}

OUTPUT

Enter a number -10

Negative number 

c program to print the largest of two given numbers

#include<stdio.h>

int main()

{

int a,b;

printf("Enter two values);

scanf("%d%d",&a,&b);

if(a>b)

{

printf("%d is largest number",a);

}

else

{

printf("%d is largest number ",b);

}

return 0;

}

OUTPUT:

Enter two value 20 60

60 is largest number 

c program of all arthmetic operators available in c

#include<stdio.h>

int main()

{

 int a,b,add,sub,mul,div,mod;

printf("Enter a and b values):

scanf("%d%d",&a,&b);

add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

mod=a%b;

printf("Add= %d\n",add);

printf("Sub= %d\n",sub);

printf("Mul= %d\n",mul);

printf("Div= %d\n",div);

printf("Mod= %d\n",mod);

return 0;

}

OUTPUT

Enter a and b values 40 20

Add=60

Sub=20

Mul=800

Div=2

Mod=0

c program to convert fahrenheit to celsius and celsius to fahrenheit

 CELSIUS TO FAHRENHEIT:

#include<stdio.h>

int 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);

return 0;

}

OUTPUT

Enter temperature in centigrade 27.5

Equivalent fahrenheit scale value is 81.5

FAHRENHEIT TO CELSIUS 

#include<stdio.h>

int main()

{

float cen,fah;

printf("Enter temperature in fahrenheit scale:");

scanf("%f",&fah);

cen=(fah-32)/1.8;

printf("Equivalent centigrade scale value is %f",cen);

return 0;

}

OUTPUT

Enter temperature in fahrenheit scale 32

Equivalent centigrade scale value is 0

c program to print the area and perimeter of the reactangle

PROGRAM

#include<stdio.h>

int main()

{

 int l,b,area,perimeter;

printf("enter length and breadth of the rectangle");

scanf("%d%d",&l,&b);

area=l*b;

perimeter=2*(l+b);

printf("Area=%d,Perimeter =%d",area,perimeter");

return 0;

}

OUTPUT

enter length and breadth of the recatangle  3 2

Area=6,Perimeter=10

c program to print the size of the datatype using sizeof()

 SWAPPING WITHOUT USING THIRD VARIABLE

PROGRAM:
#include<stdio.h>
int main()
{
printf("size of char =%d\n",sizeof(char));
printf("size of short =%d\n",sizeof(short));
printf("size of int=%d\n",sizeof(int));
printf("size of long =%d\n",sizeof(long));
printf("size of float =%d\n",sizeof(float));
printf("size of double =%d\n",sizeof(double));

return 0;
}


OUTPUT:
size of char=1
size of short=2
size of int=4
size of long=4
size of float=4
size of double=8




c program to swap two numbers with and without using third variable

SWAPPING WITHOUT USING THIRD VARIABLE
PROGRAM:
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two values");
scanf("%d%d",&a,&b);
printf("Before Swap a=%d,b=%d",a,b);
{
a=a+b;
b=a-b;
a=a-b;
}
printf("After Swap a=%d,b=%d",a,b);
return 0;
}


OUTPUT:
Enter two values 30 5
Before Swap a=30, b=5
After Swap a=5,b=30



SWAPPING WITH USING THIRD VARIABLE
PROGRAM:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter two values");
scanf("%d%d",&a,&b);
printf("Before Swap a=%d,b=%d",a,b);
{
c=a;
a=b;
b=c;
}
printf("After Swap a=%d,b=%d",a,b);
return 0;
}


OUTPUT:
Enter two values 30 5
Before Swap a=30, b=5
After Swap a=5,b=30

26.6.20

c program dynamic memory management


DYNAMIC MEMORY MANAGEMENT
void main()
{
    int *a,n,i;

    printf("How many numbers do u want to enter ");
    scanf("%d",&n);

    a=(int*)malloc(n*sizeof(int));
    printf("Enter numbers:");
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    printf("the entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",*(a+i));
    free(a);
}
OUTPUT
 How many numbers do u want to enter 5
Enter numbers

78
956
324
8562
0159

Entered numbers are
78
956
324
8562
159

c program using arrays


USING ARRAYS:
void main()
{

    int a[100],n,i;

    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]);

    printf("The entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",a[i]);
}
OUTPUT:
How many numbers do u want to enter:4
Enter numbers:23
45
67
21
The entered numbers are
23
45
67
21

call by address example 2


CALL BY ADRESSS EXAMPLE 2
void changeMe(int*a)
{
    *a=72;
}
void main()
{
    int a=145;
    printf("Before :%d\n",a);
    changeMe(&a);
    printf("After : %d",a);
}
OUTPUT
Before:145
After:72

call by address /reference example 1


CALL BY ADDRESS/REFERENCE EXAMPLE 1:
void swap(int*a,int *b)
{

int t;
t=*a;
*a=*b;
*b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(&a,&b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping 10,20
After swapping 20,10

call by value


CALL  BY VALUE:
void swap(int a,int b)
{

int t;
t=a;
a=b;b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(a,b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping : 10,20
After swapping :10,20
Explanation :the a,b in main() and swap() functions are different....
So the changes made to a and b in the swap() function has no affect on a and b of main().

c program on arthmetic example 2


POINTER ARTHMETIC EXAMPLE 2
void main()
{
    int a=10,b=20;
    int*p=&a,*q=&b;
    printf("%u,%u\n",p,q);

    printf("%u\n",p+2);
    printf("%u\n",p-2);
   /* printf("%u\n",p*2);  */

    printf("%u\n",q-p);
    /*printf("%u\n",p+q); illegal */
}
OUTPUT
6356724,6356720
6356732
6356716
4294967295

c program for pointer arthmetic


POINTER ARTHMETIC
void main()
{
    int a=15;
    char ch='x';
    float f=9.7;

    int*p=&a;
    char*q=&ch;
    float*r=&f;

    printf("Before incremention:%d,%c,%f\n",a,ch,f);
    a++;
    ch++;
    f++;
    printf("after incremention:%d,%c,%f\n\n",a,ch,f);

    printf("Before incremention:%d,%c,%f\n",p,q,r);
    p++;
    q++;
    r++;
    printf("after incremention:%d,%c,%f\n",p,q,r);


}
Before incrementation: 15,x,9.7
After incrementation: 16,y,10.7
Before incrementation:100,200,300
After incrementation: 102,201,304

C program for the necessary of different pointers


WHY DIFFERENT TYPES FOR POINTERS
void main()
{

int a=4873;
char ch='p';
float f=2173.964;

int *p;
p=&a;
printf("%u\n",*p);

p=&ch;
printf("u\n",*p);

p=&f;
printf("%u",*p);
}
OUTPUT
4873
garbage
garbage                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

c program for address of and value at operators example 2


ADDRESS OF AND VALUE AT OPERATORS EXAMPLE 2
void main()
{
    int a=35;
    int*p;
    int**q;

    p=&a;
    q=&p;

    printf("%u\n",p);
    printf("%u\n",q);

    printf("u\n",*q);
    printf("u\n",*p);
    /* printf ("%u\n",*a); illegal */
    printf("%u\n",**q);
  /*  printf("%u\n",**p); illegal */
    printf("%u",*&p);

}
OUTPUT
6356728
6356724
u
u
35
6356728

C program for size of a pointer


SIZE OF A POINTER
void main()
{
    int a=10;
    char ch='x';
    float f=9.7;

    int *p=&a;
    char*q=&ch;
    float*r=&f;

    printf("%d,%d,%d\n",sizeof(a),sizeof(ch),sizeof(f));
    printf("%d,%d,%d",sizeof(p),sizeof(q),sizeof(r));

}
OUTPUT
4,1,4
2,2,2

c program for address of and value at operators


ADDRESS OF AND VALUE AT OPERATORS
void main()
{
    int a=35;
    int *p;
    p=&a;
    printf("%u\n",a);
    printf("%u\n",&a);
    printf("%u\n",p);
    printf("%u\n",&p);
    printf("%u\n",*p);
}
OUTPUT
35
6356732
6353732
6357328
35                                                                                         

c program for to print an address


HOW TO PRINT AN  ADDRESS
void main()
{
    int a=10;
    printf("%d\n",a);
    printf("%u",&a);

}
OUTPUT
10
6356732

19.6.20

c global variable


GLOBAL VARIABLE:
int i=78;
void fun1()
{

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

}
void fun2()
{

    int i= 45;
    printf("%d\n",i);
}
void main()
{

    printf("%d\n",i);
    fun1();
    fun2();
}
OUTPUT:
78
78
45

c register storage class specifier


REGISTER STORAGE CLASS SPECIFIER
void main()
{

    register int i;
    for(i=1;i<=5;i++)
        printf("I LOVE INDIA.\n");

}
OUTPUT
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.

c static storage class


STATIC STORAGE CLASS :
void fun()
{

    static int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
    fun();
}
OUTPUT:
10
11
12
13

c auto storage class


AUTO STORAGE CLASS
void fun()
{

    int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
}
OUTPUT
10
10
10

c macro: where we can access a variable


WHERE WE CAN ACCESS A VARIABLE...
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d,%d\n",a,b);

    }
 printf("%d,%d",a,b); /*error: b not accesible */
}
EXPALNATION:
A variable defined in a block can be accessed in that block and all its inner blocks (i.r., a variable defined in a block cannot be accessed in its outer block).It is called as the scope of a variable .

c macro :where to define a variable


WHERE TO DEFINE A VARIABLE
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d",b);
    }
}
OUTPUT:
10
20

c program enclosing the variable in the macro expansion


IMPORTANCE OF ENCLOSING THE VARIABLES IN THE MACRO EXPANSION WITHIN PARENTHESIS
#include<math.h>
#define MAC(x,y)sqrt(x*x+y*y)
float fun(int x,int y)
{
    return sqrt(x*x+y*y);

}
void main()
{

    int a=3,b=4;
    float p,q,r,s;
    p=fun(a,b);
    q=fun(a+1,b+1);
    r=MAC(a,b);
    s=MAC(a+1,b+1);
    printf("Using functions:%f,%f\n",p,q);
    printf("Using macros:%f,%f",r,s);
}
OUTPUT
Using functions: 5.0000000,6.403124
Using macros      5.0000000,4.000000

c program on name less blocks


PROGRAM ON NAMELESS BLOCKS
void main()
{

    int a=10;
    printf("%d\n",a);
    {

    printf("Stella Maris school -Marripalem");
    }
}
OUTPUT
10
Stella Maris School-Marripalem                                                                                                                           

c program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define PI 3.142857
#define AREA(x)PI*x*x
void main()
{

    float r,a;
    printf("Enter radius of a circle");
    scanf("%f",&r);
    a=AREA(r);
    printf("AREA of the circle=%f",a);
}

OUTPUT
Enter radius  of a circle 45
AREA of the circle6364.285645                                      

program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define SUM(a,b) a+b
void main()
{

    int a,b;
    printf("Enter two numbers");
    scanf("%d%d",&a,&b);
    printf("Sum=%d",SUM(a,b));

}
OUTPUT:
Enter two numbers:30 20
Sum=50

program on macros example 3


PROGRAM FOR MACROS EXAMPLE 3
#define PI 3.142857
void main()
{

    float r,a;
    printf("Enter radius of a circle:");
    scanf("%f",&r);
    a=PI*r*r;
    printf("Area of circle:%f",a);

}
OUTPUT:
Enter radius of circle: 3
Area of circle: 285713

program on macros example 2


PROGRAM FOR MACROS 2
 #define TRUE 1
void main()
{

    while(TRUE)
    {

    printf("This loop runs infinite times\n");
    }
}
OUTPUT
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times.....................

program on macros example 1


PROGRAM FOR MACROS EXAMPLE 1
#define NULL 0
void main()
{

    char st[20];
    int i;
    puts("Enter a string:");
    gets(st);
    for(i=0;st[i]!=NULL;i++);
    printf("length=%d",i);
}
OUTPUT:
Enter a string:
Sirisha
Length=7

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

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