Programming Languages
----------------------------------
Programming langauges are used to develop software applications (e.g. facebook, whatsapp, banking application etc)
There are many programming languages like C, C++, Java, Python and more.
Those who work on programming languages are called Software programmer or Software developer
C language
==========
-mother of all langauges
-Denies Ritche
-1972
Example (helloword.c)
#include <stdio.h>
void main() {
printf("hello aleena");
}
Example : what is variable ?
Example variable and datatype
#include<stdio.h>
void main(){
int age=12;
float percentage=76.99;
char name[] = {'a', 'l','e','e','n','a'};
printf("Name = %s", name);
printf("\n");
printf("Percentage = %f", percentage);
printf("\n");
printf("Age = %d", age);
}
Example of math
#include <stdio.h>
void main() {
int x=50;
int y=5;
int add=x+y;
int sub=x-y;
int mul=x*y;
int div=x/y;
printf("SUM %d",add);
printf("\n");
printf("SUB %d",sub);
printf("\n");
printf("MUL %d" ,mul);
printf("\n");
printf("DIV %d" ,div);
}
Example if
#include<stdio.h>
void main(){
int age = 20;
if(age>18) {
printf("She can vote");
}
else {
printf("Sorry, She cannot vote");
}
}
Example of nestedif
#include<stdio.h>
void main(){
int marks = 90;
if(marks<35) {
printf("You are failed");
}
else if(marks>60 && marks<75) {
printf("You got first class");
}
else if(marks>75) {
printf("You got distinction");
}
else {
printf("You are passed");
}
}
Example of switch
#include<stdio.h>
void main(){
int number=100;
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
}
Example of scanf
#include <stdio.h>
void main() {
int x;
printf("enter value for x ");
scanf("%d", &x);
printf("you entered %d", x);
}
LOOP;
- for loop
- while loop
- do while loop
FOR LOOP
for(INITIALIZATION; CONDITION;INCREMENT){
}
Example of for loop
#include <stdio.h>
void main() {
int i;
for(i=1; i<=5; i++) {
printf("Hello Aleena \n");
}
for(i=3; i>=1; i--) {
printf("Hello Aliza \n");
}
}
Example of while loop
#include <stdio.h>
void main() {
int i=1;
int j=3;
while(i<=5) {
printf("Hello Aleena \n");
i++;
}
while(j>=1) {
printf("Hello Aliza \n");
i--;
}
}
Example of do while
#include <stdio.h>
void main() {
int i=1;
do {
printf("Hello Aleena \n");
i++;
}
while(i<=5);
}
Example of array
#include <stdio.h>
void main()
{
int marks[3] = {85, 56, 90};
char name[6] = {'a', 'l', 'e', 'e', 'n', 'a'};
printf("%d \n", marks[0]);
printf("%d \n", marks[1]);
printf("%d \n", marks[2]);
printf("%c", name[0]);
printf("%c", name[1]);
printf("%c", name[2]);
printf("%c", name[3]);
printf("%c", name[4]);
printf("%c", name[5]);
}
Example of array with for loop
#include <stdio.h>
void main()
{
int marks[3] = {85, 56, 90};
char name[6] = {'a', 'l', 'e', 'e', 'n', 'a'};
for(int i=0; i<3; i++) {
printf("%d \n", marks[i]);
}
for(int i=0; i<6; i++) {
printf("%c", name[i]);
}
}
Example of array to print reverse
#include <stdio.h>
void main()
{
int marks[3] = {85, 56, 90};
for(int i=2; i>=0; i--) {
printf("%d \n", marks[i]);
}
}
Example find the sum of all element array
#include <stdio.h>
void main()
{
int marks[3] = {85, 56, 90};
int sum = 0;
for(int i=0; i<3; i++) {
sum = sum + marks[i];
}
printf("Sum = %d", sum);
}
Example of constant
#include <stdio.h>
void main() {
const char name[10] = "Aleena";
const int hands = 2;
const int legs = 2;
float height = 4.11;
int weight = 30;
printf("Name = %s \n", &name);
printf("Hands = %d \n", hands);
printf("Legs = %d \n", legs);
printf("Height = %f \n", height);
printf("Weight = %d \n \n", weight);
printf(".........After one year.......\n");
height = 5.2;
weight = 35;
printf("Name = %s \n", &name);
printf("Hands = %d \n", hands);
printf("Legs = %d \n", legs);
printf("Height = %f \n", height);
printf("Weight = %d \n \n", weight);
}
Function: It is used to divide large program logic to small blocks.
Syntax,
return_type function_name(parameter list) {
}
Example: Function
#include <stdio.h>
int dateOfBirth();
void main()
{
int dob = dateOfBirth();
printf("date of birth = %d", dob);
}
int dateOfBirth()
{
return 2008;
Example of fun}
#include <stdio.h>
void author();
int add(int i, int j);
int sub(int i, int j);
int div(int i, int j);
int mul(int i, int j);
int min(int i, int j);
int max(int i, int j);
void main()
{
void author();
int sm = add(4, 5);
printf("Addition = %d \n", sm);
int sb= sub(10, 5);
printf("Substraction = %d \n", sb);
int dv= div(10, 5);
printf("Division = %d \n", dv);
int ml= mul(10, 5);
printf("Multiplication = %d \n", ml);
int mn= min(10, 5);
printf("Min = %d \n", mn);
int mx= max(10, 5);
printf("Min = %d \n", mx);
}
void author()
{
printf("Aleena Bagwan");
}
int add(int i, int j)
{
int result = i + j;
return result;
}
int sub(int i, int j)
{
int result = i + j;
return result;
}
int mul(int i, int j)
{
int result = i * j;
return result;
}
int min(int i, int j)
{
if(i<j)
return i;
else
return j;
}
int max(int i, int j)
{
if(i>j)
return i;
else
return j;
}
Pointer: It holds the address of a variable
Example: Pointer
#include<stdio.h>
void main ()
{
int x = 55;
printf("Value
#include <stdio.h>
void aleena();
void aliza();
/* global variable declaration */
int fatherAge = 40;
void main () {
aleena();
aliza();
}
void aleena() {
/* local variable declaration */
int aleenaAge = 12;
printf("Aleena age = %d \n", aleenaAge);
printf("Aleena father age = %d \n", fatherAge);
}
void aliza() {
/* local variable declaration */
int alizaAge = 9;
printf("Aliza age = %d \n", alizaAge);
printf("Aleena father age = %d \n", fatherAge);
}
Example: Inbuild math functions
#include<stdio.h>
#include <math.h>
void main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
}
File I/O : It is the concept of reading and writing data to and from file
Example: It write data to file
#include <stdio.h>
void main() {
FILE *fp;
fp = fopen("D:\\test.txt", "w+");
//fprintf(fp, "I am Aleena\n");
fputs("I am Aleena\n", fp);
fclose(fp);
}
Comments
Post a Comment