C programming:- Control Statements and Functions - Part 2
Write
a c program to convert a given decimal number to its equivalent binary number
#include <stdio.h>
#include <conio.h>
void main()
{
int n,j,a[50],i=0;
clrscr();
printf("\n enter the decimal
value :-");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%2;
i++;
n=n/2;
}
printf("\n binary
conversion\n");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}
write a function to accept 15
characters and display whether each input character is a digit , a lower case or an upper case letter
or a symbol
Any character is entered by the
user; write a program to determine whether the character entered is a capital
letter, a small case letter, a digit or a special symbol.The following table
shows the range of ASCII values for various characters.
Characters
|
ASCII Values
|
A – Z
|
65 – 90
|
a – z
|
97 – 122
|
0 – 9
|
48 – 57
|
special symbols
|
0 - 47, 58 - 64, 91 - 96, 123 –
127
|
#include<stdio.h>
#include<stdlib.h>
void
check();
char
ch[30];
int
i,n;
main()
{
printf("limit of
characters:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter a
Character");
scanf("%1s",&ch[i]);
}
puts(ch);
check();
}
void
check()
{
for(i=0;i<n;i++)
{
if(isdigit(ch[i])) // or if
(ch>=48 && ch<=57)
printf("\nentered
character %c is a digit",ch[i]);
else if(isalpha(ch[i]))
printf("\nentered
character %c is alphabet",ch[i]);
else if(ch>='a'
&&ch<='z')
printf("\nentered character
%c is in lowercase",ch[i]);
else if(ch>=97 &&
ch<=122)
printf("\nentered
character %c is in upper case",ch[i]);
else
printf("\nentered
character %c is special symbol",ch[i]);
}
}
/*output
limit
of characters:5
enter
a Character A
enter
a Character B
enter
a Character c
enter
a Character5
enter
a Character*
ABc5*
entered
character A is an alphabet
entered
character B is an alphabet
entered
character c is an alphabet
entered
character 5 is a digit
entered
character * is special symbol
*/
write a C program that reads 100
integers and find how many of them are prime numbers.
#include<stdio.h>
main()
{
int a[100],n;
int i,j,count=0;
printf("enter limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter a
number:");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
count=0;
for(j=1;j<=n;j++)
{
if(a[i]%j==0)
count++;
}
if(count==2)
printf("\n%d is a prime
no",a[i]);
else
printf("\n%d is not a
prime",a[i]);
}
}
/*output
enter
limit:5
enter
a number:1 2 3 4 5
enter
a number:enter a number:enter a number:enter a number:
1
is not a prime
2
is a prime no
3
is a prime no
4
is not a prime*/
Write a program to find
the sum of the series .
S=1- 3/2! +9/4!
-27/6!+……………… to n terms
#include<stdio.h>
#include<math.h>
//S=1-
3/2! +9/4! -27/6!+……………… to n terms
int
fact(int a);
main()
{
int t,sign=1;
int s,n,i,p,q;
printf("enter no of
terms:");
scanf("%d",&t);
s=1;p=3;q=2;
for(i=2;i<=n;i++)
{
s=s+(p/fact(q))*(pow(-1,sign));
p=p*3;
q=q+2;
}
printf("sum=%d",s);
}
int
fact(int a)
{
int i,f=1;
for(i=1;i<=a;i++)
{ f=f*i;
}
return(f);
}
/*output
sum=1*/
Write a C program to find whether a
number is perfect number or not
Definition of perfect number or What is perfect number?
Perfect
number is a positive number which sum of all positive divisors excluding that
number is equal to that number. For example 6 is perfect number since
divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
Note: 6 is the smallest perfect
number.
Next perfect number is 28 since 1+ 2
+ 4 + 7 + 14 = 28
#include<stdio.h>
int
main()
{
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect
number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
/*
output
student@ubuntu:~$
./a.out
Enter
a number: 28
28
is a perfect number
Enter
a number: 2
2
is not a perfect number
Enter
a number: 6
6
is a perfect number
*/
Perfect numbers with in arange
#include<stdio.h>
int
main()
{
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++)
{
i=1;
sum = 0;
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
Sample
output:
Enter
the minimum range: 1
Enter
the maximum range: 20
Perfect
numbers in given range is: 6
Write
a C program to read a sentence and count the number of each type of vowels in
it and print the result
#include<stdio.h>
main()
{
char ch[20];
int k,a=0,e=0,i=0,o=0,u=0;
printf("enter a sentence:");
gets(ch);
for(k=0;ch[k]!='\0';k++)
{
if(ch[k]=='a')
a++;
if(ch[k]=='e')
e++;
if(ch[k]=='i')
i++;
if(ch[k]=='o')
o++;
if(ch[k]=='u')
u++;
}
printf("
sentence has \n 'a' count=%d\n 'e' count=%d\n 'i' count=%d\n 'o' count=%d\n 'u'
count=%d",a,e,i,o,u);
}
/*
output
enter
a sentence:hello how are you
sentence has
'a' count=1
'e' count=2
'i' count=0
'o' count=3
'u' count=1
*/
Write
a c Program to find out and print all the three digit prime numbers
Refer
previous programs
write
a c program to generate and print a floyd’s triangle of 6 lines
#include<stdio.h>
main()
{
int i,j,p=1;
for(i=0;i<7;i++)
{
for(j=0;j<i;j++)
{
printf("%d
",p); p++;
}
printf("\n");
}
}
/*
output
1
2
3
4
5 6
7
8 9 10
11
12 13 14 15
16
17 18 19 20 21
*/
write an interactive program to compute the mean ,variance
and standard deviationof N numbers.
#include
<stdio.h>
#include
<math.h>
#define
MAXSIZE 10
main()
{
float x[MAXSIZE];
int
i, n;
float average, variance, std_deviation, sum
= 0, sum1 = 0;
printf("Enter the value of N
\n");
scanf("%d", &n);
printf("Enter %d real numbers
\n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/*
Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n; //type casting n to float
/*
Compute variance and standard deviation*/
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements =
%.2f\n", average);
printf("variance of all elements =
%.2f\n", variance);
printf("Standard deviation =
%.2f\n", std_deviation);
}
/*
output
Enter
the value of N
5
Enter
5 real numbers
1
2 3 4 5
Average
of all elements = 3.00
variance
of all elements = 2.00
Standard
deviation = 1.41
*/
Comments
Post a Comment