Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

C Programming Lecture #3

Uploaded by

geektechnolgy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Programming Lecture #3

Uploaded by

geektechnolgy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C Programming Lecture #3

If-Else, Ternary Operator, and For Loop


By Naman Sharma (Angaar Batch) 🔥

Introduction: Control Flow in C

Control flow ka kaam hota hai ek program ko batana ki kaunsa kaam kab aur kaise karna hai.
If-Else, Ternary Operator, aur For Loop humare powerful tools hain to guide this flow.

Ab yeh mat sochna ki yeh sirf theory hai—iska asli mazza tab aayega jab tum inko use karke

🚀
questions solve karoge!
Let’s dive in!

1. If-Else Statement
If-Else ek decision-making tool hai. Agar koi condition true ho, toh kuch kaam karo, warna kuch
aur. Jese …..

sardi hai to coffee pilado warna ………..


Syntax:

if (condition) {
// Code when condition is true
} else {
// Code when condition is false
}

Example:

#include <stdio.h>

int main() {
int temp;

printf("Enter your city temperature: ");


scanf("%d", &temp);

if (temp<=15) {
printf("Hello Friends Chai Pi Lo! \n");
} else {
printf("Juice Pilado Mosambi Ka ! \n");
}

return 0;
}
2. Ternary Operator
Ternary operator ek compact version hai if-else ka. Iska use simple ya kahu one liner decisions
ke liye hota hai.

Syntax:
condition ? value_if_true : value_if_false;

Example:
#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

number % 2 == 0 ? printf("Even\n") : printf("Odd\n");

return 0;
}

3. For Loop
Ab tumhe repetitive kaam karna hai,jese apne dost ko 200 birthday messages bhejne hai, toh
kya manually likhoge? Bilkul nahi!
For loop tumhare repetitive tasks ko automate karta hai.

Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute repeatedly
}
For Loop Examples:

Print Numbers from A to B:

#include <stdio.h>

int main() {
int A, B;

printf("Enter the start (A) and end (B): ");


scanf("%d %d", &A, &B);

for (int i = A; i <= B; i++) {


printf("%d ", i);
}

return 0;
}

Print Sum of Numbers from A to B:

#include <stdio.h>

int main() {
int A, B, sum = 0;

printf("Enter the start (A) and end (B): ");


scanf("%d %d", &A, &B);

for (int i = A; i <= B; i++) {


sum += i;
}
printf("Sum: %d\n", sum);
return 0;
}

Print Evens from A to B:


#include <stdio.h>

int main() {
int A, B;

printf("Enter the start (A) and end (B): ");


scanf("%d %d", &A, &B);

for (int i = A; i <= B; i++) {


if (i % 2 == 0) {
printf("%d ", i);
}
}

return 0;
}

Print Sum of Evens from A to B:


#include <stdio.h>

int main() {
int A, B, sum = 0;

printf("Enter the start (A) and end (B): ");


scanf("%d %d", &A, &B);

for (int i = A; i <= B; i++) {


if (i % 2 == 0) {
sum += i;
}
}
printf("Sum of evens: %d\n", sum);
return 0;
}

Print Factorial of a Number:


#include <stdio.h>

int main() {
int n, fact = 1;

printf("Enter a number: ");


scanf("%d", &n);

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


fact *= i;
}

printf("Factorial: %d\n", fact);


return 0;
}

Print Factors of a Number:


#include <stdio.h>

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

printf("Factors of %d: ", n);


for (int i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}

return 0;
}

Print Sum of Factors of a Number:


#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a number: ");


scanf("%d", &n);

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


if (n % i == 0) {
sum += i;
}
}

printf("Sum of factors: %d\n", sum);


return 0;
}
Count the Factors of a Number:
#include <stdio.h>

int main() {
int n, count = 0;

printf("Enter a number: ");


scanf("%d", &n);

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


if (n % i == 0) {
count++;
}
}

printf("Number of factors: %d\n", count);


return 0;
}
Prime Check:
#include <stdio.h>

int main() {
int n, isPrime = 1;

printf("Enter a number: ");


scanf("%d", &n);

for (int i = 2; i <= n / 2; i++) {


if (n % i == 0) {
isPrime = 0;
break;
}
}

if (n < 2) isPrime = 0;

if (isPrime) {
printf("%d is a prime number.\n", n);
} else {
printf("%d is not a prime number.\n", n);
}

return 0;
}
After seeing so many codes

Print Prime Numbers from A to B:


#include <stdio.h>

int main() {
int A, B;

printf("Enter the start (A) and end (B): ");


scanf("%d %d", &A, &B);

for (int i = A; i <= B; i++) {


int count = 0;

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


if (i % j == 0) {
count++; //Increased Count of factors…
}
}

if (count==2) { //A number is prime if factors are 2


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

return 0;
}
Questions for Practice
If-Else Questions:

1. Write a program to find whether a number is positive, negative, or zero.


2. Create a program to find the largest of three numbers.

Ternary Operator Questions:

1. Write a program to check if a number is odd or even using the ternary operator.

For Loop Questions:

1. Print all odd numbers between A and B.


2. Calculate the product of all numbers between A and B.
3. Check if a given number is a perfect number (sum of factors equals the number).

Super Challenge:

Create a program that calculates the sum of all prime numbers between two numbers A and B.

🔥
Thike boss, aaj ke liye itna kaafi hai! Angaari banne ke liye practice karo aur homework time par
submit karna mat bhoolna!
Here is a link to understand if else better:-
https://www.youtube.com/watch?v=gzmEcP2MmIM&list=PLtlkmeoc1tYHFCOvN7ECrpOY3-k6pI
EN_&index=8

Here is a link to understand all for loop programs:-

https://www.youtube.com/watch?v=pwsIE3Qnr9g&list=PLtlkmeoc1tYFS2F8gEPuEts2Vuyrgu7l4

**Padhle bro uske papa ko kya bolega loyal hu?**

You might also like