Saturday, September 7, 2013

C++ Program to find the average of given numbers with output

#include<iostream>
using namespace std;
int main(){
int n,i,a[50];
float average,total=0;
cout<<"Enter the total number of elements(max 50)";
cin>>n;
for(i=0;i<n;i++){
    cout<<"Enter the "<<i+1<<" number:";
    cin>>a[i];
}
for(i=0;i<n;i++){
    total=a[i]+total;
}
average=total/n;
cout<<"The sum of the given numbers is "<<total<<endl;
cout<<"The average is "<<average;
return 0;
}

Output:-


Thursday, August 29, 2013

C++ Program to print floyd triangle

#include<iostream>
using namespace std;
int main(){
int n,a=0,i,j;
cout<<"Enter the number of rows:";
cin>>n;
for(i=0;i<=n;i++){
for(j=0;j<i;j++){
a=a+1;
cout<<" "<<a;
}
cout<<endl;
}
return 0;
}


Output:-

Wednesday, August 28, 2013

C++ Program to push an item in a stack

#include<iostream>
using namespace std;
int main(){
int stack[10],top=-1,n,i;
if(top==9)
cout<<"Stack is Full";
else{
cout<<"Enter the item to be inserted:";
cin>>n;
top=top+1;
stack[top]=n;
}
cout<<"You have inserted:";
cout<<stack[top];
return 0;
}

Output:-




C++ Program to find the multiplication table of any number

#include<iostream>
using namespace std;
int main(){
int n,i,j;
cout<<"Enter any number:";
cin>>n;
for(i=1;i<11;i++){
cout<<n<<" * "<<i<<" = "<<(n*i)<<endl;
}
return 0;
}

Output:-

Tuesday, August 27, 2013

C++ Program to print triangle of stars

Downward triangle:-

#include<iostream>
using namespace std;
int main(){
int i,j;
for(i=5;i>0;i--){
for(j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}

output:-


Download the source code file (.cpp file) from dropbox


Upward triangle:-

#include<iostream>
using namespace std;
int main(){
int i,j;
for(i=1;i<6;i++){
for(j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}

Output:-


Download the source code file from dropbox








C++ Program for iterative linear search

#include<iostream>
using namespace std;
int main(){
int a[40],it,p=0,i,n;
cout<<"Enter the number of elements";
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"Enter item to be searched";
cin>>it;
for(i=0;i<n;i++){
if(a[i]==it){
p=i+1;
break;
}
}
if(p>0){
cout<<"item found at "<<p<<" position";
}
else
cout<<"Item not found";

return 0;
}

Output:-


Download the program  :- Pdf and source file(.cpp)

Sunday, August 11, 2013

C++ and Java Program to add two numbers

Using C++:-

#include<iostream> 
using namespace std;
int main(){
int a,b,c;
cout<<"Enter First Number";
cin>>a;
cout<<"Enter Second Number";
cin>>b;
c=a+b;
cout<<"The sum is"<<" "<<c;
return 0;
}


 

Output:-

 Using java:-

Using Scanner class(to take input from the user):- 

import java.util.Scanner;
class Add{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int a,b,c;
System.out.println("Enter First Number");
a=s.nextInt();
System.out.println("Enter Second Number");
b=s.nextInt();
c=a+b;
System.out.println("The sum of a and b is "+c);
}
}
 

Output:-

 

Using predefined values:-

class Add{
public static void main(String args[]){
int a=10,b=20;
int c=a+b;
System.out.println("First Number="+a+"\nSecond Number="+b);
System.out.println("Sum is "+c);
}
}

Output:-

 


 

 

 

Ads Inside Post