Author Archives: Sajjad Baghaee

C Programming Tutorial – 15 – Extract a character from an array + compare it with a character :

Published by:

Note:

  • //lastname[3] : Extract the 4th (not the 3rd) character of array lastname.
  • //lastname[3] > ‘M’   : compare the 4th character with character M.

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char lastname[20];
printf(“Enter your last name ? \n”);
scanf(” %s”, lastname);
if (lastname[3] > ‘M’) { // character extract and character comparison.
printf(“You are in team Blue \n”);
} else {
printf(“You are in team Red \n”);
}

return 0;

}

C Programming Tutorial – 14 – if – else if :

Published by:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

float agrade,bgrade,cgrade;
printf(“Please enter you 3 grades. \n”);
scanf(” %f”, &agrade); // You should not use \n in scanf. !!!!
scanf(” %f”, &bgrade);
scanf(” %f”, &cgrade);
float averagegarde = (agrade+bgrade+cgrade)/3;
printf(“Your average grade is : %.2f \n”,averagegarde);
if (averagegarde>=90){ // Use of else if
printf(“Your FINAL grade is A. \n”);
} else if (averagegarde>= 80) {
printf(“Your FINAL grade is B. \n”);
} else if (averagegarde>= 70){
printf(“Your FINAL grade is C. \n”);
} else {
printf(“You could not pass the course ! \n”);
}
if ((agrade>=90) && (bgrade>=80) && (cgrade>=70)){       // In this part we compare more 1 values and use logic syntax like && ||
printf(“You can enter to Final EXAM.\n”);
} else
{
printf(“You cannot enter to Final exam !\n”);}

return 0;

}

C Programming Tutorial – 13 – if :

Published by:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int Yourage;
char gender;
printf(“How old are you?\n”);
scanf(” %d”,&Yourage);
printf(“what is your gender? (m/f)\n”);
scanf(” %c”,&gender);

if(Yourage>=18)
{
if(gender == ‘m’) // compare the char value
{
printf(“welcome dude !\n”);
}

if(gender == ‘f’)
{
printf(“welcome m’lady !\n”);
}

}

if(Yourage <18)
{
printf(“You can not visit this website.\n”);
}

return 0;

}

C Programming Tutorial – 12 – change the property of a variable:

Published by:

Note:

  • //Use (float) or (int) or … before any variable to change that variable to float or int or …:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int PriceOfPopcorn=100,sales=20,WorkDay=7;
float profitPerday;
profitPerday = ((float)PriceOfPopcorn*(float)sales)/(float)WorkDay;
printf(“profit per day is = $%.2f\n”,profitPerday);

return 0;

}

C Programming Tutorial – 10 – define multiple variable at the same time :

Published by:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

float aa,bb,cc, balance;                                          // we can define multiple variable at the same time
aa=bb=cc=100.00;                                               // we can give a value to multiple variable at the same time
printf(“aa=%f , bb=%f ,cc=%f\n”, aa,bb,cc);

return 0;

}

C Programming Tutorial – 9 – int or float ? :

Published by:

Note:

  • // int just shows the integer part of the number
  • // float shows the integer and decimal part of the number:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int a=86;                                            // Int just shows the integer part of the number
int b=21;
printf(“%d\n”, a/b);
float c=86.0;                                     // float shows the integer and decimal part of the number
float d=21.0;
printf(“%f\n”, c/d);

return 0;

}

C Programming Tutorial – 8 – Get data from keyboard scanf():

Published by:

Note:

  • // scanf() :  get data from keyboard or user.
  • // Use & before all of the variables, except Array variables.

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char firstname[20];
char lovename[20];
int NumberOfBaby;
printf(“What is your name?\a\n”);
scanf(“%s”,firstname);                                                           // scanf get data from keyboard or user
printf(“What is your love name?\a\n”);
scanf(“%s”,lovename);
printf(“How many baby you will have?\a\n”);
scanf(“%d”, &NumberOfBaby);                     // Use & before all of the variables except Array variables.
printf(“%s and %s are in love. They will have %d babies\n”, firstname,lovename ,NumberOfBaby );

return 0;

}

C Programming Tutorial – 7 – Generate a header file and use it:

Published by:

Note:

  • //make sajjadinfo.h header file and use content of code 1.
  • add #include “sajjadinfo.h”    to your code.
  • “” means just look at current folder.

 

Code 1:    sajjadinfo.h
—————————

#define MYNAME “SB”
#define AGE 33

 

Code 2:
—————————

#include <stdio.h>
#include <stdlib.h>

#include “sajjadinfo.h”     // we create a Header file . “” means just look at current folder.

int main()
{

int HalfOfMyAge=(AGE/2) ;                              // we used sajjadinfo.h file constant data (AGE)
printf(“Half of age of %s is %d \n”, MYNAME , HalfOfMyAge);  // we used sajjadinfo.h file constant data (MYNAME)

return 0;

}

C Programming Tutorial – 6 – replace a string with other sting – strcpy():

Published by:

Note:

  • // replace a string with other sting:
    strcpy(food,”bacon”);

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char food[]=”tuna”;
printf(“The best food is %s\n”, food);
strcpy(food,”bacon”);                 // replace a string with other sting
printf(“The best food is %s\n”, food);

return 0;

}

C Programming Tutorial – 5 – Define an Array + replace a character in an array :

Published by:

Note:

  • //Define array:
    char name[15]= “Sajjad Baghaee”;
  • // replacing a character in an array : 
    name[2]= ‘C’;
  • Array start by 0 so S=0 a=1  j=2  j=3  a =4  d=5 =6  B=7  a=8  g=9  h=10  a=11  e=12  e=13

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char name[15]= “Sajjad Baghaee”;
printf(“This is %s \n”, name);
name[2]= ‘C’;                  // replacing the 1st character “j” in array “Sajjad Baghaee”  with “C”
name[3]=’c’;                   // replacing the 2nd character “j” in array “Sajjad Baghaee”  with “c”
printf(“This is %s\n”,name);

return 0;

}