Friday 20 May 2011

Write a C program to convert a Roman numeral to its decimal equivalent.


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf(“Enter the Roman Numeral:”);
scanf(“%s”,rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]==’I')
a[i]=1;
else if(rom[i]==’V')
a[i]=5;
else if(rom[i]==’X')
a[i]=10;
else if(rom[i]==’L')
a[i]=50;
else if(rom[i]==’C')
a[i]=100;
else if(rom[i]==’D')
a[i]=500;
else if(rom[i]==’M')
a[i]=1000;
else
{
printf(“\nInvalid Value”);
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i–)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf(“\nIts Decimal Equivalent is:”);
printf(“%d”,k);
getch();
}

Write a C program to find the 2’s complement of a binary number.


2’s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a C program to find the 2’s complement of a binary number.
 
Program:
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
/*
Output:
Enter the binary number101
The 2's complement is 011
*/

Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+………….+xn


Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression:
1+x+x2+x3+………….+xn
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum
Perform error checking. For example, the formula does not make sense for negative exponents – if n is less than 0. Have your program print an error message if n<0, then go back and read in the next pair of numbers of without computing the sum. Are any values of x also illegal ? If so, test for them too.
 
Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int s_sum,i,x,n;
clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
if(n<=0 || x<=0)
{
printf("Value is not valid\n");
}
else
{
s_sum=1;
for(i=1;i<=n;i++)
{
s_sum=s_sum+pow(x,i);
}
printf("Sum of series=%d\n",s_sum);
}
getch();
}
/*
Output:
Enter the values for x and n:2 4
Sum of series=31
*/

Write a C program to construct a pyramid of numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}
/*
Output:
Enter the number to generate the pyramid:
5

0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
5 4 3 2 1 0 1 2 3 4 5

Write a C program to generate Pascal’s triangle.


#include<stdio.h>
#include<conio.h>
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("Rows you want to input:");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
/*
Rows you want to input:6
Pascal's Triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Write a C program to count the lines, words and characters in a given text.


#include <stdio.h>
main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf(“KEY IN THE TEXT.\n”);
printf(“GIVE ONE SPACE AFTER EACH WORD.\n”);
printf(“WHEN COMPLETED, PRESS ‘RETURN’.\n\n”);
while( end == 0)
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != ‘\n’)
line[c++] = ctr;
line[c] = ”;
/* counting the words in a line */
if(line[0] == ”)
break ;
else
{
words++;
for(i=0; line[i] != ”;i++)
if(line[i] == ‘ ‘ || line[i] == ‘\t’)
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf (“\n”);
printf(“Number of lines = %d\n”, lines);
printf(“Number of words = %d\n”, words);
printf(“Number of characters = %d\n”, characters);
}
Output
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS ‘RETURN’.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205

a) Write a C program that displays the position or index in the string S where the string T begins, or – 1 if S doesn’t contain T.


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}

/*
Output:
Enter the first string:
sridhar siricilla
Enter the string to be searched:
siri
Second String is found in the First String at 8 position.
*/