# Monthly Archives: June 2014

## Write an algorithm and draw a corresponding flowchart to print the sum of all integers starting from 2 to the given positive integer x where x >= 2. 10m Dec2006

Write an algorithm and draw a corresponding flowchart to print the sum of all integers starting from 2 to the given positive integer x where x >= 2. 10m Dec2006

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm is expressed in pseudo code – something resembling C language or Pascal, but with some statements in English rather than within the programming language

1. A sequential solution of any program that written in human language, called algorithm.
2. Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem.

Pseudo code:

• Initialize I to zero , Sum to zero
• Input a Number
• While I is Less than equal to Number
•         itoa(I,SNUM,10)
•               If SNUM[0] equal to ’2′
•                           SUM=SUM+I
•                Increment I
•         Print SUM
• Stop

Detailed Algorithm:

Step 1:  Input NUM

Step 2:  I = 0 , SUM =0

Step 3:  While ( I<= NUM)

itoa(I,SNUM,10)

If ( SSNUM[0] == ’2′)[I] == S[J] )

SUM=SUM+I

I++

Step 6:    Print SUM

Step 7: End

Program Code:

 Source code
```c#include<stdio.h>
#include<stdlib.h>
void main()
{
int SUM=0,NUM,i;
char SNUM[5];
clrscr();
printf("\nENTER ANY INTERGER >= 2 : ");
scanf("%d",&NUM);
for(i=0;i<=NUM;i++)
{
itoa(i,SNUM,10);
if(SNUM[0]=='2')
SUM=SUM+i;
}
printf("\n%d",SUM);
getch();
}```

Flowchart:-

## Write a recursive program in ‘C’ to find the L.C.M. (Least Common Multiple) of two given numbers. 10m Dec2006

Write a recursive program in ‘C’ to find the L.C.M. (Least Common Multiple) of two given numbers. 10m Dec2006

#include<stdio.h>
int lcm(int,int);
void main()
{
int NUM1,NUM2,LCM;
clrscr();
printf(“ENTER ANY TWO POSITIVE NUMBERS TO FIND ITS L.C.M. : “);
scanf(“%d%d”,&NUM1,&NUM2);
if(NUM1>NUM2)
LCM = lcm(NUM1,NUM2);
else
LCM = lcm(NUM2,NUM1);
printf(“LCM OF TWO NUMBERS IS %d”,LCM);
getch();
}

int lcm(int N1,int N2)
{
static int TEMP = 1;
if(TEMP % N2 == 0 && TEMP % N1 == 0)
return TEMP;
TEMP++;
lcm(N1,N2);
return TEMP;
}

 Source code
```#include<stdio.h>
int lcm(int,int);
void main()
{
int NUM1,NUM2,LCM;
clrscr();
printf("ENTER ANY TWO POSITIVE NUMBERS TO FIND ITS L.C.M. : ");
scanf("%d%d",&NUM1,&NUM2);
if(NUM1>NUM2)
LCM = lcm(NUM1,NUM2);
else
LCM = lcm(NUM2,NUM1);
printf("LCM OF TWO NUMBERS IS %d",LCM);
getch();
}

int lcm(int N1,int N2)
{
static int TEMP = 1;
if(TEMP % N2 == 0 && TEMP % N1 == 0)
return TEMP;
TEMP++;
lcm(N1,N2);
return TEMP;
}```

Screen Shots:

## Write a program in ‘C’ to print the following format pyramid : 10m Dec2006

Write a program in ‘C’ to print the following format pyramid : 10m Dec2006

C

C C C

C C C C C

C C C C C C C

C C C C C C C C C

#include <stdio.h>
void main()
{
int I,J,N=5,K=0;
clrscr();
for(I=1;I<=N;I++)
{
for(J=1;J<=N-I;J++)
{
printf(” “);
}
while(K!=2*I-1)
{
printf(“C “);
K++;
}
K=0;
printf(“\n”);
}
getch();
}

 Source code
```#include <stdio.h>
void main()
{
int I,J,N=5,K=0;
clrscr();
for(I=1;I<=N;I++)
{
for(J=1;J<=N-I;J++)
{
printf(" ");
}
while(K!=2*I-1)
{
printf("C ");
K++;
}
K=0;
printf("\n");
}
getch();
}```

Screen Shots:

Write a non-recursive procedure in ‘C’ for calculating power of a number ‘m’ raised by another number ‘n’ i.e. mn. 10m Dec2006

Solved program can be found on this link http://cssimplified.com/c-programming/write-the-functions-to-perform-the-following-10m-dec2005

## Design an algorithm and draw corresponding flowchart to print the value of the number in words when the number entered is in the range of 1 to 299. 10m Dec2006

Design an algorithm and draw corresponding flowchart to print the value of the number in words when the number entered is in the range of 1 to 299. 10m Dec2006

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char num[3];
void convert_to_words(char*);
clrscr();
printf(“Enter any number between 1 to 299 : “);
scanf(“%s”,&num);
convert_to_words(num);
getch();
}
void convert_to_words(char *num)
{
char *single_digits[] = { “zero”, “one”, “two”, “three”, “four”,
“five”, “six”, “seven”, “eight”, “nine”};

char *two_digits[] = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,
“fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”};

char *tens_multiple[] = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”,
“sixty”, “seventy”, “eighty”, “ninety”};
int len = strlen(num);
printf(“\n%s: “, num);
if (len == 1)
{
printf(“%s\n”, single_digits[*num - '0']);
return;
}
while (*num != ‘\0′)
{
if (len == 3)
{
if (*num -’0′ != 0)
{
printf(“%s “, single_digits[*num - '0']);
printf(“hundred “);
}
–len;
}
else
{
if (*num == ’1′)
{
int sum = *num – ’0′ + *(num + 1)- ’0′;
printf(“%s\n”, two_digits[sum]);
return;
}
else if (*num == ’2′ && *(num + 1) == ’0′)
{
printf(“twenty\n”);
return;
}
else
{
int i = *num – ’0′;
printf(“%s “, i? tens_multiple[i]: “”);
++num;
if (*num != ’0′)
printf(“%s “, single_digits[*num - '0']);
}
}
++num;
}
}

 Source code
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char num[3];
void convert_to_words(char*);
clrscr();
printf("Enter any number between 1 to 299 : ");
scanf("%s",&num);
convert_to_words(num);
getch();
}
void convert_to_words(char *num)
{
char *single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
int len = strlen(num);
printf("\n%s: ", num);
if (len == 1)
{
printf("%s\n", single_digits[*num - '0']);
return;
}
while (*num != '\0')
{
if (len == 3)
{
if (*num -'0' != 0)
{
printf("%s ", single_digits[*num - '0']);
printf("hundred ");
}
--len;
}
else
{
if (*num == '1')
{
int sum = *num - '0' + *(num + 1)- '0';
printf("%s\n", two_digits[sum]);
return;
}
else if (*num == '2' && *(num + 1) == '0')
{
printf("twenty\n");
return;
}
else
{
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digits[*num - '0']);
}
}
++num;
}
}```

Screen Shots:

## Using pointers, write a program in ‘C’ to count the occurrence of each character in a given string. 10m Dec2006

Using pointers, write a program in ‘C’ to count the occurrence of each character in a given string. 10m Dec2006

#include <stdio.h>
#include <string.h>
void main()
{
char string[100],*ptr;
int c=0,count[26]={0};
clrscr();
printf(“Enter a string\n”);
gets(string);
ptr=string[0];
while (*ptr)
{
/* Consider characters from ‘a’ to ‘z’ only */
if ( ptr >= ‘a’ && ptr <= ‘z’ )
count[string[c]-’a']++;
c++;
ptr++;
}
for(c=0;c<26;c++)
{
if(count[c]!=0)
printf(“%c occurs %d times in string.\n”,c+’a',count[c]);
}
getch();
}

 Source code
```#include <stdio.h>
#include <string.h>
void main()
{
char string[100],*ptr;
int c=0,count[26]={0};
clrscr();
printf("Enter a string\n");
gets(string);
ptr=string[0];
while (*ptr)
{
/* Consider characters from 'a' to 'z' only */
if ( ptr >= 'a' && ptr <= 'z' )
count[string[c]-'a']++;
c++;
ptr++;
}
for(c=0;c<26;c++)
{
if(count[c]!=0)
printf("%c occurs %d times in string.\n",c+'a',count[c]);
}
getch();
}```

Screen Shots:

## Write a program in C’ that accepts 10 words of varying length and arranges the words in the descending order of word length. Use arrays. 10m Dec2006

Write a program in C’ that accepts 10 words of varying length and arranges the words in the descending order of word length. Use arrays. 10m Dec2006

#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
char array[10][10],temp[10];
clrscr();
printf(“Enter ten words : \n”);
for(i=0;i<10;i++)
{
printf(“%d : “,i+1);
gets(array[i]);
}
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{

if(strlen(array[i])<strlen(array[j]))
{

strcpy(temp,array[i]);
strcpy(array[i],array[j]);
strcpy(array[j],temp);
}
}
}
printf(“\nSorted ten words are : \n”);
for(i=0;i<10;i++)
{
printf(“%s \n”,array[i]);
}
getch();
}

 Source code
```c#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
char array[10][10],temp[10];
clrscr();
printf("Enter ten words : \n");
for(i=0;i<10;i++)
{
printf("%d : ",i+1);
gets(array[i]);
}
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{

if(strlen(array[i])<strlen(array[j]))
{

strcpy(temp,array[i]);
strcpy(array[i],array[j]);
strcpy(array[j],temp);
}
}
}
printf("\nSorted ten words are : \n");
for(i=0;i<10;i++)
{
printf("%s \n",array[i]);
}
getch();
}```

Screen Shots:

Write a program in ‘C’ for the multiplication of two matrices. 10m Dec2006

Solved program can be found on this link http://cssimplified.com/c-programming/an-interactive-c-program-to-multiply-two-matrices

## Write a program in ‘C’ for the addition of two polynomials. Use Arrays and Structures. 10m Dec2006

Write a program in ‘C’ for the addition of two polynomials. Use Arrays and Structures. 10m Dec2006

#include<stdio.h>

void main()
{
int poly1[6][2],poly2[6][2],term1,term2,match,proceed,i,j;
printf(“Enter the number of terms in first polynomial : “);
scanf(“%d”,&term1);
printf(“Enter the number of terms in second polynomial : “);
scanf(“%d”,&term2);
printf(“Enter the coeff and expo of the first polynomial:\n”);
for(i=0;i<term1;i++)
{
scanf(“%d %d”,&poly1[i][0],&poly1[i][1]);
}
printf(“Enter the coeff and expo of the second polynomial:\n”);
for(i=0;i<term2;i++)
{
scanf(“%d %d”,&poly2[i][0],&poly2[i][1]);
}
printf(“The resultant polynomial after addition :\n”);
for(i=0;i<term1;i++)
{
match=0;
for(j=0;j<term2;j++)
{
if(match==0)
if(poly1[i][1]==poly2[j][1])
{
printf(“%d %d\n”,(poly1[i][0]+poly2[j][0]), poly1[i][1]);
match=1;
}
}
}
for(i=0;i<term1;i++)
{
proceed=1;
for(j=0;j<term2;j++)
{
if(proceed==1)
if(poly1[i][1]!=poly2[j][1])
proceed=1;
else
proceed=0;
}
if(proceed==1)
printf(“%d %d\n”,poly1[i][0],poly1[i][1]);
}
for(i=0;i<term2;i++)
{
proceed=1;
for(j=0;j<term1;j++)
{
if(proceed==1)
if(poly2[i][1]!=poly1[j][1])
proceed=1;
else
proceed=0;
}
if(proceed==1)
printf(“%d %d”,poly2[i][0],poly2[i][1]);
}
getch();
}

 Source code
```#include<stdio.h>
void main()
{
int poly1[6][2],poly2[6][2],term1,term2,match,proceed,i,j;
printf("Enter the number of terms in first polynomial : ");
scanf("%d",&term1);
printf("Enter the number of terms in second polynomial : ");
scanf("%d",&term2);
printf("Enter the coeff and expo of the first polynomial:\n");
for(i=0;i<term1;i++)
{
scanf("%d %d",&poly1[i][0],&poly1[i][1]);
}
printf("Enter the coeff and expo of the second polynomial:\n");
for(i=0;i<term2;i++)
{
scanf("%d %d",&poly2[i][0],&poly2[i][1]);
}
printf("The resultant polynomial after addition :\n");
for(i=0;i<term1;i++)
{
match=0;
for(j=0;j<term2;j++)
{
if(match==0)
if(poly1[i][1]==poly2[j][1])
{
printf("%d %d\n",(poly1[i][0]+poly2[j][0]), poly1[i][1]);
match=1;
}
}
}
for(i=0;i<term1;i++)
{
proceed=1;
for(j=0;j<term2;j++)
{
if(proceed==1)
if(poly1[i][1]!=poly2[j][1])
proceed=1;
else
proceed=0;
}
if(proceed==1)
printf("%d %d\n",poly1[i][0],poly1[i][1]);
}
for(i=0;i<term2;i++)
{
proceed=1;
for(j=0;j<term1;j++)
{
if(proceed==1)
if(poly2[i][1]!=poly1[j][1])
proceed=1;
else
proceed=0;
}
if(proceed==1)
printf("%d %d",poly2[i][0],poly2[i][1]);
}
getch();
}```

Screen Shots:-

Write a program in ‘C’ to check whether a given string is palindrome. Use pointers. 10m Dec2006

Solved program can be found on this link http://cssimplified.com/assignments/an-interactive-c-program-to-check-whether-the-given-string-is-a-palindrome-or-not-using-pointers

## Design an algorithm, draw a corresponding flow chart and write a program in C, to print the Fibonacci series.10m Jun2006

Design an algorithm, draw a corresponding flow chart and write a program in C, to print the Fibonacci series.10m Jun2006

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm is expressed in pseudo code – something resembling C language or Pascal, but with some statements in English rather than within the programming language

1. A sequential solution of any program that written in human language, called algorithm.
2. Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem.

Pseudo code:

• Initialize I to zero, Num1 to zero, Num2 to one
• While I is less than 11
•              Print Num1
•              Initialize Num1 to Num2 & Num2 to Sum of Num1 & Num2
•  Increment I
• End

Detailed Algorithm:

Step 1:  I=0, Num1=0, Num2=1

Step 2:  While (I < 11)

Print Num1

Num1=Num2

Num2=Num1+Num2

I++

Step 3:   End

Flowchart:-

Code:

 Source code
```#include<stdio.h>
void main()
{
int i,X=0,Y=1;
clrscr();
printf("\nFIBONACCI SERIES < 1000 ARE :- \n");
i=0;
while(i<11)
{
printf(" %d ",X);
X=Y;
Y=X+Y;
i++;
}
getch();
}```

Screen Shots:

Solved Recursive program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-fibonacci-series-of-numbers-using-recursion

## Write a program that does not use the inbuilt string functions to perform the following: 10m Jun2006

Write a program that does not use the inbuilt string functions to perform the following: 10m Jun2006

(i) To compare two strings

 Source code
```#include<stdio.h>
void main()
{
int str_cmp(char*,char*);
char Str1[50],Str2[50];
int val;
clrscr();
printf("\nENTER FIRST STRING TO COMPARE : ");
scanf("%s",&Str1);
printf("\nENTER SECOND STRING TO COMPARE : ");
scanf("%s",&Str2);
val=str_cmp(Str1,Str2);
if(val==0)
printf("\nSTR1 IS EQUAL TO STR2");
else if(val<0)
printf("\nSTR1 IS LESS TO STR2");
else if(val>0)
printf("\nSTR1 IS GREATER TO STR2");
getch();
}
int str_cmp(char *STR1,char *STR2)
{
int I,S1=0,S2=0,val=0,L1=0,L2=0;
{
while(*STR1)
{
L1++;
S1+=*STR1;
STR1++;
}
while(*STR2)
{
L2++;
S2+=*STR2;
STR2++;
}
if(L1==L2)
{
val=S1-S2;
printf("\n%d",val);
}
else if(L1<L2)
val=-1;
else if(L1>L2)
val=1;
}
return val;
}```

Screen Shots:

(ii) To copy a string

#include<stdio.h>
void main()
{
void str_copy(char*,char*);
char Str1[50],Str2[50];
int val;
clrscr();
printf(“\nENTER FIRST STRING TO COMPARE : “);
scanf(“%s”,&Str1);
str_copy(Str2,Str1);
printf(“\nSTR1 IS : %s”,Str1);
printf(“\nSTR2 IS : %s”,Str2);
getch();
}
void str_copy(char *STR2,char *STR1)
{
while(*STR1)
{
*STR2=*STR1;
STR1++;
STR2++;
}
*STR2=*STR1;

}

 Source code
```#include<stdio.h>
void main()
{
void str_copy(char*,char*);
char Str1[50],Str2[50];
int val;
clrscr();
printf("\nENTER FIRST STRING TO COMPARE : ");
scanf("%s",&Str1);
str_copy(Str2,Str1);
printf("\nSTR1 IS : %s",Str1);
printf("\nSTR2 IS : %s",Str2);
getch();
}
void str_copy(char *STR2,char *STR1)
{
while(*STR1)
{
*STR2=*STR1;
STR1++;
STR2++;
}
*STR2=*STR1;
}```

Screen Shots:

Design an algorithm, and write a program to find the factorial of a number using recursion. 10m Jun2006

Algorithm:

int fact(int NUM)
{
int RES;
if(NUM==1)
return(1);
else
RES=NUM*fact(NUM-1);
return(RES);
}

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-factorial-of-a-number-using-recursion

## A C program contains the following declarations: 10m Jun2006

A C program contains the following declarations: 10m Jun2006

int i, j;

long iX;

short S;

float X;

double dX;

char C;

Determine the resultant data type of each of the following expressions:

(i) i + C                        – int (resultant data type)

(ii) X + C                     – float (resultant data type)

(iii) dX + X                 – double (resultant data type)

(iv) ((int) dX) + iX      – long (resultant data type)

(v) i + X                      – float (resultant data type)

(vi) S + j                      – int (resultant data type)

(vii) iX + j                   – long (resultant data type)

(viii) S + C                  – short (resultant data type)

(ix) iX + C                   – long (resultant data type)

(x) i + j                        – int (resultant data type)

Screen Shots: