Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts

Saturday, August 9, 2014

Program to find twin prime numbers.

Program to find twin prime numbers. Please comment if you need help.

#include<stdio.h>
void main( )
{
int  i,n,k,r,a[50],p;
printf("nEnter the range:  ");
scanf("%d", &r);
i=1;
p=0;
while(i<=r)
{
k=0;
n=1;
while(n<=i)
{
if( i%n==0 )
k++;
n++;
}
if(k==2)
{
a[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(a[n+1] - a[n]==2)
printf("\n%d and %d are twin primes", a[n], a[n+1]);
}
}

Program to swap two numbers

Program to swap two numbers with address and pointers. Again, a necessary program for all amateur CSE students.

#include<stdio.h>
void swap(int *x,int *y)
{
 int temp;
 temp=*x;
 *x=*y;
 *y=temp;
}
void main()
{
  int a,b;
  printf("Enter the number a:");
  scanf("%d",&a);
  printf("\n Enter the number b:");
  scanf("%d",&b);
  swap(&a,&b);
  printf("\n a contain %d",a);
  printf("\n b contain %d",b);
}

Program to sort numbers in ascending and descending order

Simple C program to arrange numbers in an array in ascending and descending order. If you need help, let me know.

#include<stdio.h> main() { int a[50],n,i,j,t; printf("neter the number of numbers in aray"); scanf("%d",&n); printf("enter the number"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=0;j<(n-i)-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("array in ascending"); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("decending"); for(i=0;i<n;i++) { for(j=0;j<(n-i)-1;j++) { if(a[j]<a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0;i<n;i++) { printf("%d",a[i]); } printf("\n"); }

Addition, multiplication and transpose of matrices

This is a program for the addition, multiplication and transpose of matrices. If you have any doubts, please let me know.

#include<stdio.h> main() { int m,n,i,j,k,p,q,r,a[25][25],b[25][25],c[25][25],ch; printf("\n Enter the rows and columns"); scanf("%d %d",&m,&n); printf("\n Enter the value of the array"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("\n MENU \n 1)addition 2)multiplication 3)transpose \n Enter your choice"); scanf("%d",&ch); if(ch==1) { printf("\n Enter the value of the array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; } } printf("\n the array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==2) { printf("\n Enter the value of array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][i]; } } } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==3) { for(i=0;i<m;i++) { for(j=0;j<n;j++) b[i][j]=a[j][i]; } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",b[i][j]); } printf("\n"); } } }

Program to check for a leap year.

Simple C program to check whether a year is leap year or not. If you have any doubts, please let me know.

#include<stdio.h> void main() { int num; printf ("enter number"); scanf ("%d",&num); if(num%4==0&&num%100!=0)||(num%400==0) { printf ("leap year"); } else { printf ("no"); } }

Program to print out largest of 3 numbers

Program to print out the largest of 3 numbers. If you have any doubts, please let me know.


#include<stdio.h> void main() { int large,x,y,z; printf("enter the 3 numbers"); scanf("%d",&x); scanf("%d",&y); scanf("%d",&z); large=(x>y?(x>z?x:z):(y>z?y:z)); printf("%d",large); }