C exercises and solutions programming


C Exercises

C exercises intent to help you learn C programming language effectively. You can use C exercises here to help you test your knowledge and skill of writing code in C and practice the C programming lessons. You will start from basic C exercises to more complex exercises. The solution is provided for each exercise. However, you should try to solve each problem by yourself first before you check the solution. If you have any questions regarding to each problem, you can post them at our forum or leave your comment on the left side of the page.

Exercise 1: Write a C program to print the following line as shown below:

Welcome!
You are able to test your skill of writing C code here.


Solution:

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
  printf(" Welcome!\n");
  printf(" You are able to test your skill of writing C code here.\n");
 
  system("PAUSE");     
  return 0;
}  

Exercise 2: Write five statements by using printf function to print the asterisk pattern:


*****
*****
*****
*****
*****

Solution:

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    printf("*****\n");
    printf("*****\n");
    printf("*****\n");
    printf("*****\n");
    printf("*****\n");
    system("PAUSE");
    return EXIT_SUCCESS;
}

Exercise 3:  Write a C program to declare two integer and one float variables then initialize them to 10, 15, and 12.6. It then prints these values on the screen.

 
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int x;
    int y;
    float z;
    x=10;
    y=15;
    z=12.6;
    printf("x= %d",x);printf("\t");
    printf("y=%d",y);printf("\t");
    printf("z=%3.1f",z);
    printf("\n");
 
  system("PAUSE");     
  return 0;
}

Exercise 4:  Write a C program to prompt the user to input her/his age and print it on the screen, as shown below.


Your age is 20 years old. Solution:
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int age;
    printf("Enter your age:");
    scanf("%d",&age);
    printf("You are %2d ",age);printf(" years old.\n");
 
    system("PAUSE");   
    return 0;
}

Exercise 5:  Write a C program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below.


 
Please enter your 3 numbers: 12 45 78
 
Your numbers forward:
12
45
78
 
Your numbers reversed:
78
45
12

Solution:
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int val1;
    int val2;
    int val3;
    printf("Please enter your 3 numbers:");
    scanf("%d %d %d",&val1,&val2,&val3);
   
    printf("\nYour numbers forward:\n");
    printf("%d",val1);printf("\n");
    printf("%d",val2);printf("\n");
    printf("%d",val3);printf("\n");
    printf("Your numbers reversed:\n");
    printf("%d",val3);printf("\n");
    printf("%d",val2);printf("\n");
    printf("%d",val1);printf("\n");
 
    system("PAUSE");   
    return 0;
}


Comments

Ammar Shaukat comment

 Ammar Shaukat

very great website for learning..


2016-11-08
seo comment

 seo

Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword....wait there's even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at. <a href="http://www.SEORankingLinks.com">Seo Plugin</a>
seo http://www.SEORankingLinks.com/


2016-07-29
asad comment

 asad

Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. <a href="http://www.beginnersheap.com/"> Beginners Heap</a> also provides latest tutorials of Programming from beginning to advance level.
Be with us to learn programming in new and creative way.


2016-07-27
seo plugin comment

 seo plugin

Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword....wait there's even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at. <a href="http://www.SeoOptimizedRankings.com">Seo Plugin</a>
[url=http://www.SeoOptimizedRankings.com/]seo plugin[/url]


2016-05-21
Neel comment

 Neel

4. State how many bytes of storage are reserved for the variable math in the following declaration

union {
int x;
char y[5];
float s;
} Math;

ANSWER: ____________


2016-04-19
Neel comment

 Neel

4. State how many bytes of storage are reserved for the variable math in the following declaration

union {
int x;
char y[5];
float s;
} Math;

ANSWER: ____________


2016-04-19
Chandra limbu comment

 Chandra limbu

wap to find out the largest number amg any 3 number?


2016-03-14
Chandra limbu comment

 Chandra limbu

wap to find out the largest number amg any 3 number?


2016-03-14
Chandra limbu comment

 Chandra limbu

wap to find out the largest number amg any 3 number?


2016-03-14
jai comment

 jai

show how 9mn/p+1-1/3(a+b) evaluated in c


2015-12-27
bonita comment

 bonita

to ez for me so basic gg ez code easiest code of my life


2015-12-10
bonita comment

 bonita

to ez for me so basic gg ez code easiest code of my life


2015-12-10
Doesn

 Doesn't matter

Hi Bibek,
Hope the following code helps you,

#include<stdio.h>
int main()
{
for(int i=1;i<6;i++)
{
for(int j=1; j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}


2015-09-30
Yathreb comment

 Yathreb

Hi Bibek,
Hope the following code helps you,

#include<stdio.h>
int main()
{
for(int i=1;i<6;i++)
{
for(int j=1; j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}


2015-09-30
pavani comment

 pavani

can you give my any question i want to check whether i am able to do or not


2015-08-22
REXITOO  comment

 REXITOO

some of them are useful bcos they have the write syntax ,
But some I don't understand!!!


2015-08-17
vijay comment

 vijay

solve and send it
Aman and Jasbir are very intelligent guys of their batch. Today they are playing a game "Game of Numbers".


2015-08-07
vijay comment

 vijay

solve and send it
Aman and Jasbir are very intelligent guys of their batch. Today they are playing a game "Game of Numbers".


2015-08-07
solve the problem comment

 solve the problem

A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot.


2015-08-07
Other exercises comment

 Other exercises

http://wiki.altervista.org/cs/c_language/basic_concepts


2015-06-05
ghostman comment

 ghostman

for exercise 1, what is the function for int age? i try not to put this word it's actually same word display. .


2015-04-18
Bibek comment

 Bibek

Please print the following pyramid

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5



2015-04-14
psyphy comment

 psyphy

find out how can we change the amount with minimum number of notes and show the number of denominations.E.g.1234 taka can be changed by 2 notes of taka 500,2 notes of taka 20 1 notes of taka10, 2 notes of taka 2. so minimum number of denominations.


2015-03-15
imane comment

 imane

can you give me the exercise bet in french , please


2015-01-16
lakkim sun comment

 lakkim sun

I like the most . I'm so happy for all you exercise
but some of them I don't understand . thanks you very much!!!


2014-11-20
help please comment

 help please

http://pastebin.com/jAPakAtH

can somone help me please
just compile it and add 2 accounts and go to view accounts only the last account added is read .. can someone help me please note(im not using database)


2014-11-08
dipali comment

 dipali

so useful


2014-10-14
ndombe comment

 ndombe

Helpful for biginners.


2014-09-04
MANOJ ROY comment

 MANOJ ROY

very very nice for beginners


2014-08-28
atul kasliwal comment

 atul kasliwal

Very Basic


2014-08-24
a c programmer comment

 a c programmer

very basic. didn't help.


2014-08-02
Phorn Ya comment

 Phorn Ya

It is really boring, sometimes i am very difficult to understand, but maybe my knowledge not high so sometimes i don't understand.


2014-07-28
mugisha steeven comment

 mugisha steeven

I am so happy to that application it so good.


2014-07-01
Mahbuba comment

 Mahbuba

I have been looking all over the internet and books searching for some exercises that are students who have never done any programming language before. Finally I am so happy that I found one:) Good job. It was really very helpful!!!


2014-06-15
mahesh comment

 mahesh

These <a href="http://www.beginners-tutorials.com/2013/03/useful-c-programming-exercises-for.html">c programming exercises</a> are very helpful to me. thanks for sharing.


2014-06-10
C_noob comment

 C_noob

very good exercises, awesome for beginners.


2014-04-29
Gio comment

 Gio

Good exercises


2014-04-24
khean chien comment

 khean chien

application is good


2014-03-19
srinath reddy comment

 srinath reddy

C programming exercises are very useful for beginners. The C programs posted here are good and useful. Thanks for sharing. I also posted a similar article. please check it.
http://www.beginners-tutorials.com/2013/03/useful-c-programming-exercises-for.html


2013-10-02
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.