C Language

 C Language

  • Default Program Layout
#include <stdio.h>

int main()
{



return 0;
}

Note: Put semi column on each line between the brackets.


  • Usage of % on few letters
%c = Single Char

%d = Integer Char
  
%f = decimal 

%s = charcter 

  • How Variable Works
  • char characterName[] = "Tom";
int characterAge = 67;
printf("There one was a man named %s\n", charcterName);
printf("he was %d years old.\n", charcterAge);

/*If Age need to change in between*/

characterAge=30
printf(" He really liked the name %s\n", characterName);
printf(" but did not like being %d.\n", charcterAge);

  • Data Types in C
int age = 40;
double gpa = 3.6;
char grade = 'A';
char phrase [ ] =  " Learning C Language";


All below statements works similar
  • printf("my fav %s is %f", "number", 500); /// check///
  • printf("%d", 500);
  • printf(" My fav number is %d, 500);

\*
To run a program in Visual code use the below commands after saving by CTRL+S
 go to the new terminal

gcc main.c
.\a.exe
*\

First Program: Sum of two number


#include<stdio.h>

int main()
{
    int a,b;
    printf("Enter first number\n");
    scanf("%d",&a);

    printf("Enter first number b\n");
    scanf("%d",&b);

    printf("Sum of two number is %d meters.", a+b);
    return 0;
}

scanf("%d",&a); Learn this format

Else if else statement and program

#include<stdio.h>

int main(){
    int num;
    printf(" Enter your number\n");
    scanf("%d",&num);
    if (num==1)
    {
        printf("Your number is 1\n");
    }
    else if (num ==2)
    {
        printf("Your number is 2\n");  
    }
    else if (num ==3)
    {
        printf("Your number is 3\n");
    }
    else
    {
        printf("it is not 1,2, or 3\n");
    }
      
    
    return 0;
}

Note: else if keep active until the last condition will be written after else command.


One Line Command

#include<stdio.h>

int main(){
    int a;
    printf("Enter a\n");
    scanf("%d", &a);
    // One liner conditional 
    (a<5)? printf("A is less than 5"): printf("A is not less than 5");
    return 0;
}

Switch with break

#include<stdio.h>

int main(){
    int rating;
    printf("Enter your ratiing (1-5)\n");
    scanf("%d", &rating);
    switch (rating)
    {
        case 1:
        printf("Your rating is 1\n");
        break;

        case 2:
        printf("Your rating is 2\n");
         break;

        case 3:
        printf("Your rating is 3\n");
         break;

        case 4:
        printf("Your rating is 4\n");
         break;

        case 5:
        printf("Your rating is 5\n");
         break;

        default :
        printf(" Invalid rating");
         break;
    }
    return 0;
}

Always use mid bracket after the If Else command
#include<stdio.h>

int main(){
    int a = 10;
    if (a=11)
    {
    printf("I am 11\n");
    printf("I am 11\n");}
    
    else{
    printf("I am not 11\n");
    }

        return 0;
}

How to use or gate with else statement

#include<stdio.h>

int main(){
    int physics, chemistry, maths;
    float total;
    printf("Enter Physics number\n");
    scanf("%d",&physics);
    printf("Enter Chemistry number\n");
    scanf("%d",&chemistry);
    printf("Enter maths number\n");
    scanf("%d",&maths);

    total= (maths+physics+chemistry)/3;

    if ((total<40) || physics<33|| chemistry<33 || maths<33)
    {
        printf("Your percentage is %f and you are fail\n", total);
    }
    else
    {
        printf("Your percentage is %f and you are Pass\n", total);
    }
    
    return 0;
}


Income tax program using if and gates

#include<stdio.h>

int main(){
    float tax = 0, income;
    printf("Enter your income\n");
    scanf("%f", &income);

    if(income>=250000 && income<=500000){
        tax = tax + 0.05 * (income - 250000);
    }
    
    if (income >= 500000 && income <= 1000000)
    {
        tax = tax + 0.20 * (income - 500000);
    }

    if (income >= 1000000)
    {
        tax = tax + 0.30 * (income - 1000000);
    }
    
    printf("Your net income tax to be paid by 26th of this month is %f\n", tax);

    return 0;
}

Character input how to check lower case

#include<stdio.h>

int main(){
    // 97-122 = a-z ASCII Values
    char ch;
    printf("Enter the character\n");
    scanf("%c", &ch);
    if(ch<=122 && ch>=97){
        printf("It is lowercase");
    }
    else{
        printf("It is not lowercase");
    }
    return 0;
}


Find the greatest number among 4 input digits by user

#include<stdio.h>

int main(){
    int a,b,c,d;
    int largest;
    printf("Enter 1st number\n");
    scanf("%d",&a);
    printf("Enter 2nd number\n");
    scanf("%d",&b);
    printf("Enter 3rd number\n");
    scanf("%d",&c);
    printf("Enter 4th number\n");
    scanf("%d",&d);

    if
    ((a>b)&&(a>c)&&(a>d))
    {
        printf("Greatest number is %d\n", a);

    }
   if  ((b>a)&&(b>c)&&(b>d)){
       printf("Greatest number is %d\n",b);
   }
   if ((c>a)&&(c>b)&&(c>d)){
       printf("Greatest number is %d\n",c);
   }
   if ((d>a)&&(d>b)&&(d>c)){
       printf("Greatest number is %d\n",d);
   }

    return 0;
}

Loops

Three types of loop
1: While

Print number up to 10

 #include<stdio.h>

int main(){
    int a;
    scanf("%d", &a);
    while(a<10){
    //     a = 11;
    // while(a>10){ ---> These two lines will lead to an infinite loop
        printf("%d\n", a);
        a++;
    }

    return 0;
}

2 prog
#include<stdio.h>

int main(){
   
    int a=0;
     //Loops are used to repeat similar part of a code snippet efficiently
     while (a<=100)
     {
       printf("%d\n", a);
     a++;
     }
     
     
     //=---> 100 times
     
    return 0;
}

3:
While loop prog-if want to print 10 to 20 number directly with i=0 counter( while with if command)

#include<stdio.h>

int main(){
    int i=0;
    //printf("Enter your number\n");
    scanf("%d",&i);
    while(i<=20){
        if(i>=10){
        printf(" The value of i is %d\n",i);
        }
        i++;
    }
    return 0;
}

How Increment works

#include<stdio.h>

int main(){
    int i=5;
    printf("The value of i++ is %d\n", i++);
    //pehle print fir increement
    //++i pehle increment fir print
    printf("The value of i is %d\n", i);
    return 0;

Vica versa in decrement

To print "n" natural numbers

#include<stdio.h>

int main(){
    int i=0;
    int n;

    printf("How many natural numebr you want?\n");
    scanf("%d",&n);
    do{
        printf("The number is %d\n",i+1);
        i++;
    }while(i<n);
    return 0;
}


For loop (best)
Printing n natural number using loop

#include<stdio.h>

int main(){

    int n;
    printf("Enter the number\n");
    scanf("%d",&n);
    //for(initialize;test; increment)
    //{code;
    //}
    for(int i=0; i<=n; i++){
        printf("The value of a is %d \n", i);
    }
    return 0;
}

Comments

Popular posts from this blog

Java Apni Kak

JS