sample code to calculate area of a circle
Allah Ditta wants a program that calculates and displays the area of a wheel given its radius. Use the constant value 3.14159 for π.
//area of wheel
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating vars to store data
int i;
float j = 3.14159;
//get radius from user
printf("Enter the radius of circle:");
scanf("%d", &i);
//displaying and calculating area
printf("The area of wheel is: %f", (i*i)*j);
//for disabling automatic close off of program after execution of program
_getch();
}//end main
Wednesday, 24 December 2014
Posted by Unknown
Coding sample tip calculator
Allah Ditta wants a program that calculates and displays the amount he should tip a waiter at a restaurant. The program should subtract any cold drink charges from the total bill and then calculate the tip (using a percentage) on the remainder.
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating integers to store data
int i;
int j;
int k;
//Taking input from user:
//total bill
printf("Enter the total bill: ");
scanf("%d", &i);
//tip percentage
printf("Enter the percentage amount of tip: ");
scanf("%d", &j);
//cold drinks
printf("Enter the cost of cold drinks: ");
scanf("%d", &k);
//calculating Displaying result
printf("\n \nThe tip is: %d ", ((i-k)*j)/100);
//for disabling automatic close off of program after execution of program
_getch();
}
these are coding samples to help you understand more and more.
Arithmetic Operators, Largest & Smallest Value And Odd & Even Numbers
//Arithmetic operation on three numbers
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating var for storing values
int i;
int j;
int k;
//take input from user
printf("Input three different integers \n");
scanf("%d %d %d", &i, &j, &k);
//calculating and displaying results of
//Adition
printf("\nThe sum is %d \n", i + j + k);
//average
printf("The average is %d \n", (i + j + k)/3);
//product
printf("The product is %d", i * j * k);
//smallest and largest no.
//first number
if (i < j & j<= k)
{
printf("\n \nThe smallest is %d", i);
} /*end if*/
if (j > i & j > k)
{
printf("\n \nThe largest is %d", i);
} /*end if*/
//2nd number
if (j < k & j <= i)
{
printf("\nThe smallest is %d", j);
} /*end if*/
if (j > k & j >= i)
{
printf("\nThe Largest is %d", j);
} /*end if*/
//3rd number
if (k < j & j<= i)
{
printf("\nThe smallest is %d", k);
} /*end if*/
if (k > j & j >= i)
{
printf("\nThe Largest is %d", k);
} /*end if*/
//chk and display even and odd no
//first no.
if ((i%2) == 0)
{
printf("\n \n%d is even", i);
}
else
{
printf("\n \n%d is odd", i);
}
//2nd no.
if (j%2 == 0)
{
printf("\n \n%d is even", j);
}
else
{
printf("\n \n%d is odd", j);
}
//3rd no.
if (k%2 == 0)
{
printf("\n \n%d is even", k);
}
else
{
printf("\n \n%d is odd", k);
}
//for disabling automatic close off of program after execution of program
_getch();
}//end
#include<stdio.h>;
#include<conio.h>;
//executing main program
void main()
{
//creating var for storing values
int i;
int j;
int k;
//take input from user
printf("Input three different integers \n");
scanf("%d %d %d", &i, &j, &k);
//calculating and displaying results of
//Adition
printf("\nThe sum is %d \n", i + j + k);
//average
printf("The average is %d \n", (i + j + k)/3);
//product
printf("The product is %d", i * j * k);
//smallest and largest no.
//first number
if (i < j & j<= k)
{
printf("\n \nThe smallest is %d", i);
} /*end if*/
if (j > i & j > k)
{
printf("\n \nThe largest is %d", i);
} /*end if*/
//2nd number
if (j < k & j <= i)
{
printf("\nThe smallest is %d", j);
} /*end if*/
if (j > k & j >= i)
{
printf("\nThe Largest is %d", j);
} /*end if*/
//3rd number
if (k < j & j<= i)
{
printf("\nThe smallest is %d", k);
} /*end if*/
if (k > j & j >= i)
{
printf("\nThe Largest is %d", k);
} /*end if*/
//chk and display even and odd no
//first no.
if ((i%2) == 0)
{
printf("\n \n%d is even", i);
}
else
{
printf("\n \n%d is odd", i);
}
//2nd no.
if (j%2 == 0)
{
printf("\n \n%d is even", j);
}
else
{
printf("\n \n%d is odd", j);
}
//3rd no.
if (k%2 == 0)
{
printf("\n \n%d is even", k);
}
else
{
printf("\n \n%d is odd", k);
}
//for disabling automatic close off of program after execution of program
_getch();
}//end
Posted by Unknown
Tag :
& Smallest,
Arithmetic,
C,
c++,
code,
coding,
even,
Largest,
Numbers,
odd,
Operators,
program,
project,
project c++(basic),
Value
how to sort a 2d array in C++
This program demonstrate how to sort 2d array in ascending order. The program explain it in 3x3 array you could change the values as required to meet your requirement
//prog for 2d array sort
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[3][3];
int temp;
cout << "Enter 9 elements\n\n";
//Enter Element
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << "Enter Elements no: " << i+j+1 << " : ";
cin >> arr[i][j];
}
}
cout << endl;
//print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
// run the loop double times the number of row or column which ever is greater
for (int k = 0; k <6; k++){
//arrage elemnt in ascending order in a rows
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
if(arr[i][j] > arr[i+1][j]){
temp = arr[i][j];
arr[i][j] = arr[i+1][j];
arr[i+1][j] = temp;
}
}
//compare the last element of each row with first element of its next row
for (int i = 0; i < 2; i++){
if(arr[2][i] > arr[0][i+1]){
temp = arr[2][i];
arr[2][i] = arr[0][i+1];
arr[0][i+1] = temp;
}
}
}
}
cout << endl << endl;
// print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
_getch();
}
This program arrange the rows value in in itself and then compare the last value of a row with the first value of the next row. And if it is greater it swaps them and again arrange the the row. The program continue to do it unless all the value get sorted. Go through the program and you will get it how it works
//prog for 2d array sort
#include<iostream>
#include<conio.h>
using namespace std;
void main() {
int arr[3][3];
int temp;
cout << "Enter 9 elements\n\n";
//Enter Element
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << "Enter Elements no: " << i+j+1 << " : ";
cin >> arr[i][j];
}
}
cout << endl;
//print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
// run the loop double times the number of row or column which ever is greater
for (int k = 0; k <6; k++){
//arrage elemnt in ascending order in a rows
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
if(arr[i][j] > arr[i+1][j]){
temp = arr[i][j];
arr[i][j] = arr[i+1][j];
arr[i+1][j] = temp;
}
}
//compare the last element of each row with first element of its next row
for (int i = 0; i < 2; i++){
if(arr[2][i] > arr[0][i+1]){
temp = arr[2][i];
arr[2][i] = arr[0][i+1];
arr[0][i+1] = temp;
}
}
}
}
cout << endl << endl;
// print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout << endl;
}
_getch();
}
This program arrange the rows value in in itself and then compare the last value of a row with the first value of the next row. And if it is greater it swaps them and again arrange the the row. The program continue to do it unless all the value get sorted. Go through the program and you will get it how it works
coding for reversing a number and then printing it in c
#include<stdio.h>
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
Coding program to print right triangle through odd number in C through asteriks.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
Posted by Unknown
Program, code to multiply 2 matrix using 1-d array in c++
#include<iostream>
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
Posted by Unknown