Wednesday, 18 March 2015

C Programs For Engineering 1st year Students page 4



Page 4

31. Program to accept a string print each word in new line.

#  include <stdio.h>
#  include <conio.h>
main( )
{
char ch;
clrscr( );
printf(“enter a string :”);
while(( ch=getchar( ))!=’\n’)
{
putchar(ch);
if(ch= =’ ‘)
printf(“\n”);
}
getch( );
}

32. Program to accept a string  and count no of capital letters, no. of  small letters and no. of special characters  

#  include <stdio.h>
#  include <conio.h>
main( )
{
char ch;
int c=0,s=0,s1=0;
clrscr( );
printf(“enter a string :”);
while(( ch=getchar( ))!=’\n’)
{
 if(ch>=’A’&& ch>=’Z’)
c=c+1;
else
if(ch>=’a’&& ch>=’z’)
s=s+1;
else
s1=s1+1;
}
printf(“ no of capital letters are %d”,c);
printf(“ no of smal1 letters are %d”,s);
printf(“ no of special characters are %d”,s1);
getch( );
}

33. Program to accept any single digit number and print it in words .

#  include <stdio.h>
#  include <conio.h>
main( )
{
int n;
clrscr( );
printf(“enter a number :”);
scanf(“%d “,&n);
switch(n)
{
 case 0:  printf(“ZERO”);
              break;
case 1:  printf(“ONE”);
              break;
case 2:  printf(“TWO”);
              break;
case 3:  printf(“THREE”);
              break;
case 4:  printf(“FOUR”);
              break;
case 5:  printf(“FIVE”);
              break;
case 6:  printf(“SIX”);
              break;
case 7:  printf(“SEVEN”);
              break;
case 8:  printf(“EIGHT”);
              break;
case 9:  printf(“NINE”);
              break;
 default:
printf(“please enter the number between 0 and 9”);
}
getch( );
}

34. Program to print prime numbers between 1 to 100

#  include <stdio.h>
#  include <conio.h>
main( )
{
int n, i, check;
clrscr();
for(i=1;i<=100;i++)
{
check=1;
for(n=2;n<=i/2;n++)
if(i%n= =0)
{
check=0;
break;
}
if(check= =1)
printf(“\n %d is a prime”,i);
else
printf(“\n %d is not a prime”,i);
}
getch( );
}

35. Program to  accept two numbers and print sum of two numbers by using functions

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a,b,c;
clrscr();
printf(“enter the value for a:”)
scanf(“%d”,&a);
printf(“enter the value for b:”)
scanf(“%d”,&b);
c=add(a,b);
printf(“sum of two numbers is %d”,c);
getch( );
}

int  add(int x, int y)
{
   int z;
  z=x+y;
return z;
}

36. Program to  accept a number and find factorial of given number

#  include <stdio.h>
#  include <conio.h>
main( )
{
int n,f;
clrscr( );
printf(“enter  a number:”)
scanf(“%d”,&n);
f= fact(n);
printf(“factorial value is %d”,f);
getch();
}

int fact(int n)
{
 int i, fa=1;
for(i=n;i>=1;i--)
fa=fa*i;
return fa;
}

37. Program to  accept a number and check the  given number Armstrong or not

#  include <stdio.h>
#  include <conio.h>
main( )
{
int n,arm;
clrscr();
printf(“enter  any 3 digit  number:”)
scanf(“%d”,&n);
arm= armstrong(n);
if(arm= =n)
printf(“%d is Armstrong number”,n);
else
printf(“%d not a Armstrong number”,n);
getch( );
}

int Armstrong (int n)
{
 int a,b,c,d;
a=n/100;
b=((n/10)%10);
c=n%10;
d=a*a*a+b*b*b+c*c*c;
return d; 
}

38. Program to accept a number and print the sum of given and Reverse number

#  include <stdio.h>
#  include <conio.h>
main( )
{
int a,b,n;

clrscr( );
printf(“enter  a  number:”)
scanf(“%d”,&n);
a=rev(n);
printf(“REVERSE OF A GIVEN NUMBER IS %d”,a);
b=add(n,a);
printf(“\n sum of a given and reverse number is %d”,b);
getch( );
}
int rev( int n)
{
int r,rev=0,s;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
return rev;
}
int add(int n, int a)
{
 return n+a;
}

39. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order.

#  include <stdio.h>
#  include <conio.h>
main( )
{
int i,a[10];
for(i=0;i<10;i++)
{
printf(“enter value for a[%d]”,i);
scanf(“%d”,&a[i]);
}
for(i=0;i<=4;i++)
printf(“\nA[%d]=%d”,i,a[i]);
for(i=9;i>=5;i--)
printf(“\nA[%d]=%d”,i,a[i]);
getch( );
}

40. Program to accept a string and print the reverse of the given string by using for loop.

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


Pages 123, 45678910


Load disqus comments

0 comments