Wednesday, 18 March 2015

C Programs For Engineering 1st year Students page 5


Page 5

41. Program to accept a string and check the given string  is palindrome or not .

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,lim,c,check=1;
char word[80];
clrscr( );
printf(“ enter a string”);
for(i=0;i<80 && ((word [i]= getchar())!=’\n’);i++);
 lim=i-1;
c=lim/2;
for(i=0;i<=0;i++,lim--)
if(word[i]!= word[lim])
 {
     check=0;
      break;
}
 if(check= =1)
printf(“the given string is palindrome “);
else
printf(“ not palindrome”);
getch( );
}

42. Program to accept  values into 3 dimensional array and print .

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[3][3],i,j;
clrscr( );
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
 printf(“ enter the value for a[%d][%d] :”,i,j);
scanf(“%d”,&a[i][j]);
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
 printf(“ %d:”,a[i][j]);
printf(‘\n”);
}
getch( );
}
43. Program to print upper triangle .

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“ enter which no u want”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
 if(i<j)
a[i][j]=c;
else
a[i][j]=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
printf(“ %d:”,a[i][j]);
printf(‘\n”);
}
getch( );
}

44. Program to accept two 3 dimensional array and store addition of those into arrays into the third array .

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
 {
 printf(“enter the two values for a[%d][%d]  & b[%d][%d]”, i,j,i,j);
scanf(“%d%d”,&a[i][j],&b[i][j]);
}
 for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
 c[i][j]=a[i][j]+b[i][j];
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch( );
}

45. Program to accept a string and find the length of the given string by using functions

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

46. Program to count the number of words, characters, alphabets, vowels, consonants and digit in a line of text.

#include<stdio.h>
#include<conio.h>
main( )
{
int noa=0,nob=0,noc=0,nov=0,now=0,noch=0,l,I;
char ch,s[100];
clrscr( );
printf(“enter 2 lines of text”);
gets(s);
l=strlen(s);
for(i=0;i<1;i++)
{
switch(s[i])
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U’:
nov++;
break;
}
if(isalpha(s[i]))
noa++;
if(isdigit(s[i]))
nod++;
if(noa[i]==’ ‘) && (noa[i+1]!=’ ‘)
now++;
}
noch=l-nob;
noc=noa-nov;
printf(total no of words      %d”,now);
printf(total no of characters(without blanks)%d”,noch);
printf(total no of characters(including blanks)%d”,l);
printf(total no of  alphabets                          %d”,noa);
printf(total no of vowels                                       %d”,nov);
printf(total no of characters                     %d”,noc);
printf(total no of digits                       %d”,nod);
getch( );
}

47. Program  to accept two string and compare the strings are equal or not

#  include <stdio.h>
#  include <conio.h>
int getline (char line[ ], int lim );
int strc(char str1[ ], char str2[] );
main( )
{
char str1[80],str2[80];
int comp;
clrscr( );
printf(“enter first string:”);
getline(str1,80);
printf(“enter second string:”);
getline(str2,80);
comp=strc(str1,str2);
if(comp>0)
printf(“first string is bigger”);
else
if(comp==0)
printf(“both the strings are equal”);
getch( );
}

int getline(char str[], int lin)
{
 int i;
for(i=0;i<lin&&((str[i]=getchar())!=’\n’);i++);
if(str[i]=’\0’)
return i;
}

int strc(char  str1[],char  str2[])
{
 int i;
for(i=0;str1[i];i++)
if(str1[i]!=str2[i])
return str1[i]-str2[i];
return str1[i]-str2[i];
}

48. Program to sort the entered numbers using bubble sort.

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a[100],i,j,n,t;
clrscr( );
printf(“enter the array size”);
scanf(“%d”,&n);
for(i=1;i<n;i++)
scanf(“%d”,&a[i]);
for(i=1;i<=n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i]
a[i]=a[j];
a[j]=t;
}
printf(“the sorted elements are “);
for(i=1;i<=n;i++)
print(“%d”,a[i]);
getch( );
}

49. Program to read date,month, year and print the next day’s date,month,year.

#  include <stdio.h>
#  include <conio.h>
main( )
{
 int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d,m,y,nd,nm,ny,ndays;
clrscr( );
printf(“enter the date,month,year”);
scanf(“%d%d%d”,&d,&m,&y);
ndays=month[m-1];
if(m==2)
{
if(y%100==0)
{
if(y%400==0)
ndays=29;
}
else
if(y%4==0)
ndays=29;
}
nd=nd+1;
nm=m;
ny=y;
if(nd>ndays)
{
nd=1;
nm++;
}
if(nm>12)
{
nm=1;
ny++;
}
printf(“Given date is %d:%d:%d\n”,d,m,y);
printf(“next days date is %d:%d:%d”,nd,nm,ny);
getch( );
}

50. Program to interchange two values using pointers.

#  include <stdio.h>
#  include <conio.h>
void interchange(int *x,int *y);
main( )
{
int a,b;
clrscr( );
printf(“enter values of a and b”);
scanf(“%d%d”,&a,&b);
interchange(&a,&b);
}
void interchange(x,y)
int *x,*y;
{
int t;
t=*x;
*x=*y;
*y=t;
printf(“%d=x, %d=y”,*x,*y);
getch( );
}

Pages 1234, 5678910

0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget

Blog Archive