Showing posts with label Cplusplus. Show all posts
Showing posts with label Cplusplus. Show all posts

Tuesday, 15 September 2015

Pascal Triangle Program in C#

One of the most thrilling variety styles is Pascal’s Triangle (named after Blaise Pascal, a well-known French Mathematician and truth seeker). Pascal’s Triangle became at first developed by means of the historic Chinese language, but Blaise Pascal changed into the primary individual to discover the significance of all the patterns it contained.

Source Code 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PascalTriangle
{
class Program
{
static void Main(string[] args)
{
int row;
int position;
Console.Write("Please input a row Number : ");
row = Convert.ToInt32(Console.ReadLine());
Console.Write("Please input position along the row # "+row+": ");
position = Convert.ToInt32(Console.ReadLine());
if (row < position)
{
Console.Write( "\nRow : " + row);
Console.Write("\nPosition: " + position);

Console.Write( "\nInvalid entry. Position must be less than ( < ) or equal to ( = ) Row.");
int a1 = Convert.ToInt32(Console.ReadLine());
return;
}
else
{
Console.Write("\nValue at row " + row + " and position " + position + " is " + compute_pascal(row, position));
}
int a = Convert.ToInt32(Console.ReadLine());
}

private static int compute_pascal(int row, int position)
{
if (position == 1)
{
return 1;
}
else if(position == row)
{
return 1;
}
else
{
return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
}
}
}
}


Sunday, 17 May 2015

Pascal Triangle in C++ using Recursive Function

This code is the simple demonstration of Pascal triangle in which you can tell the row and column count and it will return you the value at that specific row column count.it is the very interesting number pattern found in mathematics.

Source Code

#include <iostream>

using namespace std;

int compute_pascal(int row, int position);
int main()
{
int row, position;
cout<<"Please input a row Number ";
cin>>row;
cout<<"Please input position along the row # "<<row<<" : ";
cin>>position;
if(row<position)
{
cout<<"\nRow : "<<row;
cout<<"\nPosition: "<<position;

cout<<"\nInvalid entry. Position must be less than or equal to row.";
return 0;
}else{
cout<<"\nValue at row "<<row<<" and position " <<position<<" is "<<compute_pascal(row, position);
}

}
int compute_pascal(int row, int position)
{
if(position == 1)
{
return 1;
}
else if(position == row)
{
return 1;
}
else
{
return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
}
}

Output of the Program

pascal triangle c++ program

Monday, 4 May 2015

C++ Program to Input Marks of 6 Subjects from 20 Students and Calculate the Sum and Average Marks of all the Students using Struct

This is the simple C++ console program for beginners to compute the result of a Class. The program take take of the student, the marks in each subject and then compute the overall marks of the student and then calculate the average.You can change formula of calculation as per requirement.

Source Code

# include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
/*
 * uzair
 */
//Structure
struct student
{
char name[20];
int grade[6];
};

//Struct Instance
student stdInstance[50];


//Globel variables
int i=0;
int size=0;

// Function Declearation
int student_menu();
void add_student();

// Main
int main()
{
cout<<"\a\n\n\n"
<<"\t*******************************************************"<<endl
<<"\t** **"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** **"<<endl
<<"\t** Student ARRAY and STRUCTURES Demo **"<<endl
<<"\t** **"<<endl
<<"\t*******************************************************"<<endl
<<endl
<<endl;
cout<<"\t\t\t";
student_menu();

return 0;
}//end main
int student_menu()
{
system ("color 0F");
char student_menu_choice[5];
cout<<endl<<endl
<<endl<<endl<<endl
<<"\a\t *******************************************************"<<endl
<<"\t ** **"<<endl
<<"\t ** You Are In **"<<endl
<<"\t ** **"<<endl
<<"\t ** Student Menu **"<<endl
<<"\t ** **"<<endl
<<"\t *******************************************************"<<endl
<<endl;
loop_of_student_menu:

cout<<"\t\t What do you want to do for student " <<endl
<<"\t\t ===================================" <<endl
<<endl
<< "\n\n\t 1 => Add student "
<< "\n\n\t 2 => Student menu Exit \n"
<< "\t=== ------------------ "<<endl
<<"\t\t\t\tYou Select : ";

cin >> student_menu_choice;

i = atoi (student_menu_choice);
if (i>0)
{
switch (i)
{
case 1:
{
add_student();//Function
break ;
}
case 2:
{
return 0;
}break ;
default :
{
return 0;
}break ;
}//end switch
}//end if
else
{
system("CLS");

}//end else
return 0;
}

void add_student()
{
system ("color F0");
system("CLS");
cout<<endl<<endl<<endl
<<endl<<endl<<endl
<<"\a\t *******************************************************"<<endl
<<"\t ** **"<<endl
<<"\t ** Adding Student **"<<endl
<<"\t ** **"<<endl
<<"\t *******************************************************"<<endl;

cout<<endl
<<"\t\tCOLLECTING DATA FOR EMPLOYEE NO : "<<size
<<endl
<< "\n\tEnter name terminating at \".\" : ";

cin.getline(stdInstance[size].name,20,'.');//taking name

for(int i=0;i<6;i++){
cout<< "\n\tEnter Score For course # "<<i+1<<" : ";
cin>>stdInstance[size].grade[i];

}

cout<<endl
<<"Student added with following data" <<endl
<<endl
<<"Name : "<<stdInstance[size].name <<endl;
int sum=0, average=0;

for(int j=0;j<6;j++){
cout<< "\n\tScore For course # "<<j+1<<" : "<<stdInstance[size].grade[j];
if(stdInstance[size].grade[j]<50){
cout<< "\n\tGrade is : F ";
}else{
cout<< "\n\tGrade is : Not F ";
}
sum = sum + stdInstance[size].grade[j];
}
cout<<"\n\n\tSum For courses : "<<sum;
average=sum/6;
cout<<"\n\n\tAverage For courses : "<<average ;
if(average<50){
cout<< "\n\n\tNot Promoted ";
}else{
cout<< "\n\n\tPromoted";
}
size++;
}

Output of the Program

Program to read marks of 10 students for 4 subjects and compute and display total marks and status of each student

Saturday, 18 April 2015

| Logical C++ Programs Asked in Interviews | C++ / C Language Logical Programs | Simple Logical C programs | C Logical Programs with Answers | Logical C Programs

This is the simple C++ logical building program for beginners. The purpose of this type of programming is to give an idea of logical programming for beginners so they can learn programming in efficient ways. 

Source Code

#include<iomanip>
#include<iostream>
using namespace std;
int main(void)
{
int space=4;
char Values[7]={'1','2','3','4','5','6','7'};
for(int i=7; i>0; i--)
{
for (int j=0;j<i;j++)
{
cout<<Values[j];
cout<<" ";
}
if(i!=7)
{
cout<<setw(space);
space+=4;
}
for (int k=i;k>=0;k--)
{
cout<<Values[k];
cout<<" ";
}
cout<<endl;
}
system("pause");
return 0;
}

Output of the Program

 logical c programs asked in interviews

Find Even Numbers in Fibonacci Numbers Sequence | Numbers Sequence Fibonacci | How to find Even Fibonacci Numbers | Adding Fibonacci Numbers

This C++ program is about Fibonacci number sequence. The Program calculate the even number in Fibonacci series and also calculate their sum.

Source Code 

#include <iostream>
using namespace std;

/*Uzair*/

void welcomeScrean(void);
void evenFibonacci(void);

int main()
{
system ("color F0");

welcomeScrean();

evenFibonacci();
system("pause");
return 0;
}

void welcomeScrean(void){
cout<<"\a"
<<"\t*******************************************************"<<endl
<<"\t** **"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** **"<<endl
<<"\t** Find Even Fibonacci **"<<endl
<<"\t** **"<<endl
<<"\t** and there sum **"<<endl
<<"\t** **"<<endl
<<"\t** Program By LEP **"<<endl
<<"\t** **"<<endl
<<"\t*******************************************************"<<endl
<<endl
<<endl;
}

void evenFibonacci(void){


int num,count=0;
long previous =1, next =0,sum;
cout<<"\t Enter Your Number to Find Fibonacci ";
cin>>num;

cout<<"\n\t First "<<num<<" Even Fibonacci Numbers Are \n";

do{
if(previous%2==0){
cout<<"\n\t "<<previous<<" ";
count++;
}
sum = previous+next;
next = previous;
previous = sum;

}while(count!=num);

}

Output of the Program

how to find fibonacci numbers

Monday, 13 April 2015

C++ logical questions | C++ logical programming questions | logic building programs in c++ | programming logical thinking | logical questions in C

This is the logical C++ program in which we make a tree shape. The program represent a tree shape through logic . (this can also be done by hard code like simple cout.).

Source Code

#include <iostream>
#include <unistd.h>
using namespace std;

/*Uzair*/

void welcomeScrean(void);
void sleep();
void draw();
int main()
{
system ("color F0");
welcomeScrean();
draw();
return 0;

}

void welcomeScrean(void){
cout<<"\a"
<<"\t*******************************************************"<<endl
<<"\t*******************************************************"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** Draw By * **"<<endl
<<"\t** Program By LEP **"<<endl
<<"\t*******************************************************"<<endl
<<"\t*******************************************************"<<endl
<<endl
<<endl;
sleep();
}
void sleep(){
sleep(1);
for (int i=0 ; i<3;i++)
{
if (i==0)
{
cout<<"\tJust a min ";
}//END IF i
cout<<"\a.";
sleep(2);
}//
}
void draw(){
int size,i,j,k,k1,p;
cout<<"\n\n\tEnter A No : ";
cin>>size;
cout<<"\n\n\t";
for (i=0;i<size;i++)
{
k = size-i;
for (j=0;j<k;j++)
{
cout<<" ";
}
for (j=k;j<size;j++)
{
cout<<"*";
}
cout<<"I";
for (j=k;j<size;j++)
{
cout<<"*";
}
cout<<"\n\t";
}
k = size / 2;
k1 = k / 2;
p = size-k1;
for (i=0;i<k;i++)
{
for(j=0;j<p;j++)
{
cout<<" ";
}
for(j=0;j<k1;j++)
{
cout<<"*";
}
cout<<"I";
for(j=0;j<k1;j++)
{
cout<<"*";
}
cout<<"\n\t";
} 
}

Output of the Program  

logic building programs in c++

Monday, 6 April 2015

Write a C++ program that enter a students names and their grades and order them in ascending order according to names | how to sort names in C | name sorting in C

This is the simple C++ console program for sorting names. The program take the names and grade from the user and sort the names in ascending order.

Source Code 

#include <iostream>
#include<iomanip>
 #include<stdlib.h>
 #include<conio.h>
#include <algorithm>
using namespace std;
struct student
{
string name;
int id;
char grade[3];
};
//global variables
student stdInstance[50];
int size = 1;
//functions
void welcomeScrean(void);
void displayAllStudents(void);
void sortAndDisplayAllStudents(void);
void getStudentsWithGrade(void);

int main()
{
system ("color F0");
welcomeScrean();
int i=0,j=0;
cout<<"\t How many students do you want to enter " <<endl
<<"\t ======================================" <<endl
<<"\t You Endered : ";
cin>>j;
for(i;i<j;i++)
{
getStudentsWithGrade();
}

displayAllStudents();
sortAndDisplayAllStudents();
return 0;
}
void welcomeScrean(void){
cout<<"\a"
<<"\t*******************************************************"<<endl
<<"\t** **"<<endl
<<"\t** Welcome To **"<<endl
<<"\t** **"<<endl
<<"\t** Student Name Sorting and Data Entry **"<<endl
<<"\t** **"<<endl
<<"\t** Program **"<<endl
<<"\t** **"<<endl
<<"\t*******************************************************"<<endl
<<endl
<<endl;
cout<<"\t\t\t\n";
}
void displayAllStudents(void){
cout<<endl
<<"\t Students added with following data" <<endl
<<"\t ======================================" <<endl
<<endl;

for(int n=1;n<size;n++)
{
cout<<"\t "<<stdInstance[n].name<<endl;
}
}
void sortAndDisplayAllStudents(void){
string temp[size];

for ( int h = 0 ; h<size ; h++ )
{
temp[h]=stdInstance[h].name;
}
sort(temp, temp + size);
cout<< "\t Sorted names are"<<endl;
for(int m=1;m<size;m++)
{
cout<<endl<< "\t "<<temp[m]<<endl;
}
}
void getStudentsWithGrade(void){
//system("CLS");
cout<<endl
<<"\a"
<<"\t *******************************************************"<<endl
<<"\t ** Adding Student No # "<<size<<" **"<<endl
<<"\t *******************************************************"<<endl;

cout<<endl
<< "\n\tEnter Name : ";

cin>>stdInstance[size].name;

cout<< "\n\tEnter Grade TERMINATONG at \".\": ";
cin.getline(stdInstance[size].grade,3,'.');//taking grade

stdInstance[size].id = size;//settind customer id

size++;
cout<<endl
<<"\t Press Enter to continue . . ." <<endl;
getch();
}

Output of the Program

how to sort names in C | name sorting in c