Programming

Program to compute the fibonacci series (c++)

Posted on

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=0;
b=1;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num1-2;ct++)
{
x=a+b;
cout << x << endl;
y=a;
a=b;
b=x;
}
getch();
}

Program to enter an integer and find out if it is even or odd (c++)

Posted on

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
cout << "Enter an integer : ";
cin>>x;
if(x%2==0)
cout << "The number " << x << " is even.";
else
cout << "The number " << x << " is odd.";
getch();
}

Program to enter a string and find its length (c++)

Posted on

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

void main()
{
clrscr();
int slength;
char x[81]; //Allowing the user to input a maximum of 80 characters.
cout << "Enter the string : " << endl;
cin>>x;
slength=strlen(x);
cout << "The length of the string " << x << " is " << slength << "." << endl;
getch();
}

input:
zotechnofreaks

output:
14

C++ Program to output an integer, a floating point number and a character

Posted on

#include <iostream.h>
#include <conio.h>

void main()
{
  clrscr();
  int x = 10;
  float y = 10.1;
  char z = 'a';
  cout << "x = " << x << endl;
  cout << "y = " << y << endl;
  cout << "z = " << z << endl;
  getch();
}

output:

x = 10
y = 10.1
z = a
 

Program to print sum of series 1-(x/1! +x^2 /2!+……+x^n /n!)( JAVA)

Posted on Updated on

Import java.io.*;
import java.lang.Math;
class series2
{ public static void main(String ar[])
{
int i;
double x=0,n=0,sum=0;
DataInputStream obj= new DataInputStream(System.in);
try
{ System.out.println(“Enter the value of x:”);
x=Integer.parseInt(obj.readLine());
System.out.println(“Enter the value of n:”);
n=Integer.parseInt(obj.readLine());
}
catch(IOException e)
{}
for(i=1;i<=n;i++)
{
sum=sum+Math.pow(x,i)/fact(i);
}
System.out.println(“Answer is:”+(1-sum));Zotechno freaks!
}
static int fact(int no)
{ int f=1;
for(int j=no;j>1;j–)
f=f*j;
return f;
}
}

Program to find the sum of the digits of a number (JAVA)

Posted on

import java.io.*;
class sumDig
{ public static void main(String ar[])
{
int i=0,s=0;
DataInputStream ins=new DataInputStream(System.in);
try
{
System.out.println(“Enter the No “);
i=Integer.parseInt(ins.readLine());
}
catch(IOException e)
{ }
while(i>0)
{ s=s+i%10;
i=(int)(i/10);
}
System.out.println(“Sum of the digits is :”+s);
}
}

Program to display perfect numbers from 1 to 60 (JAVA)

Posted on

class perfect
{ public static void main(String ar[])
{
int i,sum=1;
System.out.print(“Perfect nos from 1 to 60 are\n1,”);
for(int j=2;j<=60;j++)
{ sum=1;
for(i=2;i<j;i++)
{ if(j%i==0)
sum=sum+i;
}
if(j==sum)
System.out.print(j+”,”);
}
}
}

Program to find biggest of three numbers (JAVA)

Posted on

import java.io.*;
class big
{
public static void main(String ar[])
{ float a=0,b=0,c=0;
DataInputStream obj=new DataInputStream(System.in);
try
{
System.out.println(“Enter value for a”);
a=Float.valueOf(obj.readLine()).floatValue();
System.out.println(“Enter value for b”);
b=Float.valueOf(obj.readLine()).floatValue();
System.out.println(“Enter value for c”);
c=Float.valueOf(obj.readLine()).floatValue();
}
catch(IOException e)
{ }
if(a>b &&a>c)
System.out.println(“Biggest a:”+a);
else if(b>c)
System.out.println(“Biggest b:”+b);
else
System.out.println(“Biggest c:”+c);
}
}

simple calculator (JAVA)

Posted on Updated on

import java.io.*;
class calc
{
public static void main(String arg[])
{
int a=0,b=0,c=0,ans,p=0;
DataInputStream ins;
ins=new DataInputStream(System.in);
System.out.println(“1:Addition”);
System.out.println(“2:Subtraction”);
System.out.println(“3:Multiplication”);
System.out.println(“4:Division”);
System.out.println(“5:Modulo”);
do
{
try
{
System.out.print(“Enter 1st no. “);
a=Integer.parseInt(ins.readLine());
System.out.print(“Enter 2nd no. “);
b=Integer.parseInt(ins.readLine());
System.out.print(“Enter ur choice “);
c=Integer.parseInt(ins.readLine());
}
catch(IOException e)
{ }
switch(c)
{
case 1:ans=a+b;
System.out.println(“Ans is “+ans);
break;
case 2:ans=a-b;
System.out.println(“Ans is “+ans);
break;
case 3:ans=a*b;
System.out.println(“Ans is “+ans);
break;
case 4:ans=a/b;
System.out.println(“Ans is “+ans);
break;
case 5:ans=a%b;
System.out.println(“Ans is “+ans);
break;
default:
System.out.println(“Enter valid choice”);
}
try
{
System.out.println(“Enter 1 if u want to continue “);
p=Integer.parseInt(ins.readLine());
}

catch(IOException e)
{ }
}
while(p==1);
}
}

Program to check whether a number is palindrome or not(JAVA)

Posted on

import java.io.*;
class palindrome
{ public static void main(String ar[])
{
int n=0,re=0,q;
DataInputStream obj= new DataInputStream(System.in);
System.out.println(“Enter any integer:”);
try
{ n=Integer.parseInt(obj.readLine());
}
catch(IOException e)
{}
q=n;
while(q>0)
{ re=(re*10)+q%10;
q=q/10;
}
if(n==re)
System.out.println(“No.is palindrome”);
else zotechnofreaks!
System.out.println(“No.is not palindrome”);
}
}