Some Programes With Algorithms and Outputs
Some Programes With Algorithms and Outputs
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
printf("\n--------------------------------------------------------------------------------\n");
getch();
return 0;
}
#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;
}
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;
}
Algorithm::
---------------------------------------------------------------------------------------------------------------------------------------
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::
//*---------------------------------------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::
printf("\n\n--------------------------------------------------------------------------------");
getch();
return 0;
}
Output.
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::
/*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("
");
OUTPUT.
------------------------------------------------------------------------------------------------------------------------------------------------------PROGRAMME::
/*word flow*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,n;
char str[200];
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;
}
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
}
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
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::
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
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
int main()
{
int i,j=1,cnt=0,d,k,done=1;
char name[2000];
char abbr[20];
{
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.