C Programming Codes
13.4K subscribers
139 photos
2 videos
12 links
C Programming Codes || Quizzes || DSA

Learn along with the community

Any queries
admin - @Pradeep_saii
Download Telegram
Program to reverse a number using for loop.
-----------------------------------
#include <stdio.h>

int main() {
int num, reversedNum=0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
for (; num!=0; num/=10) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
}
printf("Reversed Number = %d", reversedNum);
return 0;
}
-------------------------------------

#for_loop
Program to check whether a number is palindrome or not using for loop.
------------------------------
#include <stdio.h>

int main() {
int num, reversedNum = 0, remainder, originalNum;

printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;

// reverse the number
for (; num != 0; num /= 10) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
}

if (originalNum == reversedNum) {
printf("%d is a palindrome number.", originalNum);
}
else {
printf("%d is not a palindrome number.", originalNum);
}

return 0;
}
-------------------------------
#for_loop
👍3
Program to print numbers from 1 to n using while loop .
-------------------------
#include <stdio.h>

int main()
{
int i = 1,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i <= n) {
printf("%d \n", i);
i++;
}
return 0;
}
----------------------------
#while_loop
👍1
//Program to print evun numbers from 0 to n using while loop.
//-------------------------------
#include<stdio.h>
int main()
{ int i=0,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i=i+2;
}
return 0;
}

//#while_loop
//Program to print odd numbers from 1 to n using while loop.
//--------------------------
#include<stdio.h>
int main()
{ int i=1,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i=i+2;
}
return 0;
}

//#while_loop
👍2
//Program to add numbers from 1 to n using while loop.
#include<stdio.h>
int main()
{ int i=1,n,sum=0;
printf("Enter the value of n:");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("The sum of %d numbers is :%d",n,sum);
return 0;
}

//#while_loop
👍2
//Program to print multiplication table using while loop.
#include<stdio.h>
int main()
{ int i=1,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i<=10)
{
printf("%d x %d = %d\n",n,i,n*i);
i++;
}
return 0;
}

//#while_loop
👍1
//Program to print multiplication table in reverse order using while loop.
#include<stdio.h>
int main()
{ int i=10,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i)
{
printf("%d x %d = %d\n",n,i,n*i);
i--;
}
return 0;
}

//#while_loop
👍1
//Program to find factorial of a number using while loop.
#include<stdio.h>
int main()
{ int i=1,fact=1,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("The factorial of %d is : %d",n,fact);
return 0;
}

//#while_loop
👍2
//Program to generate fibonacci series using while loop.
#include<stdio.h>
int main()
{ int i=1,n,n1=0,n2=1,nextTerm;
printf("Enter the limit of fibonacci series:");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",n1);
nextTerm=n1+n2;
n1=n2;
n2=nextTerm;
i++;
}
return 0;
}

//#while_loop
👍2
// Program to check weather a number is prime or not using while loop.
#include <stdio.h>
int main()
{
int num, i = 2, isPrime = 1;
printf("Enter the number:");
scanf("%d", &num);
if (num <= 1)
{
isPrime = 0;
}
else
{
while (i <= num / 2)
{
if (num % i == 0)
{
isPrime = 0;
break;
}
i++;
}
}
if(isPrime==1)
{
printf("%d is a prime number.",num);
}
else{
printf("%d is not a prime number.",num);
}
return 0;
}

//#while_loop
1👍1
// Password validation using do while loop.
#include <stdio.h>
int main()
{ int password;
do {
printf("Enter the password:");
scanf("%d",&password);
}while(password!=1243);
printf("Access Granted.");
return 0;
}
//#do_while
//function to calculate factorial
#include <stdio.h>
int factorial(int);
int main()
{ int n;
printf("Enter the number you want to factoriate:");
scanf("%d",&n);
printf("Factorial of %d is %d",n,factorial(n));
return 0;
}
int factorial(int n){
if(n==1 || n==0){
return 1;
}
return n*factorial(n-1);
}
//#recursive_function
🥰1
//Guessing a number game in c
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{ int number,guess,numguesses=1;
srand(time(0));
number=rand()%100+1;
do
{ printf("Guess a number between 1 to 100:");
scanf("%d",&guess);
if(guess>number){
printf("Your guess is wrong \nPlease enter lower number\n");
}
else if(guess<number){
printf("Your guess is wrong \nPlease enter a greater number\n");
}
else{
printf("Congrats! You have guessed it correct\n");
printf("The number of attempts you've taken to guess the correct number is %d times ",numguesses);
}
numguesses++;
} while (guess!=number);
return 0;
}
#num_guessing_game
👍2🤯1
//Program to print the adress of variable and using that address get the value of that variable.
#include<stdio.h>
int main(){
int a=10;
int *address=&a;
printf("Address of variable a: %p\n",&a);
printf("Value of a:%d",*address);
return 0;
}
#pointers
👍1
//Program for understanding arithmetic operators.
#include<stdio.h>
int main(){
int a=10,b=5;
printf("Addition of a and b:%d\n",a+b);
printf("Subraction of a and b:%d\n",a-b);
printf("Multiplication of a and b:%d\n",a*b);
printf("Division of a and b:%d\n",a/b);
printf("Modulo of a and b:%d",a%b);
return 0;
}
#operators
👍3
//Program for understanding increment and decrement operators.
//post increment.
#include<stdio.h>
int main(){
int a=10;
int b=a++; //post increment:the actual value of a is first assigned to b and next the value of a will be incremented by 1.
printf("The value of a:%d and b: %d",a,b);
return 0;
}
#operators
👍1
//Program for understanding increment and decrement operators.
//pre increment.
#include<stdio.h>
int main(){
int a=10;
int b=++a; //pre increment:the actual value of a is first incremented by 1 and assigned to b.
printf("The value of a:%d and b: %d",a,b);
return 0;
}
#operators
//Program for understanding increment and decrement operators.
//post decrement.
#include<stdio.h>
int main(){
int a=10;
int b=a--; //post decrement:the actual value of a is first assigned to b and then the value of a is decremented by 1.
printf("The value of a:%d and b: %d",a,b);
return 0;
}
#operators