Category Archives: C problems for beginner

Write a program in C to read 10 numbers from the keyboard and find their sum and average

Write a program in C to read 10 numbers from the keyboard and find their sum and average

Solution in C:

#include<stdio.h>


void main()
{
int i,n,sum=0 ;double aver=0;
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=10;i++){
sum=sum+i;
aver=sum/(double)10;
}
printf("The sum of 10 no is :%d\n",sum);
printf("The Average is :%lf",aver);
return 0;

}

This code is a simple C program that calculates the sum and average of the first 10 natural numbers. Let’s go through the code step by step:

  1. The code begins with the inclusion of the standard input/output library, stdio.h, which provides functions for input and output operations.
  2. The main() function is the entry point of the program.
  3. Inside the main() function, several variables are declared:
    • i is an integer variable used as a counter for the loop.
    • n is an integer variable that will store the user input.
    • sum is an integer variable initialized to 0. It will store the sum of the numbers.
    • aver is a double variable initialized to 0. It will store the average of the numbers.
  4. The program then prompts the user to enter a number using printf(). The message displayed is “enter the number:”.
  5. The input number is read using scanf() and stored in the variable n.
  6. A for loop is used to iterate from 1 to 10. The loop variable i is initialized to 1, and the loop continues as long as i is less than or equal to 10. After each iteration, i is incremented by 1.
  7. Inside the loop, the variable sum is updated by adding the current value of i to it.
  8. The variable aver is calculated by dividing the current value of sum by 10. To ensure that the division is performed with decimal precision, (double) is used to cast the denominator to a double.
  9. After the loop completes, the program prints the sum using printf(). The message displayed is “The sum of 10 no is: %d\n”, where %d is a placeholder for the integer value of sum.
  10. Similarly, the program prints the average using printf(). The message displayed is “The Average is: %lf”, where %lf is a placeholder for the double value of aver.
  11. Finally, the return 0 statement is used to indicate the successful execution of the program and terminate the main() function.

In summary, this code calculates the sum and average of the first 10 natural numbers and prints the result.

Read more

C Programming Full Course

Write a program sum of first n natural numbers in C.

Write a program sum of first n natural numbers in C.

#include<stdio.h>

void main ()
{
int i,n ,sum= 0;
scanf("%d",&n);
printf("The first 7 natural is:",n);
for(int i=1;i<=7;i++)
{ printf("%d ",i);
sum=sum+i;
}
printf("\nThe Sum of Natural Number upto 7 terms : %d",sum);

return 0;
}

Write a program in C to display the first 10 natural numbers.

SaveCodelink

Write a program in C to display the first 10 natural numbers.

Example 1.Write a program in C to display the first 10 natural numbers.

#include<stdio.h>

void main ()
{
int i,n;
scanf("%d",&n);
printf("The first 10 natural numbers are: ",n);

for(int i=1; i<=n;i++)
{

printf("%d ",i);
}

}

When you run this code, it will output the numbers 1 through 10 separated by spaces. The loop will start with i = 1 and print it to the console. Then, it will increment i by 1 and repeat the process until i reaches 11, which violates the condition i <= 10 and terminates the loop.

OUTPUT:

The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10

C++ Beginners Problems

QUORA

Searching for a Value in an Array in C Programming

Searching for a Value in an Array in C Programming

Programming languages are used to communicate with computers and to create software applications. C programming language is one of the most popular programming languages used for system programming, embedded systems, and game development. One of the essential aspects of programming is searching for values in an array. This article will explore the code for searching for a value in an array in C programming.

Introduction to Searching for a Value in an Array

Searching for a value in an array is a common task in programming. An array is a collection of similar data types, and searching for a value in an array requires iterating through the array and comparing each element with the value being searched for. In C programming, we can use the for loop to iterate through an array and compare the elements.

The Code for Searching for a Value in an Array

The code for searching for a value in an array in C programming is shown below:

#include<stdio.h>
int main()
{
    int num[]={12,34,56,78,19};
    int pos=-1,value,i;
    scanf("%d",&value);
    for(i=0;i<5;i++)
    {
        if(value==num[i]){
            pos=i+1;
            break;
        }
    }
    if(pos==-1){
        printf("value is not common");
    }
    else printf("value %d is found at %d position",value,pos);
}

Let’s analyze the code in detail.

Line 1: Include Header File

The first line of the code is the preprocessor directive #include<stdio.h>. This line includes the standard input-output header file, which provides functions for input and output operations.

Line 2: Define the main() Function

The second line of the code defines the main() function, which is the starting point of any C program. The function returns an integer value, which represents the exit status of the program.

Line 4: Declare and Initialize the Array

The fourth line of the code declares and initializes an integer array named num. The array contains five elements, which are 12, 34, 56, 78, and 19.

Line 5-6: Declare and Initialize Variables

The fifth and sixth lines of the code declare and initialize two integer variables named pos and value. The pos variable is used to store the position of the value in the array, and the value variable is used to store the value being searched for.

Line 7: Get the Value from the User

The seventh line of the code uses the scanf() function to get the value being searched for from the user.

Line 8-12: Search for the Value in the Array

The eighth to twelfth lines of the code use a for loop to iterate through the array and search for the value. Inside the for loop, the if statement checks if the current element of the array is equal to the value being searched for. If the value is found, the position of the value in the array is stored in the pos variable, and the loop is terminated using the break statement.

Line 13-16: Print the Result

The thirteenth to sixteenth lines of the code use an if-else statement to print the result. If the value is not found, the program prints a message stating that the value is not in the array. If the value is found, the program prints a message stating the position of the value in the array.

Conclusion

Searching for a value in an array is a fundamental task in programming, and the for loop is a common construct used to iterate.

Array in C Programming

Quora | Youtube | Facebook

How do you use Scanf to enter integers into the array in C?

A Beginner’s Guide to Understanding C Programming Basics-3

C Programming Basics Problems for Beginners

1. Generating a series of numbers and calculating their sum in V

Generating like 9 99 999 9999 99999 and there sum

#include <stdio.h>

void main()
{   long int n,i,t=9;
	int sum =0;
	printf("Input the number or terms :");
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
	  t=t*10+9;
	  sum =sum+t;
	  printf("%d   ",t);
	}
	printf("\nThe sum of the series = %d \n",sum);
}

output :

C Programming

2.The Sum of Odd Natural Numbers in C

#include<stdio.h>
void main ()
{
    int i,n,odd=0,sum=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
if(i%2!=0){
    printf ("  %d",i);
sum=i+sum;
}

}

    printf("\nThe odd numbers are :%d\n",n/2+1);
    printf("The Sum of odd Natural Number upto 10 terms : %d\n",sum);
}

For example, if the user enters 5, the output could be:

Output:

1 3 5
The odd numbers are: 3
The sum of the first 5 odd natural numbers is 9

3.Prime number Checking in C

#include<stdio.h>
void main ()
{
    int i,n,f=0,count=1;
    printf("Enter a number:");
    scanf("%d",&n);
    for(n=1;i<=100;n++){

    for(i=2;i<n;i++){
        if(n%i==0){
f++;
break;

        }

    }
    }
 if(f==0)
            {printf("prime  number ");}
        else
        { printf("prime not number ");}
}

Input:

Enter a number: 5

Output:

prime not number

4.The Sum of Square Natural Number upto 5 terms in C

#include<stdio.h>
void main ()
{
int i, n,square=1,sum=0;
scanf("%d",&n);
printf("The square natural upto 5 terms are :");
for(i=1;i<=n;i++){
    square=i*i;
    printf("%d ",square);
    sum=square+sum;
}
printf("\nThe Sum of Square Natural Number upto 5 terms:%d",sum);
}

Input: 5

Output:
The square natural upto 5 terms are :1 4 9 16 25
The Sum of Square Natural Number upto 5 terms:55

5.The sum of the digits of the Given Number in C

#include<stdio.h>
void main ()
{
int r,n,sum=0;
printf("Enter any number:");
scanf("%d",&n);
while(n>0){
   r=n%10;
   sum=sum+r;
   n=n/10;

}
printf("Sum of the digits of Given Number = %d",sum);
}

Input: Enter any number:567
Output: Sum of the digits of the Given Number = 18

6. Area of Sphere surface in C

#include<stdio.h>
int main()
{
  double radius,area;
  const double pie =3.1416;
  printf("Enter a radius :");
  scanf("%lf",&radius);
  area =4*3.1416*radius*radius;
  printf("Sphere surface is:%.3lf",area);

return 0;
}

Input: Enter a radius: 8

Output: Sphere surface is:804.250

See more

Computer programming

Basics of C Programming

Arrays of Characters in C-1

Arrays of Characters in C

Here are a few examples of char array problems:

1. Reversing a string: Write a program that takes a string as input and returns the string in reverse order.

For example, if the input is “hello”, the output should be “olleh”.

Click here to the Solution

2. Counting vowels: Write a program that takes a string as input and returns the number of vowels in the string.

For example, if the input is “hello”, the output should be 2.

3. Finding substrings: Write a program that takes two strings as input and checks if the first string contains the second string as a substring.

For example, if the input is “hello world” and “world”, the output should be true.

4. Converting case: Write a program that takes a string as input and converts all uppercase letters to lowercase letters and vice versa.

For example, if the input is “Hello World”, the output should be “hELLO wORLD”.

5. Counting words: Write a program that takes a string as input and returns the number of words in the string. For example, if the input is “The quick brown fox jumps over the lazy dog”, the output should be 9.

I hope these examples give you some inspiration for working with char arrays!

Arrays of Characters in C

For more information click here:Basics of C Programming