0% found this document useful (0 votes)
83 views

Some Programes With Algorithms and Outputs

The document contains algorithms and C programs for various mathematical and logical operations: 1) It includes programs to check if a number is prime, armstrong, palindrome, and find factors. 2) Other programs calculate sine, cosine, Fibonacci series, and string length. 3) For each concept, it provides the algorithm steps and a C program to implement the algorithm with sample inputs and outputs.

Uploaded by

Biswajit Paul
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Some Programes With Algorithms and Outputs

The document contains algorithms and C programs for various mathematical and logical operations: 1) It includes programs to check if a number is prime, armstrong, palindrome, and find factors. 2) Other programs calculate sine, cosine, Fibonacci series, and string length. 3) For each concept, it provides the algorithm steps and a C program to implement the algorithm with sample inputs and outputs.

Uploaded by

Biswajit Paul
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Programme to check whether the number is prime

Algorithm::
Step1: Input num, i=2
Step2: if num=1, then print num is prime
Step3: if i<num then goto step7
Step6: else goto step6
Step7: if remainder of num/i=0 then print num is not prime and end
Step8: i=i+1 and goto step3
Step6: if i=num then print this is prime
Step7: Terminate

--------------------------------------------------------------------------------------------------------------------------------------------/*programme to check whether the input number is prime or not*/


#include<stdio.h>
#include<conio.h>
int main()
{
int num,i=2;
printf("Enter a number to check whether its is prime or not\n");
scanf("%d", &num);
printf("\n--------------------------------------------------------------------------------");
if(num==1)
printf("\nthe num is a prime number.\n");
for(;i<num;i++)
{
if (num%i==0)
{
printf("\nthe num is not a prime number.\n");
break;
}
}
if(num==i)
printf("\nThis is a prime number.\n");

printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}

Two Outputs of the above programme

Programme to check whether the number is armstrong


Algorithm:
Step1: input r, q, x=0
Step2: read n
Step3: q=n
Step4: if q>10 then r = remainder of q/10 and goto step6
Step5: else goto step8
Step6: x=x+r*r*r
Step7: q=q/10 and goto step4
Step8: x=x+q*q*q
Step9: if x=n then print n is Armstrong
Step10: Terminate

/*Programme to check whether the number is armstrong*/


#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,q,x=0;
printf("Type a number:: ::");
scanf("%d", &n);
printf("\n--------------------------------------------------------------------------------\n");
for(q=n;q>10;q=q/10)
{
r=q%10;
x=x+r*r*r;
}
x=x+q*q*q;
if(n==x)
printf("This is an ARMSTRONG number");
else
printf("this is not an ARMSTRONG number");
getch();
return 0;
}

Output of the above programme

Programme to check if the string is Palindrome


Algorithm:
Step1: Input i, j=0, k=0, out[200] (a character array)
Step2: Read in[200] (a char array)
Step3: i=length of in-1
Step4: if i>=0 then goto step6
Step5: else goto stepd
Step6: out[j]=in[i]
Step7: j=j+1, i=i-1 and goto stepx4
Step8: if in = out then print string in is palindrome
Step9: else print string in is not palindrome
Step: Terminate
--------------------------------------------------------------------------------------------------------------------------------------/*programme to check if the string is a palindrome*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j=0,k=0;
char in[200]="";
char out[200]="";
printf("Enter a Text :: ");
gets(in);
printf("\n--------------------------------------------------------------------------------\n");
for(i=strlen(in)-1;i>=0;i--)
{
out[j]=in[i];
j++;
}
if(strcmp(in,out)==0)

printf("The number is a PALLINDROM");


else
printf("This is NOT a PALLINDROM");
printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Two outputs of the programme


PROGRAMME TO CALCULATE FACTORS OF A INPUT
Step1: Input i=1, j
Step2: Read num
Step3: If i<=num then goto step5
Step4: Else goto step7
Step5: If remainder of num/i=0 then print i
Step6: i=i+1 and goto step3
Step7: terminate

/*programme to calculate factors of a given number*/


#include<stdio.h>
#include<conio.h>
int main()
{
int num,i=1,j;
printf("Enter a Number to get its factors:: ::");
scanf("%d", &num);
printf("\n--------------------------------------------------------------------------------\n\n");
printf(The Factors of %d is>> );
for(;i<=num;i++)
{
if(num%i==0)
printf("%d ",i);
}
printf("\n\n--------------------------------------------------------------------------------");
getch();
return 0;
}

Output of the above programme

Programme to find out all the primes between 1-1000


Algorithm:
Step1: Input i=2,j=2
Step2: Print 1
Step3: If j<=1000 then goto step5
Step4: Else goto step11
Stp5: If i<=j-1 then goto step7
Step6: Else goto step9
Step7: If remainder of j/i=0 then goto step10
Step8: i++ and goto setp5
Step9: if i=j then print j
Step10: j++ and goto step3
Step11: terminate
---------------------------------------------------------------------------------------------------------------------------------------

/*programme to find out prime numbers in the range 1-1000*/


#include<stdio.h>
#include<conio.h>
int main()
{
int j,i;
printf("

#Prime numbers(range:1-1000)#");

printf("\n--------------------------------------------------------------------------------\n");
printf(" %d \n",1);
for(j=2;j<=1000;j++)
{
for(i=2;i<=j-1;i++)
{
if(j%i==0)
break;
}
if(i==j)
printf(" %d \n",j);

}
printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}

A part of the output


Programme to find out all the Armstrong numbers between 1-1000
Algorithm::
Step1: Input n=1, r,q,x=0
Step2: q=n
Step3: If n<=1000 then goto step5
Step4: Else goto stp14
Step5: If q>=10 then goto step7
Step6: Else goto stp10
Step7: r=remainder of q/10
Step 8: x=x+r*r*r
Step 9: q=q/10 and goto stp5
Step10: x=x+q*q*q

Step11: if x=n then print x


Step12: x=0
Step13: n=n+1 and goto stp3
Step14: Terminate

/*programme to find out all the armstrong number between 1-1000*/


#include<stdio.h>
#include<conio.h>

int main()
{
int n,r,q,x=0;
printf("

#Armstrong Numbers(Range::1-1000)#");

printf("\n--------------------------------------------------------------------------------\n");
for(n=1;n<=1000;n++)
{
for(q=n;q>=10;q=q/10)
{
r=q%10;
x=x+(r*r*r);
}
x=x+(q*q*q);
if(x==n)
printf("%d\n\n",x);
x=0;
}
printf("--------------------------------------------------------------------------------\n");
getch();
return 0;
}

Output of the above programme

Programme to generate Fibonacci series up to nth term

Algorithm::

Step1: Input j=0,i=3,ser[47] (integer array)


Step2: Read n
Step3: Print ser[1]=0, ser[2]=1
Step4: if i<=n then goto stpa
Step5: else goto step8
step6: print ser[i]=ser[i-1]+ser[i-2]
step7: i=i+1 and goto stp4
step8: Terminate

---------------------------------------------------------------------------------------------------------------------------------------

Programme::
#include<stdio.h>
#include<conio.h>
int main()
{
int n,j=0,i;
printf("Enter no. Of terms you want:: ::");

scanf("%d",&n);
int ser[47];
printf("\n--------------------------------------------------------------------------------");
printf("The Fibbonacci Series is::\n\n");
printf("%d\n\n%d\n\n",ser[1]=0,ser[2]=1);
for(i=3;i<=n;i++)
{
ser[i]=ser[i-1]+ser[i-2];
printf("%d\n\n",ser[i]);
}
printf("--------------------------------------------------------------------------------");
getch();
return 0;
}

Output:

Programme for calculating sine for a input in degree


Algorithm::
Step1: Input r=1, n=1, x=1, p=0(float type), y(float)
Step2: Read a(float),i
Step3: If r<=i then goto step5
Step4: Else goto step12

Step5: If n<=2*r-1 then goto stp7


Step6: Else goto stp9
Step7: x=x*n
Step8: n=n+1 and goto stp5
Step9: y=(((a* 3.141592654)/180) to the power of 2*r-1)/x
Step10:p=p+(-1)to the power of (r-1)*y
Step11: x=1,n=1,r=r+1 and goto stp3
Step12: print sine of a is p
Step13: Terminate

Programme::

//*---------------------------------------Evaluation of sine------------------------------------------------------------*//
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,r,n,x=1;
float a,p=0,y;
printf("Enter angle a in degree :: ");
scanf("%f",&a);
printf("Enter Range :: ");
scanf("%d",&i);
printf("\n--------------------------------------------------------------------------------\n");
for(r=1;r<=i;r++)
{
for(n=1;n<=2*r-1;n++)
x=x*n;
y=pow((a*3.141592654)/180,2*r-1)/x;
p=p+pow(-1,r-1)*y;
x=1;
}

printf("sine of a= %f",p);
printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}

Output.
Programme for calculating cosine for a input in degree
Algorithm::
Step:Input r=1, n=1, x=1, p=0(float type), y(float)
Step2: Read a(float), i (for range)
Step3: If r<=i then goto step5
Step4: Else goto step12
Step5: If n<=2*r-2 then goto stp7
Step6: Else goto stp9
Step7: x=x*n
Step8: n=n+1 and goto stp5
Step9: y=(((a* 3.141592654)/180) to the power of 2*r-2)/x
Step10:p=p+((-1)to the power of (r-1))*y
Step11: x=1,n=1, r=r+1 and goto stp3
Step12: print cosine of a is p
Step13: Terminate
PROGRAMME::
//*---------------------------------------Evaluation of cosine------------------------------------------------------------*//
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,r,n,x=1;
float a,p=0,y;
printf("Enter angle a in Degree:: ::");
scanf("%f",&a);
printf("Enter range of expantion:: ::");
scanf("%d",&i);
printf("\n--------------------------------------------------------------------------------\n");
for(r=1;r<=i;r++)
{
for(n=1;n<=2*r-2;n++)
x=x*n;
y=pow((a*3.141592654)/180,2*r-2)/x;
p=p+pow(-1,r-1)*y;
x=1;
}
printf("cos a= %f",p);
printf("\n--------------------------------------------------------------------------------\n");getch();
getch();
return 0;
}

==========================================================================
PROGRAMME TO CALCULATE LENGTH OF A STRING
Algorithm::

Step 1: Input i=0


Step 2: Read str[200] (char type)
Step3: If str[i]= \0 then goto step5
Step4: i=i+1 and goto step3
Step5: print i
Step6: Terminate
Programme::
/*------------------------------------for calculating length of a string------------------------------------------------*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char str[200];
printf("Enter string:: ::");
scanf("%s", &str);
printf("\n\n--------------------------------------------------------------------------------");
for(i=0;;i++)
{
if(str[i]=='\0')
break;
}
printf("\n

The length of the string:: ::%d",i);

printf("\n\n--------------------------------------------------------------------------------");
getch();
return 0;
}

Output.

Programme to fill space


Algorithm::
Step1: Input i=0
Step2: Read str[200] (char type)
Step3: If i<= length of the str-1 then goto
Step4: Else goto step7
Step5: If str[i]= (space) then str[i]=$
Step6: i=i+1 and goto step3
Step7: print str
Step8: Terminate
Programme::
/*--------------------------------------------------------------Programme to fill space--------------------------------------------------------------------*/
#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
int i;
char str[200];
printf("Enter A Sentence:: ::");
gets(str);
printf("\n--------------------------------------------------------------------------------\n\n");
printf("The output is:: ::");

for(i=0;i<=(strlen(str)-1);i++)
{
if(str[i]==' ')
str[i]='$';
}
puts(str);
printf("\n--------------------------------------------------------------------------------\n\n");
getch();
return 0;
}

Output.

Programme for displaying alphabet triangle


Algorithm::
Input i=1, j=1, p[27][27], in[1][1] =64 (both array are char type)
Read n (for required number of rows)
Stepd: If i<=n then goto stepa
Else goto end
Stepa: if j<=i then goto stepb
Else goto stepc
Stepb: print p[i][j]=in[1][1]+j
J++ and goto stepa
Stepc: print newline
Stp: j=1,i=i+1 and goto stepd
Stp: Terminate

-------------------------------------------------------------------------------------------------------------------------------------------------------

PROGRAMME::
/*Programme for displaying alphabet triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n;
char p[27][27];
char in[1][1];
char another='y';
printf("\n--------------------------------Alphabet Triangle------------------------------\n");
printf(" How many row you want?\n\n ");
scanf("%d", &n);
in[1][1]=64;
printf("--------------------------------------------------------------------------------");
printf("\n The TRIANGLE is::\n\n");
for(i=1; i<=n; i++)
{
printf("

");

for(j=1; j<=i; j++)


{
p[i][j]=in[1][1]+j;
printf("%c ", p[i][j]);
}
printf("\n\n");
}
printf("--------------------------------------------------------------------------------");
getch();
return 0;
}

OUTPUT.

PROGRAMME FOR A TEXT FLOW


Algorithm::
Step1: Input i, j, n
Step2: Read str
Step3: n=length of the string str
Step 4: i=n/2
Step5: If remainder of n/2=0 then j=n/2-1
Step6: Else j=n/2
Step7: If i<=n-1 then goto step9
Step8: Else goto step12
Step9: str[i]= , str[j]=
Step10: Print str
Step11: i++, j- - and goto step7
Step12: Terminate

------------------------------------------------------------------------------------------------------------------------------------------------------PROGRAMME::
/*word flow*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{

int i,j,n;
char str[200];

printf("type a WORD as input:: ");


scanf("%s",&str);

n=strlen(str);
j=n/2;
if(n%2==0)
j=n/2-1;
printf("\n--------------------------------------------------------------------------------\n\n");
printf("The Flow is::\n

%s\n",str);

for(i=n/2;i<=n-1;i++)
{
str[i]=' ';
str[j]=' ';
printf("

%s\n",str);

j--;
}
printf("\n--------------------------------------------------------------------------------\n\n");
getch();
return 0;
}

Two outputs of the above programme.

Algorithm::
Step1: Input cnt=1, i=0, j, k=0, done=0
Step 2: Read text[200] (char type)
Step 3: J=i+1
Step4: If i<length of text then goto step6
Step5: Else goto end
Step6: If k<=i-1then goto step8
Step7: Else goto step11
Step8: if text[i]=text[k] the done++ goto step10
Step9: Else k++ & goto step6
Step10: If done!=0 then done=0 goto step17
Step11: If j<=length of text then goto step13
Step12: Else goto step15
Step13: If text[i]=text[j] then cnt++
Step14: J++ and goto step11
Step15: printf frequency of text[i]=cnt
Step16: cnt=1
Step17: i++ and goto step

/*-----------------------Programme to find frequency of a character in a string------------------------------------------*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char text[200]="";
int cnt=1,i,j,k,done=0;
printf("Enter Text:: ::");
gets(text);
printf("\n--------------------------------------------------------------------------------\n");
for(i=0;i<strlen(text);i++)
{
for(k=0;k<=i-1;k++)
{
if(text[i]==text[k])
{
done++;
break;
}
}
if(done!=0)
{
done=0;
continue;
}
for(j=i+1;j<strlen(text);j++)
{
if(text[i]==text[j])
cnt++;
}
printf("Frequence of %c:: ::%d\n\n",text[i],cnt);
cnt=1;

}
printf("--------------------------------------------------------------------------------\n");
getch();
return 0;

Output
PROGRAMME TO MAKE A SENTENCE REVERSE
Algorithm::
Step1: Input i, j, k=0, p, r, cnt=0, done=0, str[100] (char type array)
Step2: Read text[100] (char type)
Step3: i=length of text-1
Step4: If i>=0 then goto step6
Step5: Else goto step15
Step6: If text[i]= (space) then goto step8
Step7: Else goto stepe
Step8: J=i+1
Step9: Str[k]=text[j]
Step10: If text[j+1]= then print (space)and goto step14
Step11: If j=length of text then print (space)and goto step14
Step12: Print str[k]
Step13: K++, j++ and goto step9
Step14: i-- and goto step4
Step15: Terminate
PROGRAMME::
/*programme to revere a sentence*/

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
int i,j,k=0,p,r,cnt=0,done=0;
char text[100]="";
char str[100]="";
printf("Enter a text:: ::");
gets(text);
printf("\n--------------------------------------------------------------------------------\n");
printf("The Reversed Sentence is::\n\n");
for(i=strlen(text)-1;i>=0;i--)
{
if(text[i]==' ')
{
for(j=i+1;;j++)
{
str[k]=text[j];
printf("%c",str[k]);
k++;
if(text[j+1]==' ')
{
printf(" ");
break;
}
if(j==strlen(text))
break;
}
}
if(i==0)
{

for(j=0;;j++)
{
str[k]=text[j];
printf("%c",str[k]);
k++;
if(text[j+1]==' ')
break;
}
}
}
printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}

Output

PROGRAMME TO FOR CHOPPING THE BLANKS IN SEENTENCE


Algorithm::
Step1: Input i=0, j=0, cnt=0, p,str[200] (char type)
Step2: Read text[200] (char type)
Step3: If i<=length of text-1 then got step5
Step4: Else goto step9
Step5: If text[i]= (space) then cnt++ and goto step8
Step6: If cnt!=0 then str[j]= (space), j++,cnt=0 and goto step8
Step7: Else str[j]=str[i],j++

Step8: i++ and goto step3


Step9: terminate
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*Prog. for cgopping blank sentence*/


#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
int i,j=0,cnt=0,p;
char text[200]="";
char str[200]="";
printf("Enter a text(give more than one space between two words)\n");
gets(text);
printf("\n--------------------------------------------------------------------------------\n");
for(i=0;i<=(strlen(text)-1);i++)
{
if(text[i]==' ')
cnt++;
else
{
if(cnt!=0)
{
str[j]=' ';
j++;
cnt=0;
}
str[j]=text[i];
j++;
}
}
printf("The output is:: :: %s",str);

printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}
Output::

PROGRAMME TO CONVERT A SENTENCE TO WORDCASE


(First character of all the words of the sentence will be cap)

Algorithm::
Step1: Input i=0, cnt=0
Step2: Read str[200] (char type)
Step3: If i<=length of str-1 then goto step5
Step4: Else goto terminate
Step5: if cnt=0, ascii val. of str[i]>=97 and <=122 then str[i]=str[i]-32
Step6: else goto step9
Syep7: print str[i]
Steo8: cnt=1, i++ and goto step3
Step9: if str[i]!= (space), ascii val. of str[i]>= 65and <= 90 then str[i]=str[i]+32 and goto step 12
Step10: if ascii val. of str[i+1]>=97 & <= 122 then str[i+1]=str[i+1]-32 then print (space) and str[i+1]
Step11: i++ and goto step13
Step12: print str[i]
Step13: i++ and goto step3
Step14: terminate

/* PROG FOR CONVERTING WORDCASE*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

int main()
{
int i,cnt=0;
char str[200]="";
printf("Enter a text :: );
gets(str);
printf(The output is :: );
for(i=0;i<=strlen(str)-1;i++)
{
if(cnt==0)
{
if(toascii(str[i])>=97&&toascii(str[i])<=122)
str[i]=str[i]-32;
printf("%c",str[i]);
cnt=1;
continue;
}
else
{
if(str[i]!=' ')
{
if(toascii(str[i])>=65&&toascii(str[i])<=90)
str[i]=str[i]+32;
printf("%c",str[i]);
}
else
{

if(toascii(str[i+1])>=97&&toascii(str[i+1])<=122)
str[i+1]=str[i+1]-32;
printf(" %c",str[i+1]);
i++;
}
}
}

getch();
return 0;
}

Output
Algorithm::
Step1: Input i=0, j=1, cnt=0, d=0, k, done=1, abbr[20] (char type)
Syep2: read name[200] (char type)
Step3: if d<=length of name-1 Then goto step5
Step4: else goto step7
Step5: if name[d]= (space) then cnt=cnt+1
Step6: d=d+1 and goto step3
Step7: print abbr[0]=name[0]
Step8: i<=length of name-1 then goto step10
Step9: else goto step14
Step10: if name[i]= then abbr[j]=name[i+1] and goto step12
Step11: else goto step13
Step12: print .abbr[j]; j=j+1
Step13: i=i+1 and goto step8

Step14: print newline


Step15: i=0, j=0
Step16: prnt abbr[0]
Step17: if i<=length of name=1 then goto step18
Step18: if name[i]= then goto step 20
Step19: else goto step29
Step20: if done=cnt then goto step22
Step21: else goto step27
Step22: print .
Step23: k=i+1
Step24: if k<=length of name-1 then abbr[j]=name[k]
Step25: else goto step30
Step26: j++, k++ and goto step24
Step27: abbr[j]=name[i+1]
Step28: print .abbr[j]; j++,done++
Step29: i++ and goto step17
Step30: Terminate
------------------------------------------------------------------------------------------------------------------------------------------------------------------------PROGRAMME::
#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
int i,j=1,cnt=0,d,k,done=1;
char name[2000];
char abbr[20];

printf("type a your name\n");


gets(name);
printf("\n--------------------------------------------------------------------------------\n");
for(d=0;d<=strlen(name)-1;d++)

{
if(name[d]==' ')
cnt++;
}
abbr[0]=name[0];
printf("%c",abbr[0]);
for(i=0;i<=strlen(name)-1;i++)
{
if(name[i]==' ')
{
abbr[j]=name[i+1];
printf(".%c",abbr[j]);
}
j++;
}
printf("\n\n");
printf("%c",abbr[0]);
for(i=0;i<=strlen(name)-1;i++)
{
if(name[i]==' ')
{
if(done==cnt)
{
printf(".");
for(k=i+1;k<=strlen(name)-1;k++)
{
abbr[j+1]=name[k];
printf("%c",abbr[j+1]);
j++;
}
}
else
{

abbr[j]=name[i+1];
printf(".%c",abbr[j]);
j++;
}
done++;
}
}
printf("\n\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}

Output.

You might also like