Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
  • Contact Us
Sum of first N natural numbers in C

As we know natural numbers contain all the positive numbers starting from 1, 2, 3, to n numbers or infinity. For example, suppose when we calculate the sum of the first 25 numbers. That means we start adding the numbers from 1 to the given number 25, and the process is called the sum of the first N natural number. In this topic, we will learn how to find the sum of first n numbers using a C program.

Mathematical Formula

Following is the representation to find the sum of n natural numbers using the mathematical formula:

Sum of n natural number = n*(n+1)/2

Check out YouTube channel Mood

  •  Program:

#include<stdio.h>

int main()
{
    int n, sum=0, c, value;

    printf("Enter the number of integers you want to add: ");
    scanf("%d",&n);
    printf("Enter %d intergers:\n",n);
    for(c=1; c<=n; c++)
    {
        scanf("%d",&value);
        sum=sum+value;
    }
    printf("Sum of entered integers: %d",sum);
    return 0;
}

  •  Output:

 
Enter the number of integers you want to add: 5 Enter 5 intergers: 1 36 3 9 11 Sum of entered integers: 60

  •  Visit:

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

Write a program to convert string into upper case

Here is the program to convert a string to uppercase in C language,

In the program, the actual code of conversion of string to upper case is present in main() function. An array of char type s[100] is declared which will store the entered string by user.
Then, for loop is used to convert the string into upper case string and if block is used to check that if characters are in lower case then, convert them in upper case by subtracting 32 from their ASCII value.

  •  Program:

#include<stdio.h>
#include<conio.h>

int main()
{
    char s[100];
    int n;
    printf("Enter a string : \n");
    gets(s);
    for(n=0; s[n]!='\0'; n++)
    {
        if(s[n]>='a' && s[n]<='z')
        {
            s[n]=s[n]-32;
        }
    }
    printf("String in upper case :\n%s",s);
    return 0;
}

  •  Output:

 
Enter a string : nilesh patel String in upper case : NILESH PATEL

  •  Visit:

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

Reverse a String in C

This topic will discuss several ways to reverse a string in the C programming language. Reversing a string is the technique that reverses or changes the order of a given string so that the last character of the string becomes the first character of the string and so on. Furthermore, we can also check the Palindrome of the given string by reversing the original string.

For example, we enter a string "APPLE", and then use the reverse algorithm. The reverse algorithm returns the string "ELPPA" which is completely reverse of the original string.

Check out and subscribe our channel ( Mood )



  •  Program:

#include<stdio.h>
#include<string.h>

void main()
{
    char s[150];

    printf("Enter the number of rows:\n");
    gets(s);

    strrev(s);
    
    printf("Reverse of the string: %s\n", s);   
}

  •  Output:

Enter the number of rows: nilesh patel Reverse of the string: letap hselin


  •  Visit :

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

Write a program to sort given array in ascending order ( Use insertion sort, Bubble sort, Selection sort, Merge sort, Quickshort, Heapsort)

Insertion Sort Algorithm:

Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Time Complexity of InsertionSort

Best Case : O(n) #Means array is already sorted.

Average Case : O(n²) #Means array with random numbers.

Worst Case : O(n²) #Means array with descending order.

Example:

Let us sort the following numbers using Insertion sort mechanism,
12, 11, 13, 5, 15

Let us loop for i = 1 (second element of the array) to 4 (last element of the array)

i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 15

i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 15

i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of their current position.
5, 11, 12, 13, 15

i = 4. 15 will be at position 5 as all the elements are smaller than 15.
5, 11, 12, 13,15



Bubble Sort:

A well known algorithm called Bubble sort, is easy to understand. Probably this is the least efficient. The basic idea underlying the bubble sort is to pass through the array left to right several times. Each pass consists of comparing each element in the array with its successor and interchanging the two elements if they are not in proper order. At the end of each pass we can observe that the largest element of the list is moved to its final position.
Time Complexity :
Best Case : O(n²) #Means array is already sorted.
Average Case : O(n²) #Means array with random numbers.
Worst Case : O(n²) #Means array with descending order.
Sorting:

Arranging the elements in ascending order or in descending order is called Sorting. Sorting techniques are broadly categorized into two.

  1. Internal Sorting and
  2. External Sorting.

1. Internal Sorting : All the records that are to be sorted are in main memory.

2. External Sorting: Some sorts that cannot be performed in main memory and must be done on disk or tape. This type of sorting is known as External Sorting.

Efficiency: One of the major issues in the sorting algorithms is its efficiency. If we can efficiently sort the records then that adds value to the sorting algorithm. We usually denote the efficiency of sorting algorithm in terms of time complexity. The time complexities are given in terms of big-oh notation.

Commonly there are O(n2) and O(n log n ) time complexities for various algorithms. Quick sort is the fastest algorithm and bubble sort is the slowest one.

Sorting Method — — — — — — — — — — — — — - Efficiency

Bubble sort/Selection sort/Insertion sort — — — — 0(n2)

Quick / Merge — — — — — — — — — — — — — — — 0(n log n)



Selection Sort Algorithm:

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

Time Complexity of InsertionSort

Best Case : O(n²) #Means array is already sorted.
Average Case : O(n²) #Means array with random numbers.
Worst Case : O(n²) #Means array with descending order.


Merge Sort Algorithm:

One of the best sorting technique. If n value is large, it follows divide and conquer approach.

Like Quicksort, Merge Sort is a Divide and conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves.

Time Complexity :

Best Case : O(nlogn) #Means array is already sorted.

Average Case : O(nlogn) #Means array with random numbers.

Worst Case : O(nlogn) #Means array with descending order.


Quick Sort Algorithm:


This is the best sort Technique. QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  1. Always pick first element as pivot.
  2. Always pick last element as pivot
  3. Pick a random element as pivot.
  4. Pick median as pivot.

Time Complexity :

Best Case : O(nlogn) #Means array is already sorted.

Average Case : O(nlogn) #Means array with random numbers.

Worst Case : O(n^2) #Means array with descending order.

Algorithm:

  1. First element is considered as pivot in the implemented code below.
  2. Then we should move all the elements which are less than pivot to one side and greater than pivot to other side.
  3. We divide the array into two arrays in such a way that elements > pivot and elements < pivot.

  •  Program:

#include<stdio.h>
#include<conio.h>

int main()
{
    int a[10],i,j,n,min,temp;
    printf("Enter number you want: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter value at position [%d] :",i+1); 
        scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
        min=a[i];      
        for(j=i+1;j<n;j++)
        {
            if(a[j]<a[i])
            {
                min=j;
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }  
       printf("%d ->",a[i]);
    }
    printf("%d ->",a[i]);
    getch();
    return 0;
}


  •  Output:


Enter number you want: 5
Enter value at position [1] :13
Enter value at position [2] :56
Enter value at position [3] :38
Enter value at position [4] :94
Enter value at position [5] :24
13 ->24 ->38 ->56 ->94 ->


  •  Visit :

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

Write a program to find a character from given string

C program to search all occurrences of a character in a given string - In this post, we will explain the multitude of ways to search all occurrence of a character in a given string in c programming.

Suitable example and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

A string is nothing but an array of character. The value of a string is determined by the terminating character. Its value is considered to be 0.

  •  Program:

#include<stdio.h>
#include<conio.h>

int main()
{
    char str[20], ch, a=1;
    int i=0;
    printf("\n Enter a string : ");
    gets(str);
    printf("\n Enter Character to search in string : ");
    scanf("%c",&ch);
    printf("\n character is ");
    for(i=0; str[i]; i++)
    {
        if(str[i]==ch)
        {
            printf("%d",i+1);
            a=0;
        }
    }
    if(a==1)
    {
        printf("Not found");
    }
    getch();    
}

  •  Output:

Enter a string :ABCDEF
Enter character to search in string :E Character is 5

  •  Visit:

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
  • Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 )
  • Write a C program to interchange two numbers
  • Write a C program to read and store the roll no and marks of 20 students using array
  • Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • February 20241
  • January 20242
  • November 20234
  • October 20236
  • September 20236
  • August 20235
  • April 20231
  • March 20231
  • October 20221
  • August 20223
  • July 20222
  • May 20223
  • April 20222
  • March 20221
  • January 20221
  • December 20216
  • November 20214
  • October 20211
  • September 20216
  • August 202127
  • July 20216

Report Abuse

Copyright

  • Home
  • About us
  • Privacy Policy
  • Disclaimer
  • Contact Us

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes