Home » C programming language

C programming exercises, practices with solutions



In this section we are providing some c programming exercise for practice, we are also providing the solutions with the output to make your programming skills perfect. If you have any query related to any exercise please write in the comment.

C exercise set -1

  1. Write a c program to take input of two numbers, find their sum, product and sum of the squares.
  2. Write c program to take name, address, age and height in feet (a float value should be entered), print all values in new lines.
  3. Write a c program to input n numbers print their sum and largest number without storing them into an array.
  4. Write a c program to input and print the age which should be greater than 18 and less than 30, age check should be there and input age recursively until age value is not valid (use goto statement).
  5. Write a c program to read time in HH24:MM:SS format using single scanf() statement, make sure input value should be correct, if input values are wrong read them recursively; print the time in HH24:MM:SS if values are correct.



1) Write a c program to take input of two numbers, find their sum, product and sum of the squares.

#include <stdio.h>

int main()
{
	int age;
	float height;
	char name[30], address[100];
	
	//Enter details
	printf("Enter name: ");
	fgets(name,30,stdin);
	printf("Enter address: ");
	fgets(address,100,stdin);	
	
	printf("Enter age: ");
	scanf("%f",&age);
	printf("Enter height in feet: ");
	scanf("%f",&height);
	
	//print input details
	printf("Name: %s\n",name);
	printf("Address: %s\n",address);
	printf("Age: %d, Height: %0.2f\n",age,height);
	
	return 0;
}

Output

Enter first number: 10
Enter second number: 20 
a: 10, b: 20
Sum of numbers: 30
Product of numbers: 200 
Sum of squares of numbers: 500




2) Write c program to take name, address, age and height in feet (a float value should be entered), print all values in new lines.

#include <stdio.h>

int main()
{
	int age;
	float height;
	char name[30], address[100];
	
	//Enter details
	printf("Enter name: ");
	fgets(name,30,stdin);
	printf("Enter address: ");
	fgets(address,100,stdin);	
	
	printf("Enter age: ");
	scanf("%d",&age);
	printf("Enter height in feet: ");
	scanf("%f",&height);
	
	//print input details
	printf("Name: %s",name);
	printf("Address: %s",address);
	printf("Age: %d, Height: %0.2f\n",age,height);
	
	return 0;
}

Output

Enter name: Mike the rocks
Enter address: New Delhi, India 
Enter age: 23 
Enter height in feet: 5.7 
Name: Mike the rocks
Address: New Delhi, India 
Age: 23, Height: 5.70 




3) Write a c program to input n numbers print their sum and largest number without storing them into an array.

#include <stdio.h>

int main()
{
	int number,sum,largest_number=0;
	int choice;
	
	sum=0; largest_number=0;
	
	do{
		printf("Enter an integer number: ");
		scanf("%d",&number);
		sum+=number;
		if(number>largest_number)
			largest_number=number;
		
		printf("Want to continue... (1 for yes):");
		scanf("%d",&choice);		
	}while(choice==1);
	
	printf("Sum of all input numbers: %d\n",sum);
	printf("Largest number: %d\n",largest_number);
	
	return 0;
}

Output

Enter an integer number: 10 
Want to continue... (1 for yes):1 
Enter an integer number: 20 
Want to continue... (1 for yes):1 
Enter an integer number: 30 
Want to continue... (1 for yes):1 
Enter an integer number: 40 
Want to continue... (1 for yes):0 
Sum of all input numbers: 100 
Largest number: 40 




4) Write a c program to input and print the age which should be greater than 18 and less than 30, age check should be there and input age recursively until age value is not valid (use goto statement).

#include <stdio.h>

int main()
{
	int age;
	
	start:
		printf("Enter age (18-30): ");
		scanf("%d",&age);
		if(age<18 || age>30)
		{
			printf("Incorrect age, please enter between 18-30.\n");
			goto start;
		}
		
		printf("Wau.. your age is: %d\n",age);
		
		return 0;
}

Output

Enter age (18-30): 15 
Incorrect age, please enter between 18-30.
Enter age (18-30): 33 
Incorrect age, please enter between 18-30.
Enter age (18-30): 31 
Incorrect age, please enter between 18-30.
Enter age (18-30): 23 
Wau.. your age is: 23 



5) Write a c program to read time in HH24:MM:SS format using single scanf() statement, make sure input value should be correct, if input values are wrong read them recursively; print the time in HH24:MM:SS if values are correct.

#include <stdio.h>

int main()
{
	int hh,mm,ss;
	
	start:
		printf("Enter time in HH24:MM:SS format: ");
		scanf("%02d:%02d:%02d",&hh,&mm,&ss);
		
		if(hh<0 || hh>23 || mm<0 || mm>59 || ss<0 || ss>59)
		{
			printf("Incorrect time, please enter time in HH24:MM:SS format.\n");
			goto start;
		}
		
		printf("Input time is correct.\n");
		printf("The time is %02d:%02d:%02d\n",hh,mm,ss);
		
		return 0;
}

Output

Enter time in HH24:MM:SS format: 24:30:00 
Incorrect time, please enter time in HH24:MM:SS format. 
Enter time in HH24:MM:SS format: 23:59:60 
Incorrect time, please enter time in HH24:MM:SS format. 
Enter time in HH24:MM:SS format: 23:59:59 
Input time is correct.
The time is 23:59:59





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.