







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 13
This page cannot be seen from the preview
Don't miss anything!








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>
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; }
} 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);
1 is A factor 2 3 4 5 6 C code: #include <stdio.h>
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; } }
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=
#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
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; }