Pointer and Flow Control
Conditional Statement
Check Odd or Even Number
#include<stdio.h>
int main(){
int a = 10;
int *p = &a;
// a = 15;
// if( a % 2 == 0 ){
if( (*p) % 2 == 0 ){
printf("Even Number\n");
}
else{
printf("Odd Number\n");
}
return 0;
}
Check Vote Eligibility
#include<stdio.h>
int main(){
int age = 21;
int *p = &age;
//*p = 15;
//if(a>=18){
if(*p>=18){
printf("Voter\n");
}
else{
printf("Not Voter\n");
}
return 0;
}
Find Maximum Between Two Numbers
#include<stdio.h>
int main(){
int a = 10, b = 20;
int *p = &a, *q = &b;
/*
// Finding Maximum using Variable
if(a > b){
printf("Maximum : %d\n", a);
}else{
printf("Maximum : %d\n", b);
}
*/
// Finding Maximum using Pointer
if(*p > *q){
printf("Maximum : %d\n", *p);
}else{
printf("Maximum : %d\n", *q);
}
return 0;
}
Loop Statement
Print Natural Number from 1 to N
#include<stdio.h>
int main(){
int a = 0, b = 10;
int *p = &a, *q = &b;
/*
// Printing Natural Number Series from 1 to 10 using Variable
for(a=1; a<=b; a++){
printf("%d ", a);
}
printf("\n");
*/
// Printing Natural Number Series from 1 to 10 using Pointer
for(*p=1; *p<=*q; (*p)++){
printf("%d ", *p);
}
printf("\n");
return 0;
}
Summation of Natural Number from 1 to N
#include<stdio.h>
int main(){
int a = 0, b = 10, c = 0;
int *p = &a, *q = &b, *r = &c;
/*
// Summation of Natural Number Series from 1 to 10 using Variable
c = 0;
for(a=1; a<=b; a++){
c = c + a; //c += a;
}
printf("%d\n", c);
*/
// Summation of Natural Number Series from 1 to 10 using Pointer
*r = 0;
for(*p=1; *p<=*q; (*p)++){
*r = *r + *p;
//*r += *p
}
printf("%d\n", *r);
return 0;
}
Last updated