Wednesday, 18 March 2015

C Programs For Engineering 1st year Students page 6



Page 6

51. Program to print “PASCAL TRIANGLE”.

#include<stdio.h>
#include<conio.h>
main()
{
int n,p=1,q,num,sp;
clrscr( );
printf(“enter the number of rows”);
scanf(“%d”,&n);
for(p=0;p<=n;p++)
{
for(sp=1;sp<=40-(3*p);sp++)
printf(“ “);
for(q=0;q<n;q++)
{
if((q==q)||(q==0))
num=1;
else
num=num*((q-q)+1)/q;
printf(“%2d”,num);
printf(“\n”);
}}
getch( );
}

52. Program to check whether a given number is perfect or not.

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,n,s=0;
clrscr();
printf(“enter the number”);
scanf(“%d”,&n);
for(i=1;i<n/2;i++)
if(n%i==0)
s+=i;
if(s= =n)
printf(“the number is perfect no”);
else
printf(“the number is not perfect “);
getch( );
}

53. Program to check whether a given number is prime number.

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,n,c=0;
clrscr( );
printf(“enter a number”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
printf(“given number is a prime number”);
else
printf(“given number is not prime number”);
getch( );
}

54. Program to read ‘n’ number and print them in matrix terms in all orders.

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,n,c,p,q,r,k,a[20];
clrscr();
printf(“enter the array size”);
scanf(”%d”,&n);
printf(“enter the elements”);
for(i=1;i<=n;i++)
scanf(“%d”,&a[i]);
i=1;
while(i<=n)
{
if(n%i==0)
{
r=i;
c=n/i;
k=1;
for(p=1;p<=r;p++)
{
for(q=1;q<=c;q++)
printf(“%d”,a[k++])
printf(“\n”);
}
i++;
getch( );
}

55. Program to search an element using binary search

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[100],i,n,x, mid, top, bot,c;
clrscr();
printf(“enter the array  size;”);
scanf(“%d”,&n);
printf(“enter the array elements”);
for(i=1;i<=n;i++)
scanf(“%d”,&a[i]);
top=1;
bot=n;
c=0;
printf(“enter the element to searched”);
scanf(“%d”,&x);
while((top <=bot)&&(c==0))
{
mid=(top+bot)/2;
if(a[mid]<x)
top=mid+1;
else
if(a[mid]>x)
bot=mid-1;
else 
c=1;
}
if(c==1)
printf(“elements is at position;%d”,mid);
else
printf(“elements is not in list”);
getch( );
}

56. Program to accept two numbers and print the sum of given two numbers by using pointers

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a, b,c;
clrscr( );
a=10;
b=20;
c=*(&a)+*(&b);
printf(“%d”,c);
getch( );
}

57. Programs to multiply two Matrices

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[10][10],b[10][10],c[10],[10],i,j,m,n,p,q,k;
clrscr( );
printf(“enter the size of first matrices”);
scanf(“%d%d’,&m,&n);
printf(“enter the size of second matrix”);
scanf(“%d%d’,&p,&q);
if(n==p)
{
printf(“enter first matrices elements”);
for(i=1;i<m;i++)
for(j=1;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter second matrix elements”);
for(i=1;i<p;i++)
for(j=1;j<q;j++)
scanf(“%d”,&b[i][j]);
for(i=1;i<m;i++)
for(j=1;j<n;j++)
{
c[i][j]=0;
for(k=1;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf(“the multiplication matrix is”);
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
print(“%2d”,c[i][j]);
printf(“\n”);
}
}
else
printf(“multiplication is not possible”);
getch( );
}


58. Program to print prime number between 1-100

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,n,c;
clrscr( );
for(n=1;n<=100;n++)
{
c=0;
for(i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
printf(“\n%d”,n);
}
getch( );
}

59. Program to accept a string and find the length of the string

#  include <stdio.h>
#  include <conio.h>
main( )
{
char name[80];
int i;
clrscr( );
printf(“enter a string ;”);
for(i=0;i<80&&((name[i]=getchar( ))!=’\n’);i++);
printf(“%d is the size of string”,i);
getch( );
}

60. Program to fibanocci of matrix

#  include <stdio.h>
#  include <conio.h>
#  include <math.h>
main( )
{
int a[10][10],i,j,m,n sum=0;
float norm;
clrscr( );
printf(‘enter the matrix size”);
scanf(“%d%d”,&m,&n);
printf(“enter the element of matrix”);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
scanf(“%d”,&a[i][j]);
sum=sum+(a[i][j]*a[i][j])
}
norm=sqrt(sum);
printf(“norm=%f”,norm);
getch( );
}

Pages 12345, 678910
Load disqus comments

0 comments