Posts

Patterns in C++ Language by MistarAV

List of Source Code Print triangle using *, digits and characters Print inverted triangle using * and digit Code to Print pyramid Code to Print reverse pyramid Code to Print Pascal's traingle Code to Print Floyd's triangle Programs to print triangles using *, numbers and characters Patterns code in C Programming language                        Click here .                Example 1: Program to print half pyramid using * * * * * * * * * * * * * * * * Source Code #include <iostream> using namespace std; int main() { int rows; cout << "Enter number of rows: "; cin >> rows; for(int i = 1; i <= rows; ++i) { for(int j = 1; j <= i; ++j) { cout << "* "; } cout << "\n"; } return 0; } Example 2: Program to print half pyramid a using numbers 1 1 2 1 2 3 1 2 3 4 1 2 3

Pattern in C Language By MistarAV

C Examples Half pyramid of * Half pyramid of numbers Half pyramid of alphabets Inverted half pyramid of * Inverted half pyramid of numbers Full pyramid of * Full pyramid of numbers Inverted full pyramid of * Pascal's triangle Floyd's triangle Example 1: Half Pyramid of * * * * * * * * * * * * * * * * C Program Copy #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("\n"); } return 0; } Example 2: Half Pyramid of Numbers 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 C Program #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= row

Write a C program on Newton forward interpolation method by MistarAV

#include<stdio.h> #include<math.h> int fact(int); void main() { float arr[10][11],x,h,p,y,px=1; int i,j,n,ch=30; printf("\nEnter the number of data:"); scanf("%d",&n); printf("\nEnter the data"); for(i=0;i<n;i++) { printf("X%d=",i+1); scanf("%f",&arr[i][0]); printf("Y%d=",i+1); scanf("%f",&arr[i][1]); } //Forming difference table. for(j=2;j<=n;j++) for(i=0;i<n-1;i++) arr[i][j]=arr[i+1][j-1]-arr[i][j-1]; //Printing table printf("\nDifference table is:-"); printf("\n\tx\tY"); for(i=0;i<=n-2;i++) printf("\t%c^%dY",ch,i+1); for(i=0;i<n;i++) {printf("\n"); for(j=0;j<n+1-i;j++) {printf("\t%.4f",arr[i][j]); } } //Take the value of x for f(x) printf("\nEnter the value x for function f(x):"); scanf("%f",&x); //Calculate the value of f(x) for x h=arr[1][0]-arr[0][0]; p=(x-arr[0][0])/h; y=arr[0][1]; for(i=1;i<n

Find the root of the equation using the Bisection method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) cos(x) - x * exp(x) void main() { float x0, x1, x2, f0, f1, f2, e; int step = 1; clrscr(); /* Inputs */ up: printf("\nEnter two initial guesses:\n"); scanf("%f%f", &x0, &x1); printf("Enter tolerable error:\n"); scanf("%f", &e); /* Calculating Functional Value */ f0 = f(x0); f1 = f(x1); /* Checking whether given guesses brackets the root or not. */ if( f0 * f1 > 0.0) { printf("Incorrect Initial Guesses.\n"); goto up; } /* Implementing Bisection Method */ printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do { x2 = (x0 + x1)/2; f2 = f(x2); printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2); if( f0 * f2 < 0) { x1 = x2; f1 = f2; } else { x0 = x2; f0 = f2; } step = step + 1; }while(fabs(f2)>e); printf("\nRoot is: %f", x2); get

Find the root of the equation using the Regula-Falsi method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) x*log10(x) - 1.2 int main() { float x0, x1, x2, f0, f1, f2, e; int step = 1; clrscr(); /* Inputs */ up: printf("\nEnter two initial guesses:\n"); scanf("%f%f", &x0, &x1); printf("Enter tolerable error:\n"); scanf("%f", &e); /* Calculating Functional Values */ f0 = f(x0); f1 = f(x1); /* Checking whether given guesses brackets the root or not. */ if( f0*f1 > 0.0) { printf("Incorrect Initial Guesses.\n"); goto up; } /* Implementing Regula Falsi or False Position Method */ printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do { x2 = x0 - (x0-x1) * f0/(f0-f1); f2 = f(x2); printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2); if(f0*f2 < 0) { x1 = x2; f1 = f2; } else { x0 = x2; f0 = f2; } step = step + 1; }while(fabs(f2)>e); printf("\n

Write a C program on Cubic spline interpolation method by MistarAV

#include<stdio.h> #include<conio.h> #include<math.h> void main(){ clrscr(); char choice='y'; int n,i,j,k; float h[10],a,b,c,d,sum,s[10]={0},x[10],F[10],f[10],p,m[10][10]={0},temp; printf("No of samples? "); scanf("%d",&n); printf("\nEnter all sample points: "); for(i=0;i<n;i++) scanf("%f%f",&x[i],&f[i]); for(i=n-1;i>0;i--){ F[i]=(f[i]-f[i-1])/(x[i]-x[i-1]); h[i-1]=x[i]-x[i-1]; } //*********** formation of h, s , f matrix **************// for(i=1;i<n-1;i++){ m[i][i]=2*(h[i-1]+h[i]); if(i!=1){ m[i][i-1]=h[i-1]; m[i-1][i]=h[i-1]; } m[i][n-1]=6*(F[i+1]-F[i]); } //*********** forward elimination **************// for(i=1;i<n-2;i++){ temp=(m[i+1][i]/m[i][i]); for(j=1;j<=n-1;j++) m[i+1][j]-=temp*m[i][j]; } //*********** backward substitution *********// for(i=n-2;i>0;i--){ sum=0; for(j=i;j<=n-2;j++) sum+=m[i][j]*s[j]; s[i]=(m[i][n-1]-sum)/m[i][i]; } while(choice=='y'){ printf("\nEnte

Write a program to convert uppercase to lowercase letters by MistarAV

#include <iostream> #include <cstring> using namespace std; int main() { char s[20]; int i; cout<<"Enter the String in uppercase: "; cin>>s; for(i=0;i<=strlen(s);i++) { if(s[i]>=65 && s[i]<=92) { s[i]=s[i]+32; } } cout<<"The entered string in lowercase: "<<s; return 0; } Output Enter the String in uppercase: A The entered string in lowercase: a

Write a program to swap two variable values using functioncall by value by MistarAV

#include<iostream> using namespace std; void swap(int,int); int main() { int num1,num2; cout<<"\n Enter First Number : "; cin>>num1; cout<<"\n Enter Second Number : "; cin>>num2; cout<<"\n Before Swapping the Value : \n"<<" "<<num1<<"\t"<<num2<<"\n"; swap(num1,num2); cout<<"\n Outside Function After Swapping the Value : \n"<<" "<<num1<<"\t"<<num2<<"\n"; } void swap(int num1,int num2) { int num3; num3=num1; num1=num2; num2=num3; cout<<"\n Inside Function After Swapping the Value : \n"<<" "<<num1<<"\t"<<num2; } Output Enter First Number : 2 Enter Second Number : 8 Before Swapping the Value : 2 8 Inside Function After Swapping the Value : 8 2 Outside Function After Swapping the Value : 2 8

Write a program to print Fibonacci series:Eg: 0, 1, 1, 2, 3, 5, 8……… by MistarAV

#include <iostream> using namespace std; int main() { int n, t1 = 0, t2 = 1, nextTerm = 0; cout << "Enter the number of terms: "; cin >> n; cout << "Fibonacci Series: "; for (int i = 1; i <= n; ++i) { if(i == 1) { cout << t1 << ", "; continue; } if(i == 2) { cout << t2 << ", "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; cout << nextTerm << ", "; } return 0; } Output Enter the number of terms: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Write a Program to read two arrays(A & B) values and storeaddition of elements into array-C by MistarAV

#include<iostream> using namespace std; #define MAX 20 void readArray(int a[],int size) { int i; for(i=0;i< size;i++) { cout<<"Enter "<<i+1<<"element : "; cin>>a[i]; } } void printArray(int a[],int size) { int i; for(i=0;i < size; i++) cout<<" "<<a[i]; } void addArray(int a[],int b[],int c[],int size) { int i; for(i=0; i< size;i++) c[i]=a[i]+b[i]; } int main() { int A[MAX],B[MAX],ADD[MAX],SUB[MAX]; int i,n; cout<<"\nEnter size of an Array :"; cin>>n; cout<<"\nEnter elements of Array 1:\n"; readArray(A,n); cout<<"\nEnter elements of Array 2:\n"; readArray(B,n); /* add Arrays*/ addArray(A,B,ADD,n); cout<<"\nArray elements after adding :\n"; printArray(ADD,n); cout<<"\n\n"; return 0; } Output Enter size of an Array :2 Enter elements of Array 1: Enter 1element : 4 Enter 2element : 5 Enter elements of Array 2: Enter 1element :