Wednesday 20 July 2016

ARRAY programming

1. WAP in c to input 5 elements in an array and print it.


#include<stdio.h>

#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("\n enter aray elements\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n the entered values are: ");
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
getch();
}


output:











2. WAP in c to find sum and average age of 10 students.


#include<stdio.h>
#include<conio.h>
main()
{
int age[10],i,s=0;
clrscr();
float av;
printf("\n enter age of 10 students\n");
for(i=0;i<10;i++)
{
scanf("%d",&age[i]);
}
for(i=0;i<10;i++)
{
s=s+age[i];
}
av=s/10.0;
printf("\ntotal age = %d\nAverage age = %f",s,av);
getch();
}




Output:













3. WAP in c to print all even numbers in an array.


#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i;              
printf("\n enter aray elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\nall even numbers are:\n");
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
printf(" %d",a[i]);
}
}
getch();
}





Output:













4. WAP in c to add two 1D array.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf("\n enter 5 elements in first array\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter 5 elements in second array\n");
for(i=0;i<5;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
}
printf("\nsum of two arrays are:\n");
for(i=0;i<5;i++)
{
printf(" %d",c[i]);
}
getch();
}





Output:













5. WAP in c to find largest and smallest element in an array.


#include<stdio.h>
#include<conio.h>
void main()
{
float a[10];
float max,min;
int i;
clrscr();
printf("\nenter 10 elements in an array:\n");
for(i=0;i<10;i++)
{
scanf("%f",&a[i]);
}
max=min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
  max=a[i];
if(a[i]<min)
  min=a[i];
}
printf("the minimum is %f and the maximum is %f",min,max);
getch();
}





Output:




6. LINEAR SEARCH

          OR

SEQUENTIAL SEARCH

          OR

WAP in C to search an element in an array.


#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,n,f=0;
clrscr();
printf("\n enter 5 element\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the element to be searched: ");
scanf(" %d",&n);
for(i=0;i<5;i++)
{
if(a[i]==n)
{
printf("\n elements found at location %d",i+1);
f=1;
break;
}
}
if(f==0)
{
printf("\nElement not found");
}
getch();
}

Output:











7. BINARY SEARCH

          OR

WAP in C to search an element in an array using BINARY SEARCH.

Note: In binary search data should be in sorted order.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20]={3,5,6,8,9,11,14,15,17,19,21,27,32,36,43,49,53,68,79,98},l=0,h=19,mid,i,n,f=0;
clrscr();
printf("\nArray elements are:\n ");
for(i=0;i<20;i++)
{
printf("%d ",a[i]);
}
printf("\nenter the element to be searched : ");
scanf("%d",&n);
while(l<=h)
{
mid=(l+h)/2;
if(a[mid]==n)
{
printf("\nFound");
f=1;
break;
}
else
{
if(a[mid]>n)
{
h=mid-1;
}
else
{
l=mid+1;
}
}
}
if(f==0)
{
printf("\nElement not found");
}
getch();
}

Output:








8. /*-------BUBBLE SORT-------*/


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

int a[5],i,j,temp;
clrscr();
printf("\n enter 10 elements in an array :");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\t the sorted array is : \n");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
getch();
}




Output:













9. /*----- Selection Sorting-----*/


#include<stdio.h>
#include<conio.h> 
void main()
{
int a[100], n, i, j, p, temp;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for ( i = 0 ; i < n ; i++ )
{
scanf("%d", &a[i]);
}
for ( i = 0 ; i < ( n - 1 ) ; i++ )
{
p = i;
for ( j = i + 1 ; j < n ; j++ )
{
if ( a[p] > a[j] )
{
p = j;
}
}
if(p!=i)
{
temp = a[i];
a[i] = a[p];
a[p] = temp;
}
}
printf("Sorted list:\n");
for ( i = 0 ; i < n ; i++ )
{
      printf(" %d", a[i]);
}
getch();
}

Output:










10. WAP in c to convert decimal into binary.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],r,i=0,j,t,k,n;
clrscr();
printf("\n enter any numbar\n");
scanf("%d",&n);
while(n!=0)
{
r=n%2;
a[i]=r;
n=n/2;
i++;
}
k=--i;
j=0;
while(i>=j)
{
t=a[j];
a[j]=a[i];
a[i]=t;
j++;
i--;
}
printf("binary : ");
for(i=0;i<=k;i++)
{
printf(" %d",a[i]);
}
getch();
}



Output:




Sunday 10 July 2016

PATTERN programming

1.  WAP in c to print 


*
**
***
****

#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}


Output :
 

2.  WAP in c to print 


1
2  2
3  3   3
4  4   4   4


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch(); 

Output :

3. WAP in c to print 


1
1 2
1 2 3
1 2 3 4


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch(); 

Output :

4. WAP in c to print 


1
2 3
4 5 6 
7 8 9 10


#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k=1;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k++);
}
printf("\n");
}
getch(); 

Output :

5. WAP in c to print 


@
+ +
@ @ @
+ + + +


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==0)
{
printf("+ ",i);
}
else
{
printf("@ ",i);
}
}
printf("\n");
}
getch();
}


Output :

6. WAP in c to print 


* * * * *
* * * * *
* * * * *
* * * * *
* * * * *


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("* ");
}
printf("\n");
}
getch(); 

Output :

7. WAP in c to print 


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


#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d\t",i*j);
}
printf("\n");
}
getch(); 

Output :

Sunday 3 July 2016

ADVANCED LOOPING programs

1. WAP in c to for factorial of a given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a,s=1;
clrscr();
printf("\nEnter any number: ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
s=s*i;
}
printf("\nfactorial of %d is %d",a,s);
getch();
}

Output:







2. WAP in c for fibonacci series.

1 1 2 3 5 8 13 21 .......... 

#include<stdio.h>
#include<conio.h>
void main()
{
int f1=1,f2=1,f3=0,i=1;
clrscr();
printf("Fibonacci series:");
printf("%d %d ",f1,f2);
while(i<=8)
{
f3=f1+f2;
printf("%d ",f3);
f1=f2;
f2=f3;
i++;
}
getch();
}

Output:







3. WAP in c to check whether a given number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,f=0;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
f=1;
break;
}
}
if(f==1)
{
printf("\nNot Prime Number");
}
else
{
printf("\nPrime Number");
}
getch();
}

Output:






4. WAP in c to generate all prime numbers from 1 to 100.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,f;
clrscr();
printf("\n\t\tPrime numbers from 1 to 100\n");
for(i=1;i<=100;i++)
{
f=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
{
printf("%d\t",i);
}
}
getch();
}

Output:







5. WAP in c to find sum of digits of a given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,s=0,n,t;
clrscr();
printf("\nEnter any number:\n");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
n=n/10;
s=s+r;
}
printf("\nSum of digits of %d is %d",t,s);
getch();
}



Output:






6. WAP in c to find reverse of digits of a given number.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,s=0,n,t;
clrscr();
printf("\nEnter any number:\n");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
printf("\nReverse of digits of %d is %d",t,s);
getch();
}

Output:







7. WAP in c to check whether a given number is Armstrong or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,s=0,n,t;
clrscr();
printf("\nEnter any number:\n");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
n=n/10;
s=s+r*r*r;
}
if(s==t)
{
printf("\nArmstrong number");
}
else
{
printf("\nNot Armstrong number");
}
getch();
}

Output:






8. WAP to print all Armstrong numbers from 1 to 1000.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,s,n,t;
clrscr();
printf("\nArmstrong numbers from 1 to 1000:\n");
for(t=1;t<=1000;t++)
{
s=0;
n=t;
while(n!=0)
{
r=n%10;
n=n/10;
s=s+r*r*r;
}
if(s==t)
{
printf("%d\t",t);
}
}
getch();
}

Output:






9. WAP in c to check whether a given number is PALINDROME or not
(palindrome number is a number which is equal to its reverse).
ex-121,1331 etc.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,s=0,n,t;
clrscr();
printf("\nEnter any number:\n");
scanf("%d",&n);
t=n;   
while(n!=0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
if(s==t)
{
printf("palindrome");
}
else
{
printf("not palindrome");
}
getch();
}


Output:







10. WAP in c to print all PALINDROME numbers from 1 to 1000.
palindrome number is a number which is equal to its reverse.
ex-121,1331 etc.


#include<stdio.h>
#include<conio.h>
void main()
{
int r,s,n,t;
clrscr();
printf("\n\n\t\tpalindrome numbers from 1 to 1000\n\n");
for(n=1;n<=1000;n++)
{
s=0;
t=n;
while(t!=0)
{
r=t%10;
t=t/10;
s=s*10+r;
}
if(s==n)
{
printf("%d\t",n);
}
}
getch();

}


Output:


index

MENU Sample Papers  (new sample paper added for computers) C Language Mathematics IX (NCERT solution)   Computer IX (CBSE) ...