You are on page 1of 6

1.

Write a C program to count the number of


occurrences of all characters within the given
string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80];
int i,len,count=0;
printf("\n Enter a string:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>=65&&str[i]<=90||str[i]>=97&&
str[i]<=122)
count++;
}
printf("\n The no.of characters are %d",count);
getch();
}

Output:

Enter a string: Honesty is the best policy


The no. of characters are 22
2. Write a C program to check whether a number
is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,ctr=0;
printf("\n Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
ctr++;
}
}
if(ctr= =2)
{
printf("%d is prime",n);
}
else
{
printf("%d is not prime",n);
}
getch();
}
Output :
Enter a number: 5
5 is prime
3. Write a C program to convert a decimal number
to its binary equivalent.

#include<stdio.h>
#include<conio.h>
void main()
{
int decimal,binary[20],n=0,i,j;
printf("Enter the decimal no.:");
scanf("%d",&decimal);
for(i=decimal;i>=1;i=i/2)
{
binary[n]=i%2;
n++;
}
printf("The binary equivalent is :");
for(j=n-1;j>=0;j--)
{
printf("%d",binary[j]);
}
getch();
}

Output:

Enter the decimal no.:57


The binary equivalent is: 111001
4. Write a program to calculate Fibonacci
series.

#include<conio.h>
#include<stdio.h>
void main()
{
int a=0,b=1,c,n;
printf("\n Enter a limit:");
scanf("%d",&n);
printf("%d %d",a,b);
for(c=a+b;c<=n;)
{
printf("%d",c);
a=b;
b=c;
c=a+b;
}
getch();
}

Output :

Enter a limit: 5
011235
5.Write a program to find out the occurrences of
a given string within a text message.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80],str1[10],str2[10];
int i,len,count=0,j=0;
printf("\nEnter a text message: ");
gets(str);
printf("\nEnter a string: ");
gets(str1);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=' ')
{
str2[j]=str[i];
j++;
}
else
{
if(strcmpi(str1,str2)==0)
{
count++;
}
strcpy(str2,"\0");
j=0;
}
printf("No.of occurrences: %d",count);
getch();
}

Output :

Enter a text message: Everybody has hobbies and my


hobbies include reading and traveling.

Enter a string: hobbies


No. of occurrences: 2

You might also like