Basic C programs Collection For NewBies - Part 1

Posted by Suraj Singh on December 04, 2018 · 16 mins read
Hello Guyz,



Welcome Again To My blog. readers, Today I am going To Share Some Really Very Basic C Program Code Written, When I was newbie In C Programming. I am Sharing These Codes, Because When i was new, these small codes really help me in Understand C Programming. I hope, these code will also help to new C Programming guys and Those who are just starting learning.

1. Fahrenheit To Celsius Converting Function

// ===============================================================================
// ===============x Fahrenheit To Celsius Converter x----------------------------
// ===============================================================================
// demonstrating cin, newline function
//
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++

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


int main () {
int ftemp, degree;
cout << "\n [+] Please Enter Fahrenheit Temperature : ";
cin >> ftemp;
degree=(ftemp-32)*5/9;
cout << "\n [+] Your Temperature in Celesius : " << degree << endl;
// formula Fahrenheit=(ftemp-32)*5/9
}

2. Triangle Herons Formula Function

// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C
//
// To Perform Triangle Herons Formula
//
#include <stdio.h>
#include <math.h>


int main(){
float a,b,c,d,s, area;
printf("[+] Enter Sides Of Triangles : A B C ");
scanf("%f %f %f", &a, &b, &c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("[+] Area Of Triangle is : %f\n", area);
}


3. Area of Rectangle Function

// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C
//
// To FInd Area Of Rectagle


#include<stdio.h>

int main(){
int l, b, area;
printf("\n%s", "[+] Enter Length & Width Of Rectangle : ");
scanf("%d %d",&l,&b);
area = l * b;
printf("\n[+] Area Of Rectangle : %d \n", area);

}


4. Example of Setw Function

// ===================================================================================
// -------------------------x Example Of setw() Functions x---------------------------
// ===================================================================================
// demonstrates setw manipulator
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++


#include <iostream>
#include <iomanip>
using namespace std;

int main(){
long value1=1000000,value2=100000,value3=10000, value4=1000;

cout << "[+] Value 1 " <<setw(8) << value1 << endl
<< "[+] Value 2 " <<setw(8) << value2 << endl
<< "[+] Value 3 " <<setw(8) << value3 << endl
<< "[+] Value 4 " <<setw(8) << value4 << endl;
}


5. Example Of Square Root Function

// =============================================================================
// -------------------------x Square root of integers x------------------------
// =============================================================================
// demonstrate sqrt() library function
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++

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

int main(){
double number, answer;
cout << "\n [+] Enter Number For Finding Square Root : " ;
cin >> number;
answer=sqrt(number);
cout << "\n [+] Your Square root is : "<< answer <<endl;
}


6. Cubelist Function

// ========================================================================
// -------------------x cubelist.cpp x-------------------------------------
// ------------------------------------------------------------------------
// demonstrating For loop with setw() functions
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++


#include <iostream>
#include <iomanip>
using namespace std;

int main(){
unsigned long times;
long cube;
cout << "\n [+] Cube list From 1 to : ";
cin >> times;
cout <<"\n\n|----------------------------|"<< endl;
for(int i=0; i<=times; i++){
cube=i*i*i;
cout << " | "<< setw(6) << i << " | "<<setw(15) << cube << " | "<< endl;
}
}


7. Area Of Circle Function

// ========================================================================
// ====================x Area of Circle x==================================
// ========================================================================
//demonstrating float point variables

#include<iostream>
using namespace std;
int main(){
double radius, area;
const float PI = 3.14159;
cout << " \n [+] Enter Radius of Circle : ";
cin >> radius;
area = PI * radius * radius;
cout << "\n [+] Area Of Circle is : "<< area << endl;
// formula 3.14159*radius*radius
}


8. Multi Time String Printer Function

// =======================================================================
// --------------------x Multitime String Printer x-----------------------
// =======================================================================
// demonstrating for loop with Printing Multi lines
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++



#include<iostream>
using namespace std;

int main(){
string st; // used for storing line character
unsigned long times=1; // Used For storing Line number
cout << "\n[+] Please Enter The String For Printing : " ;
getline(cin , st); // Using Getline Because cin only support one word
cout << "\n[+] Please Enter Numbers For Line ; ";
cin >> times;

if(times>1){
for(int i=0;i<=times;i++){
cout << st << endl;
}
}
else if (times<1){
cout << "[error] Bad Value ";
cout << times << st << endl;
}
}


9. Celsius To Fahrenheit Converter Function

// ===============================================================================
// ===============x Celsius To Fahrenheit Converter x----------------------------
// ===============================================================================
// demonstrate cin, newline


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


int main () {
string a;
int ftemp, degree;
cout << "\n [+] Please Enter Celsius Temperature : ";
cin >> degree;
//degree=(ftemp-32)*5/9;
ftemp=(degree*9/5)+32;
cout << "\n [+] Your Temperature in Fahrenheit : " << ftemp << endl;
cin >> ;

// formula Fahrenheit=(ftemp-32)*5/9
}

10. Temperature To Fahrenheit Function

// This Program Will Change Temperature value In Fahrenheit
// demonstrate cin, newline
// Author : Suraj Singh Bisht
// Email : surajsinghbisht054@gmail.com
// Code : C++


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


int main () {
int ftemp, degree;
cout << "\n [+] Please Enter Fahrenheit Temperature : ";
cin >> ftemp;
degree=(ftemp-32)*5/9;
cout << "\n [+] Your Temperature in Celesius : " << degree << endl;
// formula Fahrenheit=(ftemp-32)*5/9
}


Please Forgive me, If There Is any type of mistake in these code because I Wrote These codes, When I was new in C programming. Actually, I am publishing these code Just to provide some useful examples of new C Programmer.


I hope, this Example Codes Are Helpful For You.
Have A Nice Day.