Skip to content

Instantly share code, notes, and snippets.

@sumukus
Last active September 14, 2019 12:48
Show Gist options
  • Save sumukus/19b2f08d221c434b5459e9c1fd78de5e to your computer and use it in GitHub Desktop.
Save sumukus/19b2f08d221c434b5459e9c1fd78de5e to your computer and use it in GitHub Desktop.
This file contains some basic C programming code
#include<stdio.h>
/* do while loop and break statement used to
count the number of character in the string */
int main(){
char str[]="Grettings";
int count=0;
do{
if(str[count]=='\0'){
printf("There are %d characters in total\n",count);
break;
}
count++;
}while(1);
return 0;
}
#include<stdio.h>
//switch case example to check vowel
int main(){
char test;
printf("Enter the character:");
scanf("%c",&test);
switch(test){
case 'a':
printf("%c is a vowel\n",test);
break;
case 'e':
printf("%c is a vowel\n",test);
break;
case 'i':
printf("%c is a vowel\n",test);
break;
case 'o':
printf("%c is a vowel\n",test);
break;
case 'u':
printf("%c is a vowel\n",test);
break;
default:
printf("%c is a not a vowel\n",test);
}
}
#include<stdio.h>
//if else statement example to test even and odd
int main(){
int test;
printf("Enter the value:");
scanf("%d",&test);
if(test%2==0){
printf("%d is Even",test);
}else{
printf("%d is Odd",test);
}
return 0;
}
#include<stdio.h>
/* for loop and continue statement example
to print the even number from integer array */
int main(){
int numbers[]={7,12,3,4,9,18,222};
int size=sizeof(numbers)/sizeof(int);
for(int i=0;i<size;i++){
if(numbers[i]%2 != 0)
//if number is odd, no printing
continue;
else
printf("%d ",numbers[i]);
}
return 0;
}
#include<stdio.h>
//Compute the n factorial in iterative manner
int main(){
int N,fact=1;
printf("Enter the value of N:");
scanf("%d",&N);
for(int i=1;i<=N;i++){
fact*=i;
}
printf("Factorial of %d! is %d\n",N,fact);
return 0;
}
#include<stdio.h>
//Function prototype
int gcdFunction(int, int);
/* use of recursive function to compute gcd of
two numbers using user defined function */
int main(){
int a,b;
printf("Enter value of a and b:");
scanf("%d%d",&a,&b);
//Function Call
printf("gcd of %d and %d is %d\n",a,b,gcdFunction(a,b));
return 0;
}
//Function Definition
int gcdFunction(int x,int y){
if(x%y==0)
return y;
else
return gcdFunction(y,x%y);
}
#include<stdio.h>
//if-else if ladder example to find leap year
int main(){
int year;
printf("Enter the year:");
scanf("%d",&year);
if(year%400==0)
printf("%d is a leap year\n",year);
else if(year%100==0)
printf("%d is not a leap year\n",year);
else if(year%4==0)
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment