You are on page 1of 21

Area of Circle

/*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/


#include<stdio.h>
main()
{
float radius,area,circum;
printf(\nEnter the radius of the circle);
scanf(%f,&radius);
area=3.14*radius*radius;
circum=2*3.14*radius;
printf(\nArea=%f,area);
//printf(\nCircumference=%f,circum);
}
OUTPUT:
Enter the radius of the circle
7
Area=153.86

Temperature Conversion
LOGIN: Balaji
/*TEMPERATURE CONVERSION*/
#include<stdio.h>
main()
{
float celcius,fahren,c,f;
clrscr();
printf(\n Tempterature Conversion);
printf(\nEnter the fahrenheit value:);
scanf(%f,&f);
celcius=(9.0/5.0)*(f-32);
printf(Celcius=%d,celcius);
printf(\nEnter the celcius value:);
scanf(%f,&c);
fahren=(9.0/5.0)*c+32;
printf(Fahrenheit=%d,fahren);
getch();
}
OUTPUT:

Enter the fahrenheit value:5


Celsius=-1.5
Enter the celcius value:14
Fahrenheit=50.00

Evaluate the given Expression


LOGIN: SURESH
/* Expression Evaluation*/
#include<stdio.h>
main()
{
int a,b,c;
float x,y,z;
printf ("Enter the values for a,b,c \n");
scanf("%d,%d,%d", &a,&b,&c);
x = (a * b) c;
y = (b/c) * a;
z = (a - b) / (c + d)
printf(" The value of x is ,x the value of y is ,y The value of z is ,z );
}
OUTPUT:
Enter the value for a,b,c.
a= 5; b = 6; c= 12;
x = 28.000;
y = 2.5000
z = 0.0555

Find the Given year is Lear Year or Not


#include<stdio.h>
main()
{
int ye;
printf ("Enter the year \n");
scanf("%d", &ye);
if (ye%4==0)
printf("It is a Leap Year \n");
else
printf("It is Not a Leap Year\n");
}

OUTPUT:
Enter the year 2000
It is a Leap Year
Enter the year 1995
It is Not a Leap Year

Largest of three numbers


/*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(Biggest of three Nos);
printf(Enter the values of A,B,C);
scanf(%d%d%d,&a,&b,&c);
if((a>b)&&(b>c))
printf(\n a=%d is greatest,a);
elseif(b>c)
{
printf(\n b=%d is greatest,b);
}
else
{
printf(\n c=%d is greatest,c);
}
getch();
}
OUTPUT:
Enter the values of A,B,C:50 90 120
C=120 is greatest

Age-Height Evaluation Program


#include<stdio.h>
#include<conio.h>
main()
{

int age,height;
clrscr();
printf("\n Enter the age (in the range of 2 - 5 )and height ");
scanf("%d%d",&age,&height);
if(age<4)
{
if(height<55)
printf("\n the height is short");
else if((height >=55)&&(height < 75))
printf("\n the height is normal");
else
printf("\n the height is tall");
}
else
{
if(height<75)
printf("\n the height is short");
else if((height >=75)&&(height < 100))
printf("\n the height is normal");
else
printf("\n the height is tall");
}
getch();
}
OUTPUT:
Enter the age (in the range of 2 5) and height
2
65
the height is normal
Enter the age (in the range of 2 5) and height
5
120
the height is tall

Find the Given number is palindrome or Not


/* PROGRAM TO FIND THE SUM AND REVERSE OF THE GIVEN NUMBER*/
#include<stdio.h>
main()
{
unsigned long int a,num,sum=0,rnum=0,rem;
printf(\nEnter the number...);
scanf(%ld,&num);
a=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
rnum=rnum*10+rem;
num=num/10;
}
printf(\nThe sum of the digits of %ld is %ld\n,a,sum);
printf(\nThe reverse number of the %ld is %ld,a,rnum);
if(a==rnum)
printf(\nThe given number is a palindrome);
else
printf(\nThe given number is not a palindrome);
}
OUTPUT:
Enter the number...12321
The sum of the digits of 12321 is 9
The reverse number of the 12321 is 12321
The given number is a palindrome

Generate Armstrong Series between a Range


/* PROGRAM TO ARMSTRONG SERIES WITHN A RANGE*/
#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;
printf("Printing all Armstrong numbers between 1 and 500:\n\n");
number = 001;
while (number <= 500)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);
if (temp == number)
{
printf("\nAmstrong Number:%d", temp);

}
number++;
}
}
OUTPUT:
Printing all Armstrong numbers between 1 and 500
Amstrong Number:1
Amstrong Number:153
Amstrong Number:370
Amstrong Number:371

Multiplication of two matrices


// MULTPLICATION OF TWO MATRIX
#include<stdio.h>
main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
printf(\nEnter the Rows and Columns of A matrix...);
scanf(%d %d,&m,&n);
printf(\nEnter the Rows and Columns of B matrix...);
scanf(%d %d,&r,&s);
if(m!=r)
printf(\nThe matrix cannot multiplied);
else
{
printf(\nEnter the elements of A matrix);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(\t%d,&a[i][j]);
}
printf(\nEnter the elements of B matrix);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(\t%d,&b[i][j]);
}
printf(\nThe elements of A matrix);
for(i=0;i<m;i++)
{
printf(\n);
for(j=0;j<n;j++)
printf(\t%d,a[i][j]);
}
printf(\n The elements of B matrix);
for(i=0;i<m;i++)
{

printf(\n);
for(j=0;j<n;j++)
printf(\t%d,b[i][j]);
}
for(i=0;i<m;i++)
{
printf(\n);
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(The multiplication of two matrixes);
for(i=0;i<m;i++)
{
printf(\n);
for(j=0;j<n;j++)
printf(\t%d,c[i][j]);
}
}
OUTPUT
Enter the Rows and Columns of A matrix... 3 3
Enter the Rows and Columns of B matrix... 3 3
Enter the elements of A matrix 1 2 3 4 5 6 7 8 9
Enter the elements of B matrix 1 2 3 4 5 6 7 8 9
The elements of A matrix
1
2
3
4
5
6
7
8
9
The elements of B matrix
1
2
3
4
5
6
7
8
9
The multiplication of two matrixes
30
36
42
66
81
96
102
126
150

Arithmetic Opertions Using Switch Case


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;

char op;
clrscr();
printf("\n Arithemetic Operations using switch case");
printf("\nEnter the value of a and b");
scanf("%d%d",&a,&b);
printf("\nSelect the operation u want");
printf("\n+.Add\n-.Sub\n*.Mul");
printf("\n/.Div\n%.Mod");
printf("\nEnter ur choice");
scanf("%d",&op);
switch(op)
{
case +:
c=a+b;
printf("\nThe Sum is %d",c);
break;
case -:
c=a-b;
printf("\nThe difference is %d",c);
break;
case *:
c=a*b;
printf("\nThe product is %d",c);
break;
case /:
c=a/b;
printf("\nThe Quotient is %d",c);
break;
case %:
c=a%b;
printf("\nThe Reminder is %d",c);
break;
default:
printf("\nEnter the operator to perform Arithmetic operations");
break;
}
getch();
}
OUTPUT:
Arithemetic Operation
Enter the value of a and b 40 50
Select the operation u want
+.Add
-.Sub
*.Mul
/.Div
%.Mod
Enter ur choice*

The Difference is 2000

Generating Pascal triangle


//CONSTRUCT PASCAL TRIANGLE
#include<stdio.h>
main()
{
int noline,i,j,temp;
printf(Enter the number of lines to print);
scanf(%d,&noline);
for(i=1;i<=noline;i++)
{
for(j=1;j<=noline-i;j++)
printf( );
temp=i;
for(j=1;j<=i;j++)
printf(%4d,temp++);
temp=temp-2;
for(j=1;j<i;j++)
printf(%4d,temp-);
printf(\n\n);
}
printf(\n);
}
OUTPUT:
Enter the number of lines to print 5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

Employee Payroll Using Structure

#include<stdio.h>
#include<conio.h>
struct employ
{
char name[40];
int Emp_no;
float basic_sal;

float net_salary;
};
void main()
{
struct employ emp;
float hra,da,det;
clrscr();
printf("\nEmployee Details");
printf("\nEnter the emp name");
scanf("%s",emp.name);
printf("\nEnter the employee no");
scanf("%d",&emp.Emp_no);
printf("\nEnter the basic salary");
scanf("%f",&emp.basic_sal);
hra=((15*emp.basic_sal)/100);
da=((10*emp.basic_sal)/100);
det=((5*emp.basic_sal)/100);
emp.net_salary=emp.basic_sal+hra+da-det;
printf("\nEmployee name:%s",emp.name);
printf("\nEmployee no:%d",emp.Emp_no);
printf("\nEmployee Basic salary:%f",emp.basic_sal);
printf("\nHRA:%f",hra);
printf("\nDA:%f",da);
printf("\nDetection:%f",det);
printf("\nGross salary:%f",emp.net_salary);
getch();
}
OUTPUT:
Employee Details
Enter the employee name aaa
Enter the employee no1000
Enter the basic salary 5000
Employee name:aaa

Employee no:1000
Employee Basic salary:5000.000000
HRA:750.00000
DA:500.000000
Detection:250.000000
Gross salary:6500.000000

Find Student Marks using Union


//STUDENT RECORD USING POINTER AND UNION
#include<stdio.h>
main()
{
union student
{
char name[25];
char regno[25];
int avg;
char grade;
} stud[50],*pt;
int i,no;
printf(Enter the number of the students...);
scanf(%d,&no);
for(i=0;i<no;i++)
{
printf(\n student[%d] information:\n,i+1);
printf(Enter the name);
scanf(%s,stud[i].name);
printf(\nEnter the roll no of the student);
scanf(%s,stud[i].regno);
printf(\nEnter the average value of the student);
scanf(%d,&stud[i].avg);
}
pt=stud;
for(pt=stud;pt<stud+no;pt++)
{
if(pt->avg<30)
pt->grade=D;
else if(pt->avg<50)
pt->grade=C;
else if(pt->avg<70)
pt->grade=B;
else

pt->grade=A;
}
printf(\n);
printf(NAME REGISTER-NO AVERAGE GRADE\n);
for(pt=stud;pt<stud+no;pt++)
{
printf(%-20s%-10s,pt->name,pt->regno);
printf(%10d \t %c\n,pt->avg,pt->grade);
}
}
OUTPUT:
Enter the number of the students
3
student[1] information:
Enter the name MUNI
Enter the roll no of the student 100
Enter the average value of the student 95
student[2] information:
Enter the name LAK
Enter the roll no of the student 200
Enter the average value of the student 55
student[3] information:
Enter the name RAJA
Enter the roll no of the student 300
Enter the average value of the student 25
NAME REGISTER-NO
AVERAGE
MUNI 100
95
A
LKA 200
55
B
RAJA

300

25

GRADE

Swapping Two Numbers (without third Variable) CALL BY VALUE


#include<stdio.h>
#include<conio.h>
main()
{
int x1,x2;
void swap(int ,int);
clrscr();
printf("\n Enter the two numbers");
scanf("%d%d",&x1,&x2);
printf("\n Before swapping the values of x1 = %d and x2 = %d",x1,x2);
swap(x1,x2);
getch();
}
void swap(int x,int y)
{
x=x+y;
y=x-y;

x=x-y;
printf("\n After swapping the values of r1 = %d and r2 = %d",x,y);
}
OUTPUT:
Enter the two numbers
15 30
Before swapping the values of r1 =15 and r2 =30
After swapping the values of r1 =30 and r2 = 15

Swapping Two Numbers Call By Reference


#include<stdio.h>
#include<conio.h>
main()
{
int r1,r2;
void swap(int *,int *);
clrscr();
printf("\n Enter the two numbers");
scanf("%d%d",&r1,&r2);
printf("\n Before swapping the value of r1= %d and r2= %d",r1,r2);
swap(&r1,&r2);
printf("\n After swapping the value of r1= %d and r2= %d",r1,r2);
getch();
}
void swap(int *a,int *b)
{
int temp;
temp= *a;
*a = *b;
*b = temp;
}
OUTPUT:
Enter the two numbers
100
200
Before swapping the value of r1 = 100 and r2 = 200
After swapping the value of r1 = 200 and r2 = 100

Factorial Computation using recursive Function


#include<stdio.h>
main()

{
int num,a;
printf(Enter the number);
scanf(%d,&num);
a=recur(num);
printf(The factorial of the number %d is %d,num,a);
}
recur(int no)
{
int fact=1;
if(no==1)
return(1);
else
fact=no*recur(no-1);
}
OUTPUT:
Enter the number 5
The factorial of the number 5 is 120

Difference between Post Increment (n++) and Pre Increment (++n)


Post Increment (n++): It increases the value of variable by 1 after execution of the statement.
Pre Increment (++n): It increases the value of variable by 1 before execution of the statement.
Program

class Demo
{
public static void main(String
args[])
{
int n=10;
System.out.println(n);
System.out.println(n+
+);
System.out.println(n);
}
}

Program

class Demo

Output

10
10
11

Output

{
args[])

public static void main(String


{
int n=10;
System.out.println(n);
System.out.println(+

+n);
}

10
11
11

System.out.println(n);

Prime Number
Logic: Prime Number are divisible by itself only.
Not divisible by any
Number

Divisible by 2 ...no need to


check further

Divisible by 3 ...no need to


check further

7%2=1
7%3=1
7%4=3
7%5=2
7%6=1

8%2=0
8%3=
8%4=
8%5=
8%6=
8%7=

9%2=1
9%3=0
9%4
9%5
9%6
9%7
9%8

Numbers are not divisible by more than half of the number


No need to check upto 6
check upto 3 only

No need to check upto 7


check upto 4 only

No need to check upto 8


check upto 4 only

Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Prime
{
public static void main(String args[])
{
int n, i, res;
boolean flag=true;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter a No.");
n=scan.nextInt();
for(i=2;i<=n/2;i++)
{
res=n%i;
if(res==0)
{
flag=false;
break;
}
}
if(flag)
System.out.println(n + " is Prime
Number");
else

Please Enter a No.: 7


7 is Prime Number

System.out.println(n + " is not Prime

Number");
}
}

Fibonacci Series ( 1 1 2 3 5 8 13...)


Logic: Sum of previous two numbers will give us next number.
prev

next

sum

shifted to prev

shifted to next

13

13

...

13

...

...

prev will give you fibonacci series


Program

Output

class Fibonacci
{
public static void main(String args[])
{
int prev, next, sum, n;
prev=next=1
for(n=1;n<=10;n++)
{
System.out.println(prev);
sum=prev+next;
prev=next;
next=sum;
}
}
}

1
1
2
3
5
8
13
...

Sum of 1st 10 Natural Numbers


Logic: Sum of previous two numbers will give us next number.
sum

sum

10

10

15

sum+n

15

21

21

28

28

36

36

45

45

10

55

Program

Output

class Sum10
{
public static void main(String args[])
{
int n, sum=0;
for(n=1;n<=10;n++)
{
sum+=n;
//or sum=sum+n;
}
System.out.println(sum);
}
}

55

Sum of Square of 1st 10 Natural Numbers


Logic: Sum of previous two numbers will give us next number.
sum

n*n

sum

1*1

2*2

sum+n*n

3*3

14

14

4*4

30

30

5*5

55

55

6*6

91

91

7*7

140

140

8*8

204

204

9*9

285

285

10*10

385

Program

Output

class SumSq10
{
public static void main(String args[])
{
int n, sum=0;
for(n=1;n<=10;n++)
{
sum+=n;*n
//or sum=sum+n;*n
}
System.out.println(sum);
}

385

Factorial
Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1
prod

prod

prod*n
5

20

20

60

60

120

120

120

Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Factorial
{
public static void main(String args[])
{
int n, i, prod=1;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter a No.");
n=scan.nextInt();
for(i=n;i>=1;i--)
{
prod*=i;
//prod=prod*i;
}
System.out.println("Factorial of " + n + "
is " + prod);
}
}

Please Enter a No.: 5


Factorial of 5 is 120

Biggest of 3 Numbers using Logical Operators


Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Biggest3
{
public static void main(String args[])
{
int n1, n2, n3, big;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
System.out.println("Please Enter No 3: ");

Please Enter No 1: 5
Please Enter No 2: 23
Please Enter No 3: 14
Biggest No: 23

n3=scan.nextInt();
if(n1>n2 && n1>n3)
big=n1;
else if(n2>n1 && n2>n3)
big=n2;
else
big=n3;
System.out.println("Biggest No: " + big);
}

Biggest of 3 Numbers using Nested If


Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Biggest3
{
public static void main(String args[])
{
int n1, n2, n3, big;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
System.out.println("Please Enter No 3: ");
n3=scan.nextInt();
if(n1>n2)
{
if(n1>n3)
big=n1;
else
big=n3;
}
else
{
if(n2>n3)
big=n2;
else
big=n3;
}
System.out.println("Biggest No: " + big);
}
}

Please Enter No 1: 5
Please Enter No 2: 23
Please Enter No 3: 14
Biggest No: 23

Swap 2 Numbers using 3rd Variable


Logic:
n1

n2

temp

23

23

23

23

23

Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Swap
{
public static void main(String args[])
{
int n1, n2, temp;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
temp=n1;
n1=n2;
n2=temp;
System.out.println("First No: " + n1);
System.out.println("Second No: " + n2);

Please Enter No 1: 5
Please Enter No 2: 23
First No: 23
Second No: 5

}
}

Swap 2 Numbers without using 3rd Variable


Logic:
n1

n2

10

15

15

10

10

Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class Swap
{
public static void main(String args[])
{
int n1, n2, temp;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
n1=n1+n2;
n2=n1-n2;
n1=n1-n2

Please Enter No 1: 10
Please Enter No 2: 5
First No: 5
Second No: 10

System.out.println("First No: " + n1);


System.out.println("Second No: " + n2);
}
}

Sum of Digits
Logic: 513 -> 5+1+3=9
N

res

513

sum
0

513%10

513/10

3
51

51%10

51/10

4
5

5%10

5/10

4
9

Program

Output

//Note: Scanner class work with JDK1.5 or above


import java.util.*;
class SumDigits
{
public static void main(String args[])
{
int n, res;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No.: ");
n1=scan.nextInt();
while(n>0)
{
res=n%10;
n=n/10;
sum+=res;
//sum=sum+res;
}
System.out.println("Second No: " + n2);

Please Enter No 1: 10
Please Enter No 2: 5
First No: 5
Second No: 10

You might also like