Advertisements

#### Search results for while

##### Example:[1]
``````
Write a c program function to calculate the sum of all digits of an integer number.
``````
``````
#include <stdio.h>
int SumDigit(int );
int main()
{
int res;
res = SumDigit(1223);
printf("The sum of digits %d\n", res);
return 0;
}
int SumDigit(int num)
{
int sum = 0,rem;
while(num > 9)
{
rem = num%10;
sum += rem;
num /= 10;
}
sum += num;
return sum;
}

``````
##### Example:[2]
``````
Write a c program function to accept an integer number and return msb last digit of the number.
``````
``````
#include <stdio.h>
int LastDigit(int );
int main()
{
int res;
res = LastDigit(56431223);
printf("The last msb digit is %d\n", res);
return 0;
}
int LastDigit(int num)
{
int rem;
while(num > 9)
{
rem = num%10;
num /= 10;
}
rem = num%10;
return rem;
}
``````
##### Example:[3]
``````
Write a C program function to convert Decimal to Binary number system.
``````
``````
#include <stdio.h>
long binary(long int n);
int main()
{
long int num = 29;
long res;
res = binary(num);
printf("Result in binary is %d\n",res);
return 0;
}
long binary(long int n)
{
long rem,a = 1,bin = 0;
while(n > 0)
{
rem = n%2;
bin = bin + rem *a;
n /= 2;
a *= 10;
}
return bin;
}

``````
##### Example:[4]
``````
Write a c program function to check a given number is even parity(even no of 1s) or odd parity(odd no of 1s).
``````
``````
#include <stdio.h>
int CheckParity(int );
int main()
{
unsigned int num = 12;
printf("Parity of %d,%s\n",num,(CheckParity(num)? "odd" : "even"));
return 0;
}

int CheckParity(int n)
{
int parity = 0;
while(n)
{
parity = !parity;
n = n & (n-1);
}
return parity;
}
``````
##### Example:[5]
``````
Write a c program function that reverses the string passed to it without declaring any new string within the function.
``````
``````
#include <stdio.h>
void reverse(char a[]);
int main() {
char str[10] = "Hello";
reverse(str);
printf("\nReverse string is :%s\n", str);
return (0);
}
void reverse(char d[])
{
int k,len = 0;
char temp;
int i = 0;
for(k = 0;d[k] != '\0';k++)
{
len++;
}
while(i < (len - 1))
{
temp = d[i];
d[i] = d[len-1];
d[len-1] = temp;
i++;
len--;
}
}
``````
##### Example:[6]
``````
Write a program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!).
``````
``````
#include <stdio.h>
#include <string.h>
unsigned int search_word(char *,char *);
int main()
{
char *s_str = "the";
char *l_str = "the word searching for from these sentence is the.";
unsigned int result;
result = search_word(s_str,l_str);
printf("Small string is found %d times in long string\n",result);
return 0;
}
unsigned int search_word(char *s1,char *s2)
{
int times = 0;
int i = 0,j = 0,k = 0;
int size = strlen(s1);
printf("Size of small string \" %s \" is: %d\n",s1,size);
while(s2[j] != '\0')
{
if(j >= (size - 1))
{
if(s2[j] == s1[size-1] && s2[j-1] == s1[size-2] && s2[j-2] == s1[size-3])
{
times = times + 1;
}
}
j++;
}
return times;
}
``````
##### Example:[7]
``````
Write a c program function to accept a string and return the character written two times continuously in the string.
``````
``````
#include <stdio.h>
char res_char(char *);
int main()
{
char *str = "Hello how are you";
char result;
result = res_char(str);
printf("Got result %c\n",result);
return 0;
}
char res_char(char *f)
{
char res;
while(*f != '\0')
{
if(*f == *(f + 1))
{
res = *f;
}
f++;
}
return res;
}
``````
##### Example:[8]
``````
Write a C program function to count frequency of each character in a string and return the maximum frequency.
``````
``````
#include <stdio.h>
int freq_func(char *);
int main()
{
char *s = "Here is the string that needs to be verified for the frequency of each character";
int max_freq;
max_freq = freq_func(s);
printf("Max Frequency is:%d\n",max_freq);
return 0;
}
int freq_func(char *r)
{
int i =0,j = 0,res1 = 0,res2 = 0;
while(r[j] != '\0')
{
while(r[i] != '\0')
{
if(r[j] == r[i])
{
res1 = res1 + 1;
}
i++;
}
if(res1 > res2)
{
res2 = res1;
}
i = 0;
res1 = 0;
j++;
}
return res2;
}
``````
##### Example:[9]
``````
Write a C program function to take a string as its input and return the first word of the string passed to it.

``````
``````
#include <stdio.h>
void getfword(char *s, char f_word[]);
int main() {
char str[] = "Hello how are you";
char res[20];
getfword(str,res);
printf("Result is:%s\n", res);
}
void getfword(char *s, char f_word[])
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
break;
}else
{
f_word[i] = s[i];
f_word[i + 1] = '\0';
i++;
}
}
}

``````
##### Example:[10]
``````
Write a c program function to calculate the number of words present in a string passed to it.
``````
``````
#include <stdio.h>
int NumOfWords(char *s);
int res = 1;
int main() {
char str[] = "Hello how are you";
res = NumOfWords(str);
printf("Result is:%d\n", res);
}
int NumOfWords(char *s)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
res = res + 1;
}
i++;
}
return res;
}

``````
##### Example:[11]
``````
Write a c program to accept a string as input and return the number of words present in it using pointers.

``````
``````
#include <stdio.h>
void NumOfWords(char *s, int *);
int main() {
char str[] = "Hello how are you";
int res = 1;
NumOfWords(str,&res);
printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int *r)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
*r = *r + 1;
}
i++;
}
}

``````
##### Example:[12]
``````
Write a function to loop through the strings present in an array of pointers to string
``````
``````
#include <stdio.h>
char *StringSet[] = {
"string1 1 0 2 0",
"string2 1 2 0 5 9",
"string3 0 9 2",
""
};
int main(){
int i = 0;
while(*StringSet[i] != '\0'){
printf("String %s\n",StringSet[i]);
i++;
}
}
``````
##### Example:[13]
``````
Write a c program function to display all node data present in a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void AddAtBegin(int );
void Traverse();
void main()
{
AddAtBegin(10);
Traverse();
}
void AddAtBegin(int val)
{
struct Node *ptr;
if(head == NULL)
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
head = ptr;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = head;
head = ptr;
}
}
}
void Traverse()
{
struct Node *ptr = head;
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
``````
##### Example:[14]
``````
Write a c program function to add new nodes at the end of a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void Traverse();
void main()
{
AddAtEnd(10);
AddAtEnd(20);
Traverse();
AddAtEnd(30);
Traverse();
}
void AddAtEnd(int val)
{
struct Node *ptr,*aptr = head,*bptr;
if(head == NULL)
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
head = ptr;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
while(aptr != NULL)
{
bptr = aptr;
aptr = aptr->next;
}
ptr->data = val;
bptr->next = ptr;
ptr->next = NULL;
}
}
}
void Traverse()
{
struct Node *ptr = head;
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
``````
##### Example:[15]
``````
Write a c program function to add new nodes in the middle of a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void AddAtMid(int ,int );
void Traverse();
void main()
{
AddAtEnd(10);
AddAtEnd(20);
AddAtEnd(30);
AddAtMid(25,2);
Traverse();
}
void AddAtEnd(int val)
{
struct Node *ptr,*aptr = head,*bptr;
if(head == NULL)
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
head = ptr;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
while(aptr != NULL)
{
bptr = aptr;
aptr = aptr->next;
}
ptr->data = val;
bptr->next = ptr;
ptr->next = NULL;
}
}
}
void Traverse()
{
struct Node *ptr = head;
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
void AddAtMid(int val,int pos)
{
struct Node *aptr = head,*bptr = head,*cptr;
int count = 0;
if(head == NULL)
{
printf("List is empty\n");
}else
{
while(aptr != NULL)
{
aptr = aptr->next;
count++;
}
printf("%d\n", count);
if(count < pos)
{
printf("We have less nodes than %d\n", pos);
}else
{
aptr = (struct Node *)malloc(sizeof(struct Node ));
if(aptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
count = 0;
while(count != pos)
{
cptr = bptr;
bptr = bptr->next;
count++;
}
aptr->data = val;
cptr->next = aptr;
aptr->next = bptr;
}
}
}
}
``````

#### Search Links

 Search more C examples Search more C MCQs

#### Popular C search examples

 fibonacci factorial typedef while volatile static state machine float heap overflow continue break extern register do sizeof return long enum 2-D arrays fscanf