Q.1 Write a program to covert a number from binary to decimal and vice versa.
-----------------------------------------------------------------------------------------------
//1.Binary to decimal Conversion and
//2.Decimal to Binary
//coded by JA,BPC PVM
void main()
{
int ch;
void bin2dec(void);
void dec2bin(void);
clrscr();
printf("Enter a choice\n");
printf("1. Binary To Decimal\n");
printf("2. Decimal To Binary\n");
scanf("%d",&ch);
switch(ch)
{
case 1: bin2dec();
break;
case 2: dec2bin();
break;
default: printf("Oops... Invalid Choice");
}
getch();
}
void bin2dec()
{
int n,b=1,ans=0,r;
printf("Enter a Binary Number\n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
ans=ans+r*b;
n=n/10;
b=2*b;
}
printf("Decimal#:%d",ans);
}
void dec2bin()
{
int a,d,b=1,i,ans=0;
printf("Enter a decimal number\n");
scanf("%d",&d);
while(d>0)
{
a=d%2;
//printf("%d\n",a);
d=d/2;
a=b*a;
ans=ans+a;
b=b*10;
}
printf("Binary#:%d",ans);
}
----------------------------------------------------------------------------------------------
#include <stdio.h>
-----------------------------------------------------------------------------------------------
//1.Binary to decimal Conversion and
//2.Decimal to Binary
//coded by JA,BPC PVM
void main()
{
int ch;
void bin2dec(void);
void dec2bin(void);
clrscr();
printf("Enter a choice\n");
printf("1. Binary To Decimal\n");
printf("2. Decimal To Binary\n");
scanf("%d",&ch);
switch(ch)
{
case 1: bin2dec();
break;
case 2: dec2bin();
break;
default: printf("Oops... Invalid Choice");
}
getch();
}
void bin2dec()
{
int n,b=1,ans=0,r;
printf("Enter a Binary Number\n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
ans=ans+r*b;
n=n/10;
b=2*b;
}
printf("Decimal#:%d",ans);
}
void dec2bin()
{
int a,d,b=1,i,ans=0;
printf("Enter a decimal number\n");
scanf("%d",&d);
while(d>0)
{
a=d%2;
//printf("%d\n",a);
d=d/2;
a=b*a;
ans=ans+a;
b=b*10;
}
printf("Binary#:%d",ans);
}
Qn.2 Program to find sum of natural numbers upto n
--------------------------------------------------------------------
main() {
main() {
int i, sum;
i=1;
sum=0;
printf(“Enter the Limit n”);
scanf(“%d”, &n)
while(i<=n)
{
sum
= sum + i;
}
printf(“ Sum of Natural numbers upto %d is: %d”, n, sum);
}
Qn.3 Program to find factorial of n
---------------------------------------------
main() {
int i, f;
i=1;
f = 1;
printf(“Enter the Limit n”);
scanf(“%d”, &n)
while(i<=n)
{
f
= f * i;
}
printf(“ Factorial of %d is: %d”, n, f);
}
Qn.4 Program to find the multiplication table of n
----------------------------------------------------------------
main( ) {
int i,n,prd;
printf(“Enter the number n”);
scanf(“%d”, &n);
printf(“\n Multiplication Table of %d”, n);
for(i=1; i<=n; i++)
{
prd
= i*n;
printf(“%d
x %d = %d\n”, i, n, prd);
}
}
Qn.5 Program to odd number series upto n
-------------------------------------------------------
main()
{
int i, n;
printf(“Enter the number n”);
scanf(“%d”, &n);
printf(“\n Odd number series upto %d\n”, n);
for(i=1; i<=n; i++)
{
if(
i%2 !=0 ) printf(“%d \t”, i);
}
}
Qn.6 Fibonacci sequence is the series of numbers:
---------------------------------------------------------------
main()
{
int n, t1=0,t2 =1;
printf(“Enter n”);
scanf(“%d”,&n);
printf(“%d\t%d\t”,t1,t2);
for(
i = 3; i<=n; i++)
{
nt
= t1 +t2;
printf(“%d\t”,nt);
t1
= t2;
t2
= nt;
}
}
Qn.7 Write
a program to convert temperature from Celsius to Fahrenheit
/* Example Program – nested if – else statement */
main() {
float t, nt; char v;
printf(“Enter
the Temperature\n”); scanf(“%d”,&t);
printf(“Enter
the Temperature Scale: C or F \n”); scanf(“%c”,&v);
if(( v == ‘C’) || (v == ‘F’) )
{
if(v == ‘C’)
nt =
(9/5 * t ) + 32; printf( “The
temperature after conversion is %f”, nt);
else
nt = (t – 32) *
5/9; printf( “The temperature after
conversion is %f”, nt);
}
else
printf(
“The temperature Code entered is INVALID”);
}
Qn.8 Check
for Prime numbers program in C language
------------------------------------------------------------------------#include <stdio.h>
main()
{
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
printf("n is a Prime number");
else
printf("n is not a Prime number");
}
Qn.9 Finding
GCD
------------------------
main()
{
int n1,n2,i, hcf;
printf(“Input two
numbers\n”);
if(n1<n2)
min=n1; else min = n2;
for(i=1;i<=min;i++)
{
If((n1%i==0)&&(n2%i==0))
{
hcf=i;
}
}
printf(“HCF of %d
and %d is:%d”, n1,n2,hcf);
}
Qn. 10 Generate
Prime Numbers Series
---------------------------------------------------
#include <stdio.h>
main()
{
int n, i, j,c;
printf("Enter a Limit: n\n");
scanf("%d", &n);
printf(“Prime Number series\n”);
for (i = 2;
i <= n; i++)
{
c = 0;
for (j = 1;
j<= i; j++)
{
if (n % i == 0) c++;
}
if (c == 2) printf("%d\nt", i);
}
}
Qn: 11 Programs with Arrays – finding sum of elements
------------------------------------------------------------------------
void main()
{
int
values[5]={11,1,3,6,10};
for (int i =
1; i < 5; i++)
{
sum =
values[i];
}
printf(“Sum
of Array Elements %d”, sum);
}
Modifying the above program to accept data at run time
void main()
{
int
v[5],sum=0;
printf(“Input Array Elements\n”):
for (int i =
0; i < 5; i++)
{
scanf(“%d”, &v[i]);
}
printf(“Sum of
Array Elements is:\n”);
for (int i = 0; i < 5; i++)
{
sum = sum
+ v[i];
}
printf(“%d”, sum);
}
Qn.12 Program to find largest of n array elements
----------------------------------------------------------------
void main()
{
int i,
v[25],n, larg;
printf(“Enter Size\n”);
scanf(“%d”,
&n);
printf(“Input Array Elements\n”):
for ( i = 0;
i < n; i++)
{
scanf(“%d”, &v[i]);
}
larg = v[0];
for ( i = 0; i < n; i++)
{
if( v[i]
> larg) larg = v[i];
}
printf(“\n Largest is %d”, larg);
}
Exercise Qn: Program to list even
numbers from an array of n elements
Qn13. Find length of a string?
main(){
char pqr[15];
int i, L=0;
printf(“Enter a string”);
scanf(“%s”,pqr);
for(i=0;
pqr[i] !=‘\0’; i++)
{
L=L+1;
}
printf(“Length of the string is %d”, L);
}
Qn.14. Reversing a string?
main()
{
char pqr[15];
int
i, L=0;
printf(“Enter a string”);
scanf(“%s”,pqr);
for(i=0; pqr[i] !=‘\0’; i++)
{
L=L+1;
}
i= L-1;
while(i < =0)
{
printf(“%c”, pqr[i]);
i=i-1;
}
}
Qn.14. Count vowels?
main()
{
char pqr[15];
int
i, L=0;
printf(“Enter a string”);
scanf(“%s”,pqr);
for(i=0; pqr[i]!=‘\0’; i++)
{
if ( (pqr[i]==‘a”)|| (pqr[i]==‘e’)||
(pqr[i]==‘i’)|| (pqr[i]==‘o’)||(pqr[i]==‘u’))
L= L+1;
}
printf(“Count of vowels is %d”, L);
}
Qn.15. Compare two strings?
main()
{
char pqr[15],xyz[15];
int
i, L1=0, L2=0, flag=0;
printf(“Enter two string”);
scanf(“%c%c”,pqr, xyz);
for(i=0; pqr[i] !=‘\0’; i++)
{
L1=L1+1;
}
for(i=0; xyz[i] !=‘\0’; i++)
{
L2=L2+1;
if(L1 != L2)
printf(“Not
Similar”);
else
{
for (i=0; i<L1; i++)
{
if(pqr[i]
!= xyz[i])
flag=1;
}
}
if(flag==0)
printf(“Two
strings similar”);
else
printf(“Two
strings similar”);
}
}
Qn.16. Count occurrence of a character?
main()
{
char h, pqr[15];
int
i, ans =0;
printf(“Enter a string”);
scanf(“%s”,pqr);
printf(“Enter a Character”);
scanf(“%c”, &h);
for(i=0; pqr[i]!=‘\0’; i++)
{
if ( pqr[i]==h )
ans =ans +1;
}
printf(“Count of %c in %s is %d:”, h, pqr, ans);
}
Qn.17 Write a program to concatenate two
strings
main()
{
char str1[10], str2[10], str3[20];
int
i, L = 0, J = 0;
printf(“Enter String 1\n”);
scanf(“%s”, str1);
printf(“Enter String 2\n”);
scanf(“%s”, str2);
for(i=0; str1[i] !=‘\0’; i++)
{
str3[i] = str1[i];
L = L+1;
}
J = L ;
for(i=0; str2[i] !=‘\0’; i++)
{
str3[J] = str2[i];
J++;
}
str3[J] = ‘\0’;
printf(“The
Concatenated String is: %s”, str3);
}
Q.18. Roots of Quadratic Equation
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int a,b,c,d,d1,d2,p1,x,y;
float d3,p2;
clrscr();
printf(“\n Enter the values of a,b and c”);
scanf(“%d%d%d”,&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>0)
{
d1=sqrt(d);
x=(-b+d1)/(2*a);
y=(-b-d1)/(2*a);
printf(\n Roots are Real and Unequal”);
print(“\nsolution:\nRoot1=%d\Root2=%d”,x,y);
}
if(d==0)
{
x=(-b)/(2*a);
y=(-b)/(2*a);
printf(“\n Roots are Real and Equal)
printf(“\n Root1=%d\n Root2=%d”,x,y);
}
if(d<0)
{
printf(“\nRoot are Imaginary”);
d2 = -d;
d3 = sqrt(d2);
p1 = -b/(2*a);
p2 = d3/(2*a);
printf(“\nRoot 1=%d+%f”,p1,p2);
printf(“\n Root 2=%d-i,%f”,p1,p2);
}
}
No comments:
Post a Comment