Simple program example in c language


Basic simple c  language programs examples for beginners

1
C program to print "hello world".
Code
#include<stdio.h>
int main(){
    printf("Hello world");
    return 0;
}

2
C program to add two numbers in c.
Code
#include<stdio.h>
int main(){
    int a,b,sum;
    scanf("%d %d",&a,&b);
    sum = a + b;
    printf("%d",sum);
    return 0;
}

3
C program to accept a string from users and print it.
Code
#include<stdio.h>
int main(){
    char str[20];
    scanf("%s",&str);
    printf("%s",str);
    return 0;
}

4
C program to check a given number is positive or negative.
Code
#include<stdio.h>
int main(){
    int num;
    scanf("%d",&num);
   
    if(num ==0)
         printf("Number is zero.");
    else if(num > 0)
printf("Number is positive.");
else
printf("Number is negative.");
    return 0;
}

5
C program to swap two numbers.
Code
#include<stdio.h>
int main(){
    int a,b,c;
    scanf("%d %d",&a,&b);
   
    c = a;
    a = b;
    b = c;

    printf("%d %d",a,b);
    return 0;
}

6
C program to find area of a circle.
Code
#include<stdio.h>
int main(){
    floar r,area;
        
    printf("Enter radius of circle: ");
    scanf("%f",&r);

    area = 3.14 * r * r;

    printf("Area of circle is: %f",area);
    return 0;
}

7
C program to print numbers from 1 to 10.
Code
#include<stdio.h>
int main(){

int i;

    for(i=1;i<=10;i++){
        printf("%d ",i);
    }

    return 0;
}

8
C program to print numbers from 1 to 10 in reverse order.
Code
#include<stdio.h>
int main(){

int i;

    for(i=10;i>= 0;i--){
        printf("%d ",i);
    }

    return 0;
}

9
C program to print all even numbers of the given range.
Code
#include<stdio.h>
int main(){

    into min,max;

    printf("Enter min and max even numbers respectively: ");
    scanf("%d %d",&min,&max);

    for(i=min;i<=max;i=i+2){
        printf("%d ",i);
    }

    return 0;
}

10
C program to accept a string and find out total numbers of characters.
Code
#include<stdio.h>
#include<string.h>

int main(){
    char str[20];

    printf("Enter any string: ");
    scanf("%s",&str);
    printf("Length of string: %d",strlen(str));

    return 0;
}

11
C program to add n natural numbers from 1.
Code
#include<stdio.h>
int main(){

int n,sum = 0;

    printf("Enter any positive number: ");
    scanf("%d",&n);

    for(i=1;i<=n;i++){
        sum = sum + 1;
    }

printf("Sum of n numbers: %s",sum);

    return 0;
}

12
C program to get the marks of student and print the average marks.
Code
#include<stdio.h>
int main(){

int n;
float marks, sum = 0;
   
    printf("Enter any total numbers of student: ");
    scanf("%d",&n);

   
    for(i=1;i<=n;i++){
        printf("Enter marks of student %d: ",i);
       scanf("%f",&marks);

       sum = sum + marks;
    }

printf("Average marks: %f",sum/n);

    return 0;
}

13
C program to print the all elements of any array.
Code
#include<stdio.h>
int main(){

int arr[]={1,2,34,56,89};

int i;

    for(i=0;i<5;i++){
        printf("%d ",arr[i]);
    }

    return 0;
}

14
C program to find square root of a number.
Code
#include<stdio.h>
#include<math.h>

int main(){
    int num;
    scanf("%d",&num);

    printf("Square root: %f",sqrt(num));
    return 0;
}

15
C program to find max of two unequal numbers.
Code
#include<stdio.h>
int main(){
    int a,b,max;
    scanf("%d %d",&a,&b);
   
    if(a > b)
         max = a;
    else
         max = b;

    printf("%d",max));
    return 0;
}

16
C program to check given number is even or odd.
Code
#include<stdio.h>

int main(){

    int number;
  
    printf("Enter any integer: ");
    scanf("%d",&number);

    if(number % 2 ==0)
         printf("%d is even number.",number);
    else
         printf("%d is odd number.",number);
  
    return 0;

}

17
Write a c program to print multiplication table.
Code
#include<stdio.h>
int main(){
  int r,i,j,k;
  printf("Enter the number range: ");
  scanf("%d",&r);
  for(i=1;i<=r;i++){
      for(j=1;j<=10;j++)
           printf("%d*%d=%d ",i,j,i*j);
      printf("\n");
  }
  return 0;
}

18
Code to find the ASCII values of given character in c. 
Code
#include<stdio.h>

int main(){
  
    char c;

    printf("Enter any character: ");
    scanf("%c",&c);

    printf("ASCII value of given character: %d",c);
        
    return 0;
}

19
C program to convert upper case to lower case.
Code
#include<stdio.h>
#include<string.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string: ");
  scanf("%s",str);
  printf("The string is: %s",str);
  for(i=0;i<=strlen(str);i++){
      if(str[i]>=65&&str[i]<=90)
       str[i]=str[i]+32;
  }
  printf("\nThe string in uppercase is: %s",str);
  return 0;
}

20
C program to convert lower case to upper case.
Code
#include<stdio.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string: ");
  scanf("%s",str);
  printf("The string is: %s",str);
  for(i=0;i<=strlen(str);i++){
    if(str[i]>=97&&str[i]<=122)
         str[i]=str[i]-32;
  }
  printf("\nThe string in lowercase is: %s",str);
  return 0;
}

24 comments:

Anonymous said...

how to construct outline hollow rectangle?

Anonymous said...

very very usefull and simple thank u !!!!

Anonymous said...

nice

Google Ads Article said...

very nyc

Anonymous said...

question 6 appears to have a typo. floar is not a datatype.

Kumar Shanmugam said...

Its a spelling mistake. that is float not floar.

Anonymous said...

hlo am navi mujea ak qustion ka ans cheai kya ap mujea btea sakte ho

Anonymous said...

excellent stuff .thank you

Anonymous said...

maa da lora

Anonymous said...

gracious

Anonymous said...

arrigato

Anonymous said...

good

Anonymous said...

Please help me i want to learn c language. But i have little knowldge in computer science, but i am good in mathematics.

Syuhaida Safaai said...

hey there, google brought me here, can you give me example of simple programming using c++? the very very basic one.

vikas arora said...

nice questions

Hadil said...

well.....

Unknown said...

nice and less time consum

Unknown said...

It is awesome.!!

Unknown said...

Ha... Pucho

Unknown said...

Its simply super

Unknown said...

it was very useful

Unknown said...

thank you so much ......

SHIRIMA AD said...

I've found them helpful ,May the Almighty bless you all.

SHIVAM SHARMA said...

Useful post Thanks for sharing it that truly valuable knowledge about similar topic. Tableau training in pune