Thursday, 6 April 2023

C programming NOTES Till LOOPS

                                        Coding Notes


switch operator

 #include <stdio.h>


int main() {

   int day;

   printf("Enter dayd (1 to 5 ): ");

   scanf("%d", &day);

   switch (day) {

       case 1 : printf("modnay \n");

                break;

       case 2 : printf("tuesday \n");

                break;                

       case 3 : printf("wednesday \n");

                break; 

       case 4 : printf("thursday \n");

                break;  

       case 5 : printf("friday \n");

                break;  

       default : printf("not a day");       

   }


    return 0;

}   

                                                               SIMPLE PROGRAM

#include<stdio.h>

int main(){

    

    float a;

    printf("Enter the radius ");

    scanf("%f", &a);

    printf("area is : %f", 3.14*a*a);

}


                             

                                                 IF ELSE STATEMENT

         

 #include <stdio.h>

int main() {

    int number;

    

    printf("enter the number");

    scanf("%d", &number);

    

    if(number>=0) {

        printf("number is positive \n");

        if(number%2==0)

       { printf("number is even");}

        else {printf("number is odd");}

    }

    else {

        printf("number is negative ");

    }

    return 0;

    

}         

                                                         FAIL OR PASS IN NORMAL AND TERNARY


#include<stdio.h>

int main() {

    

    int marks;

    printf("Enter your marks (0 to 100 ) : ");

    scanf("%d", &marks);

    

//     if(marks>=30 && marks<=100){

//         printf("You are pass");

          

//       }

//     else if (marks>=0 && marks<30) {

//         printf("Fail");

//     }

//     else {

//         printf("wrong input");

//     }

           // DEKH UPAR LIKHA HAI HUMNE NORMAL WALA CODE AB HUM TERNARY operator MAI LIKHE GE 

    marks <30 ? printf("fail") : printf("pass");      




  return 0;

}

                                                 PASSING GRADE
#include<stdio.h>
int main(){
    
    int marks;
    printf("Enter your marks:");
    scanf("%d", &marks);
    
   if ( marks<30)
    {printf("Your grade is C");}
   else if (marks >=30 && marks<=70)
   { printf("Your grade is B");}
   else if (marks >70 && marks<=90)
   { printf("Your grade is A");    }
   else if (marks>70 && marks<=100)
   { printf("Your grade is A+");} 
   else  {printf("wrong input");}
  return 0; 
}

                                                                    FOR LOOP

#include<stdio.h>

int main(){

    

   // i ko hum ierator ya counter bol te hai     pehela i indicate karta hai value kaha se start hua hai 2nd i indicate kar ta hai value kaha khatam hogi aur 3rd i indicate kar ta hai i+1 means i ki value koi bhi ho usko plus 1 ke ke aage dalo jub tak 2nd i ko satisfy na ho. ek aur baat i+1 ko hum i++ bhi likh sakte hai but i++ tub hi likhege jub hame kisi bhi chiz ko 1 se increase kar na ho  nhi tho vo hi regular i=i+1.

  // sirf value increase nhi hogi decrease bhi ho sakti hai agar i-1 kiya tho conditions apply

//int i=1 ki jage hum float i=1 bhi le sakte hai 

//aur hum character bhi likh sakte hai like for(char ch='a'; ch<='z'; ch++) like this to print a to z

    for(int i=1; i<=100; i= i+1) {

        printf("%d \n", i);

    }

    return 0;

}

           #include<stdio.h>

int main(){

    

    for(int i=1; i<=5; i++)

    {

        printf("Hello world \n");

    }

    return 0;

}


#include<stdio.h>

int main(){

    

    for(int i=1; i<=50; i++)

    {

        printf("%d \n", i);

    }

    return 0;

}



#include<stdio.h>

int main(){

    

    for(char ch='a'; ch<='z'; ch++)

    {

        printf("%c\n",ch);

    }

    return 0;

}

                                                                  WHILE LOOP   


#include <stdio.h>


int main() {

int i=0, n;

printf("Enter the number");

scanf("%d", &n);

while(i<=n){

    printf("%d \n", i);

    i++;

}


    return 0;

}

                                              comparison between for while 

#include <stdio.h>


int main() {

int n;

printf("Enter the number");

scanf("%d", &n);

//int i=0;

//while(i<=n){

//    printf("%d \n", i);

 //   i++;}

 for(int i=0; i<=n; i++){

 printf("%d \n", i);}

 



    return 0;

}

                                      DO WHILE LOOP

#include <stdio.h>

int main(){

int i=1;

do {

    printf("%d \n",i);

    i++;

} while(i<=5);


    return 0;

                         // Q 23 calculate sum of all number btn 5 to 50 (including 5 and 50)


#include<stdio.h>

int main(){

    

    int sum = 0;

    for(int i=5; i<=50; i++){

        sum = sum + i;

    }

    printf("sum is %d", sum);

    

    return 0;

}

    

                                                Q Print first natural number and then reverse it

#include<stdio.h>

int main(){

    

    int n;

    printf("Enter the number ");

    scanf("%d", &n );

    

    int sum=0;

    for(int i=1; i<=n; i++){

    sum =sum+i;}

    

        printf("sum is %d \n", sum); //yaha pe sum ki jage i mat likh dena sum hi likh na coz yaha hum sum ki value print kar wa ne wale hai 

    for (int i=n; i>=1; i--){

        printf("%d \n",i);

    }

return 0;    

}                                 

                                    SAME QUESTION IN DIFF FORM

#include<stdio.h>

int main(){

    

    int n;

    printf("Enter the number ");

    scanf("%d", &n );

    

    int sum=0;

    for(int i=1, j=n; i<=n && j>=1; i++, j--)  // already hamare pass ek veriablr the 1 issme humne ek naya veriable dala j jiski value hum ne start ki one ke saath i ka kam hoga sum calaulate kar na and j ka kam number ko reverse order mai peint kar wana i ki cond hai i jub tak less then n na rahe j ki cond hai j jub tak > 1 na rahe tub tak reverse order mai print hote jahe i and j ko equal time lagne wala hai isleya && lagaya dono ke bich mai har bar i++ kyuki hum sum kar rahe hai j--

    {

    sum =sum+i;

    printf("%d \n", j);

    }

    

    printf("sum is %d \n", sum); 

    

    

    // i ko  completly nikal bhi sakte hai likr for(j=n; j>=1;j--)

    //aur sum mai sum = sum+j hoga 

return 0;    

}                                               Question print any table 

#include<stdio.h>

int main(){

    

    int n;

    printf("Enter the number ");

    scanf("%d", &n );

    

    for(int i=1; i<=10; i++){

        printf("%d \n", n*i);a

    }


return 0;    

}

                                          // Q print any table in reverse order 


#include<stdio.h>


int main(){


    int n;

    printf("Enter the number ");

    scanf("%d", &n );


    for(int i=10; i>=1; i--){


        printf("%d \n", n*i); 

    }

        return 0;

    }


                                                       Breaking any loop

#include<stdio.h>

int main(){

    

    for(int i=1; i<=5; i++){

        if(i==3){

            break;                  // simply jub bhi i 3 hoga tho vo break ho jaega aur sidha end print hoga 

        }

        printf("%d\n", i);

    }

    printf("end");

    

}


                    // QUESTIOn 17 keep taking numbers as input fron user until user enters an odd number


#include<stdio.h>

int main(){

    

    int n;

    do{

        printf("Enter the number ");

        scanf("%d", &n);

        printf("%d\n", n);

        

        if(n%2 !=0){

            break;

        }

    }while (1);

    printf("number is odd");

    

    return 0;

}

                                                             QUESTION 18

// keep taking number as input from user untill user enters a number while is multiple of 7


#include<stdio.h>

int main(){

    int n;

    do{

        printf("enter a number ");

        scanf("%d\n", &n);

        printf("%d\n",n);

        

        if(n % 7 == 0){ //multiple of 7

            break;

        }

    } while(1);

    printf("thank you");

    

    return 0;

}

                                                    CONTINUE STATEMENT

// Online C compiler to run C program online

#include <stdio.h>


int main() {

    for (int i=1; i<=5; i++){

        if(i==3){        // jub bhi kisi chiz ko skip kar na hai tho 

            continue;   // continue jaise ek code sare number print              kare ga btn 1to 5 bue 3 ko chood ke .

        }

        printf("%d\n", i);

    }

  


    return 0;

}

               // Q print all number from 1 to 10 expect 6


#include <stdio.h>


int main() {

    

    for (int i=1; i<=10; i++){

        if(i==6){

            continue;

        } printf("%d\n",i);

    }

      

    return 0;

}                 

                                  //  Q print all the odd numnber from 5 to 50 


#include <stdio.h>


int main() {

    

    for(int i=5; i<=50; i++){

        if(i % 2 !=0){   // number odd hai 

             printf("%d\n", i);

        }

    }

    

    return 0;

}

                                         //  print the factorial of a number n. 


#include <stdio.h>

 int main(){

     

     int n;

     printf("Enter the number :");

     scanf("%d", &n);

     

     int fact = 1;

     for(int i=1; i<=n; i++) {

         fact = fact * i;

     }

     printf("%d", fact);

     

     return 0;

 }

No comments:

Post a Comment