C Question
C Question
C Question
Syntax
Flow Diagram
Example
Live Demo
#include <stdio.h>
int main () {
int a;
return 0;
}
Strings in C
Strings are defined as an array of characters. The difference between a character
array and a string is the string is terminated with a special character ‘\0’.
Declaration of strings:
#include<stdio.h>
int main()
{
float num1, num2, num3, largest;
1. for loop
2. while loop
3. do...while loop
while loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
Output
1
2
3
4
5
do...while loop
do {
// the body of the loop
}
while (testExpression);
How do...while loop works?
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %.2lf",sum);
return 0;
}
Output
Here, we have used a do...while loop to prompt the user to enter a number. The
loop works as long as the input number is not 0.
The do...while loop executes at least once i.e. the first iteration runs without
checking the condition. The condition is checked only after the first iteration has
been executed.
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
So, if the first input is a non-zero number, that number is added to the sum variable
and the loop continues to the next iteration. This process is repeated until the user
enters 0.
But if the first input is 0, there will be no second iteration of the loop
and sum becomes 0.0.
Outside the loop, we print the value of sum.
Using Function
1. The main() calls the sort() to sort the array elements in ascending
order by passing array a[], array size as arguments.
2) The sort() function compare the a[j] and a[j+1] with the condition
a[j]>a[j+1],if a[j] is the highest value than a[j+1] then swap the both
elements.
3)To sort the array in ascending order
a) Repeat the step b from i=0 to i<n-1
b) for loop iterates from j=0 to j<n-i-1
find the highest element by comparing a[j] and a[j+1] and swap both
elements. Repeat until all iterations of j.
4) After all iterations of i, the sorted array will be generated in which the
elements are in ascending order.
5) To print the sorted array, the main() function calls the print() function
by passing the array, size of the array as arguments.
#include <conio.h>
int sort(int *a,int n)
{
int i,j,temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
} } }
}
print(int *a,int n)
{
int i;
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
int main()
{
int a[10000],i,n,key;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,n);
print(a,n);
}
Output:
Output
C
1 Enter size of the array: 5
2 Enter elements in array : 5 4 3 2 1
31 2 3 4 5
8-5
8-4
4-1
19.(a)
i)strcat( ) Function :
strcat( ) function in C language concatenates two given strings. It
concatenates source string at the end of destination string. Syntax
for strcat( ) function is given below.
Example :
strcat ( str2, str1 ); - str1 is concatenated at the end of str2.
strcat ( str1, str2 ); - str2 is concatenated at the end of str1.
• As you know, each string in C is ended up with null character (‘\0′).
• In strcat( ) operation, null character of destination string is overwritten
by source string’s first character and null character is added at the end of
new destination string which is created after strcat( ) operation.
int main()
{
char name[30] = “Hello”;
return 0;
}
Output :
String before strrev( ) : Hello
String after strrev( ) : olleH