Fundamental C Programming Concepts, Cheat Sheet of Law

A variety of fundamental c programming concepts, including the sum of the first n numbers, finding the biggest of three numbers using a conditional operator, displaying even numbers from 1 to 10 using a while loop, determining if a year is a leap year, printing a multiplication table, finding the factors of a number, identifying prime numbers, determining if a number is an armstrong number, counting the digits in a number, finding the sum of digits in a number, calculating the least common multiple (lcm) of two numbers, and finding the greatest common divisor (gcd) of two numbers. Sample c code implementations for each of these concepts, making it a valuable resource for students and developers learning or practicing c programming.

Typology: Cheat Sheet

2022/2023

Uploaded on 11/13/2023

harish-14
harish-14 🇮🇳

1 document

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1:
Eg: if the input is 5,we have to add 1+2+3+4+5 and print the sum
#include <stdio.h>
int main() {
// Write C code here
int a;
int sum=0;
scanf("%d",&a);
for(int i=1;i<=a;i++){
sum=sum+i;
}
printf("%d",sum);
return 0;
}
2:
#include <stdio.h>
Sum of
first N
number
s
Biggest of
3 Numbers
using
Conditiona
l Operator
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Fundamental C Programming Concepts and more Cheat Sheet Law in PDF only on Docsity!

1: Eg: if the input is 5,we have to add 1+2+3+4+5 and print the sum #include <stdio.h> int main() { // Write C code here int a; int sum=0; scanf("%d",&a); for(int i=1;i<=a;i++){ sum=sum+i; } printf("%d",sum); return 0; } 2: #include <stdio.h>

Sum of

first N

number

s

Biggest of

3 Numbers

using

Conditiona

l Operator

int main() { // Write C code here int a,b,c; scanf("%d%d%d",&a,&b,&c); (a>b)&&(a>c)?printf("%d",a):b>c?printf("%d",b):printf("%d",c); return 0; #include <stdio.h> int main() { int i=2; // Write C code here while(i<=10){ printf("%d\n",i); i=i+2; } return 0; }

Display

Even

Numbers

from 1 to

10 using

While Loop

} else printf("not leap"); return 0; C program to print the multiplication table #include <stdio.h> int main() { int m; int i=1; // Write C code here scanf("%d",&m); while(i<=10){ printf("\n%d * %d = %d",m,i,m*i);

i++;

return 0;

If the input is 6,there are 4 factors.

1 is A factor 2 3 4 5 6 C code: #include <stdio.h>

C

program

to find

the

factors

of a

number

The sum of factor is 1+2+3 is 6 itself.so 6 is a perfect number. Initialize fact= The logic is first write a for loop If fact is two it is a prime number,because 1 and that itselt is a factor so it is a prime number #include <stdio.h> int main() { // Write C code here int num,fact=1,sum=0; scanf("%d",&num); for(int i=1;i<=num/2;i++){ if(num%i==0){ fact+=1; } }

Prime

Number

if(fact==2){ printf("%d is a prime number",num); }else printf("Not prime"); return 0; } Logic is if the given is 153,then if cube of individual digits is equal to the number itself,it is a Armstrong number. Eg:153, 111+555+333=

Armstro

ng

number

in c

#include <stdio.h> int main() { // Write C code here int sum=0,num,rem; scanf("%d",&num); int temp=num; while(num>0){ rem=num%10; sum+=rem; num=num/10; } printf("%d",count); return 0; } C program to find the lcm

Sum of

digits of

a given

number

The logic is use a ternary to store the bigger number in lcm variable and use a while loop. C code: #include <stdio.h> int main() { // Write C code here int num1,num2,fact=1,lcm; scanf("%d%d",&num1,&num2); lcm=(num1>num2)?num1:num2; while(fact){ if(lcm%num1==0 && lcm%num2==0){ printf("%d",lcm); fact=0; } lcm++; }

return 0; }