Tuesday 8 March, 2011

Write a program to find maximum element from 1-Dimensional array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,i,max=0; // declared the array a with size 20
clrscr();
printf("\n Enter the number of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n Maximum element from above array inserted is : %d",max);
getch();
return 0;
}
Output:
Enter the number of elements for 1-D array : 5
Enter element [1] : 1
Enter element [2] : 8
Enter element [3] : 2
Enter element [4] : 5
Enter element [5] : 9
Maximum element from above array inserted is: 9

Write a program that prints the following Floyd’s triangle

1
2 3
4 5 6
7 8 9 10
11 ………..15
.
.
79 …………………91

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k;
clrscr();
printf("\n\n Enter the number : ");
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
{
printf(" %d ",k);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Print the following triangle

Print the following triangle.
a b c d e
  a b c d
    a b c
      a b
        a

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,j,k;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
{
printf(" ");
}
for(j=1;j<=n-i;j++)
{
printf(" %c",96+j);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
a b c d e
  a b c d
   a b c
    a b
     a

Write a program to find the roots of an equation ax2 + bx + c = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,alf,bt,dlt;
clrscr();
printf("\n Enter a: ");
scanf("%f",&a);
printf("\n Enter b: ");
scanf("%f",&b);
printf("\n Enter c: ");
scanf("%f",&c);
dlt=b*b-4*a*c;
if(dlt==0)
{
printf("\n ALPHA=BETA=%f",-b/(2*a));
}
else if(dlt<0)
{
printf("\n Imaginary Roots");
}
else
{
alf=(-b+sqrt(dlt))/(2*a);
bt=(-b-sqrt(dlt))/(2*a);
printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
}
getch();
return 0;
}
Output:
(1)
Enter a: 12
Enter b: 2
Enter c: 34
Imaginary Roots
(2)
Enter a: 2
Enter b: 6
Enter c: 2
Alpha = -0.381966
Beta = -2.618034
(3)
Enter a: 2
Enter b: 4
Enter c: 2
ALPHA=BETA= -1.000000

Write a program to generate Fibonacci series

#include<stdio.h>
#include<conio.h>
int main()
{
int n1=0,n2=1,n3=1,n,i;
clrscr();
printf("Enter the Number : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf(" %d ",n3);
n3=n1+n2;
n1=n2;
n2=n3;
}
getch();
return 0;
}
Output:
Enter the Number : 5
1 1 2 3 5

Print series 2, 4, 16,……,n*n using shorthand operator and while loop

#include<stdio.h>
#include<conio.h>
int main()
{
int n,x; // declare the variable
long double i=2; //declare the variable of long double type
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
x=1;
while(x<=n) // loop will be execute till the value of x I less or equal n
{
printf("%.2Lf\n",i); // print the value of I upto 2 decimals only
x++;
i*=i;
}
getch();
return 0;
}
Output:
Enter the number : 4
2.00
4.00
16.00
256.00

print 2,4,6,8,...,n

Print series 2, 4, 6, 8,…....,n.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n; // declare two integer type variables
clrscr(); // clears the output window
printf("\n Enter the number : ");
scanf("%d",&n); // input the value of n
printf(“\n”); // will break the line on output window
for(i=2;i<=n;i++)
{
if(i%2==0)
{
printf(" %d ",i);
}
}
getch();
return 0;
}
Output:
Enter the number : 14
2 4 6 8 10 12 14

prime or not

Write program to check whether the entered number is PRIME or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,flag=0; // declare variable i, n & flag and initialize flag with 0
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n %d is a Prime number",n);
}
else
{
printf("\n %d ia not a Prime number",n);
}
getch();
return 0;
}
Output:
Enter the number : 4
4 is not a Prime number
Enter Number : 7
7 is a Prime number

Write program to accept number of days and print year, month and remaining days

#include<stdio.h>
#include<conio.h>
int main()
{
int day,y,m; /*day for day input & output,
y for calculate year,
m for calculate month
printf("Enter the number of days : ");
scanf("%d",&day);
y=day/365; //calculate years
day%=365; // calculate remaining days
m=day/30; // calculate months
day%=30; // calculate remaining days
printf("%d years,%d months, %d days",y,m,day);
getch();
return 0;
}
Output:
Enter number of days : 1025
2 years,9 months, 25 days

Beginning with C:1st example

Write a program to convert Rupees (float) to paisa (int).
#include<stdio.h>
#include<conio.h>
int main()
{
float rs; //real variable for rupees
int ps; // integer variable for paisa
clrscr();
printf("\n\n Enter rupees to convert into paisa : ");
scanf("%f",&rs);
ps=rs*100;
printf("\n Paisa of given rupees is %d",ps);
getch();
return 0;
}
Output:
Enter rupees to convert into paisa : 98.52
Paisa of given rupees is 9852