0% found this document useful (0 votes)
28 views18 pages

C Program

The document provides a comprehensive guide to basic C programming concepts through a series of exercises. It covers topics such as variables, input/output, control flow, arrays, strings, pointers, functions, and structures. Each section includes code examples and tasks to reinforce learning.

Uploaded by

bubalan28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views18 pages

C Program

The document provides a comprehensive guide to basic C programming concepts through a series of exercises. It covers topics such as variables, input/output, control flow, arrays, strings, pointers, functions, and structures. Each section includes code examples and tasks to reinforce learning.

Uploaded by

bubalan28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C program

C Basics: Variables, I/O, and Control Flow (Questions 1-15)


1. Print Your Name: Write a program to print your full name.
#include<stdio.h>
int main()
{
printf("My full Name is : AKILESH");
return 0;
}

2. Declare and Initialize: Declare an integer variable age and a float variable height. Assign your
age and height to them and print both
#include<stdio.h>
int main()
{
int age=18;
float height=171.5;
printf("My age is : %d\n",age);
printf("My height is: %.1f",height);
return 0;
}

3. Simple Arithmetic: Ask the user for two numbers, then print their sum, difference, product,
and quotient.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
printf("sum is: %d\n",a+b);
printf("difference is: %d\n",a-b);
printf("product is : %d\n",a*b);
printf("quotient is : %d\n",a/b);
return 0;
}

4. Area of a Rectangle: Create two variables for length and width. Calculate and print the area
of the rectangle.
#include<stdio.h>
int main()
{
int length;
int width;
int area;
printf("Enther a length: ");
scanf("%d",&length);
printf("Enter a width :");
scanf("%d",&width);
area = length*width;
printf("Area of a rectangle: %d",area);
return 0;

5. Check Even or Odd: Ask the user for a number and print whether it is "Even" or "Odd". (Hint:
Use the modulo % operator).
#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d",&num);
if(num%2==0)
{
printf("The number is even");
}
else
{
printf("The number is odd");
}
return 0;
}

6. Positive, Negative, or Zero: Ask for a number and print if it's "Positive", "Negative", or
"Zero".
#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d",&num);
if(num==0)
{
printf("The number is zero");
}
else if(num<0)
{
printf("The number is negative");
}
else
{
printf("The number is positive");
}
return 0;
}

7. Largest of Two Numbers: Ask the user for two numbers and print the larger one.
#include<stdio.h>
int main()
{
int num1;
int num2;
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
if(num1>num2)
{
printf("The largest numbers is: %d",num1);
}
else
{
printf("The largest number is: %d",num2);
}
return 0;
}

8. Simple switch: Create a variable day and set it to a number between 1 and 3. Use a switch
statement to print "Monday" for 1, "Tuesday" for 2, and "Wednesday" for 3.
#include<stdio.h>
int main()
{
int day=2;
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
}
return 0;
}

9. for Loop Countdown: Use a for loop to print a countdown from 10 to 1.


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

10. while Loop Sum: Use a while loop to calculate the sum of numbers from 1 to 10.
#include<stdio.h>
int main()
{
int i=1;
int sum;
while(i<=10)
{
sum+=i;
i++;
}
printf("sum of number from 1 to 10 is: %d",sum);
return 0;
}

11. Print Even Numbers: Use a for loop and an if statement to print all even numbers between 1
and 20.
#include<stdio.h>
int main()
{
int i;
for(i=1;i<20;i++)
{
if(i%2==0)
{
printf("%d ",i);
}
}
return 0;
}

12. break Statement: Write a for loop that prints numbers from 1 to 10, but stops (breaks) when
it reaches 5.
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d ",i);
if(i==5)
{
break;
}
}

return 0;
}

13. continue Statement: Write a for loop that prints numbers from 1 to 10, but skips printing the
number 5.
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{

if(i==5)
{
continue;
}
printf("%d ",i);
}
return 0;
}

14. User Input Loop: Keep asking the user to enter a number until they enter 0.
#include<stdio.h>
int main()
{
int i;
do
{
printf("Enter a number: ");
scanf("%d",&i);
}while(i!=0);
return 0;
}

15. Character Input: Ask the user to enter a single character and print it back.
#include<stdio.h>
int main()
{
char mychar;
printf("Enter a single character: ");
scanf(" %c",&mychar);
printf("%c",mychar);
return 0;
}

Arrays and Strings (Questions 16-25)

16. Initialize and Print Array: Create an integer array of size 5 and store the numbers 10, 20, 30,
40, 50. Print the third element.
#include<stdio.h>
int main()
{
int myarray[5] = {10,20,30,40,50};
printf(" %d",myarray[2]);
return 0;
}

17. Sum of Array Elements: Using the array from the previous question, write a loop to calculate
and print the sum of all its elements.
#include<stdio.h>
int main()
{
int myarray[5] = {10,20,30,40,50};
int i;
int sum=0;
for(i=0;i<=5;i++)
{
sum+=myarray[i];
}
printf("sum of array is: %d",sum);
return 0;
}

18. Find Max in Array: Create an integer array and write code to find and print the largest
number in it.
#include<stdio.h>
int main()
{
int myarray[5]={10,20,30,40,50};
int i;
int largest;
largest=myarray[0];
for(i=0;i<5;i++)
{
if(myarray[i]>largest)
{
largest=myarray[i];
}
}
printf("largest number in the array: %d",largest);
return 0;
}

19. User Input into Array: Ask the user to enter 3 numbers and store them in an array. Then,
print the array elements.

#include<stdio.h>

int main()

int myarray[3];

int i;

printf("Enter first numbers: ");

scanf(" %d",&myarray[0]);

printf("Enter second number: ");

scanf(" %d",&myarray[1]);

printf("Enter third number: ");

scanf(" %d",&myarray[2]);

for(i=0;i<3;i++)

{
printf(" %d",myarray[i]);

return 0;

20. Declare a String: Declare a string (character array) and initialize it with your favorite color.
Print the string.
#include<stdio.h>
int main()
{
char favcolour[]="Blue";
printf("My favorite colour is: %s",favcolour);
return 0;
}

21. String Length: Ask the user to enter their first name and print its length. (Hint: Use the
strlen() function from ).
#include<stdio.h>
#include<string.h>
int main()
{
char myName[20];
int length;
printf("Enter your name: ");
scanf("%s",myName);
printf("Its length is: %zu",strlen(myName));
return 0;
}

22. Concatenate Strings: Create two strings, "Hello " and "World!". Combine them into a third
string and print it. (Hint: Use strcpy() and strcat()).
#include<stdio.h>
#include<string.h>
int main()
{
char str1[10]="Hello ";
char str2[10]="World!";
char str3[20];
strcat(str1,str2);
strcpy(str3,str1);
printf("%s",str3);
return 0;
}
23. Compare Strings: Ask the user to enter a password. If it is "secret123", print "Access
Granted". Otherwise, print "Access Denied". (Hint: Use strcmp()).
#include<stdio.h>
#include<string.h>
int main()
{
char password[10]="secret123";
char userpassword[10];
printf("Enter the password: ");
scanf("%s",userpassword);
if(strcmp(password,userpassword))
{
printf("Access Denied");
}
else
{
printf("Access Granted");
}
return 0;
}

24. Access String Character: Create a string "Programming" and print its first character (P) and
its third character (o).
#include<stdio.h>
int main()
{
char str[20]="Programming";
printf("%c \n",str[0]);
printf("%c",str[2]);
return 0;
}

25. Reverse a String: (Slightly challenging) Create a string and print it in reverse.

#include<stdio.h>

#include<string.h>

int main()

char str[20]="AKILESH";

printf("Reversed string: %s",strrev(str));

return 0;

}
Pointers and Memory (Questions 26-30)

26. Print Memory Address: Declare an integer variable and print its value and its memory
address.
#include<stdio.h>
int main()
{
int num=15;
printf("%d\n",num);
printf("%zu",&num);
return 0;

27. Declare a Pointer: Declare an integer variable x and an integer pointer ptr. Make ptr point to
x.
#include<stdio.h>
int main()
{
int x=5;
int *ptr;
ptr=&x;
printf("%d\n",x);
printf("%d",*ptr);
return 0;
}

28. Dereference a Pointer: Using the pointer from the previous question, print the value of x
through the pointer.
#include<stdio.h>
int main()
{
int x=50;
int *ptr;
ptr=&x;
printf("The value of x is: %d",*ptr);
return 0;
}

29. Change Value with Pointer: Using the pointer from question 27, change the value of x to 100
and then print the new value of x.
#include<stdio.h>
int main()
{
int x=50;
int *ptr;
printf("The value of x is: %d\n",x);
ptr=&x;
*ptr=100;
printf("The new value of x is: %d",x);
return 0;
}
30. Pointer to Array: Create an integer array and a pointer that points to its first element. Use
the pointer to print the first element.
#include<stdio.h>
int main()
{
int myarray[10]={10,20,30,40};
int *ptr;
ptr=myarray;
printf("The first element is : %d",*ptr);
return 0;
}

Functions (Questions 31-38)

31. Simple Function: Write a function called sayHello() that prints "Hello from a function!". Call
this function from main().
#include<stdio.h>
void sayHello()
{
printf("Hello from a function!");
}
int main()
{
sayHello();
return 0;
}

32. Function with Parameters: Write a function printNumber() that takes an integer as a
parameter and prints it.
#include<stdio.h>
void printNumber(int num)
{
printf("%d",num);
}
int main()
{
printNumber(15);
return 0;
}

33. Function that Adds: Write a function add() that takes two integers as parameters and returns
their sum. Call it from main and print the result.
#include<stdio.h>
void add(int num1,int num2)
{
int sum;
sum=num1+num2;
printf("The sum is: %d",sum);
}
int main()
{
add(5,5);
return 0;
}

34. Function Declaration: Declare a function multiply() at the top of your file, define it after
main(), and call it from main().
#include<stdio.h>
void multiply();

int main()
{
multiply();
return 0;
}

void multiply()
{
printf("The function is called");
}

35. Square a Number: Write a function that takes a number and returns its square.
#include<stdio.h>
#include<math.h>
void mysquare(int num)
{
int square;
square=num*num;
printf("The square of a number is: %d",square);
}
int main()
{
mysquare(5);
return 0;
}

36. Global vs. Local Variable: Declare a global variable and a local variable inside a function with
the same name. Print both to see the scope.
#include<stdio.h>
int num=10;
void myfunc()
{
int num=20;
printf("The value of local variable: %d\n",num);
}
int main()
{
myfunc();
printf("The value of Global variable: %d",num);
return 0;
}

37. math.h Function: Include the library and use the sqrt() function to find the square root of 64.
#include<stdio.h>
#include<math.h>
int main()
{
int num=64;
int root=sqrt(num);
printf("The squareroot of %d is: %d",num,root);
return 0;
}

38. Simple Recursion: Write a recursive function to print a countdown from a given number
down to 1.
#include<stdio.h>
void countdown(int n)
{
if(n==0)
{
return;
}
printf("%d\n",n);
countdown(n-1);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d",&num);
printf("Countdown:\n");
countdown(num);
return 0;
}

Structures, Enums, and Unions (Questions 39-44)

39. Create a struct: Define a struct named Student with two members: id (int) and name (char
array).
#include<stdio.h>
#include<string.h>
struct student
{
int id;
char name[10];
};
int main()
{
struct student s1;
s1.id=6;
strcpy(s1.name,"AKILESH");
printf("ID: %d\n",s1.id);
printf("NAME: %s",s1.name);
return 0;
}

40. Use a struct: Create a Student variable, assign values to its members (e.g., id=1,
name="John"), and print them.
#include<stdio.h>
#include<string.h>
struct student
{
int id;
char name[10];
};
int main()
{
struct student s1;
s1.id=1;
strcpy(s1.name,"John");
printf("ID: %d\n",s1.id);
printf("NAME: %s",s1.name);
return 0;
}

41. Array of structs: Create an array that can hold 2 Student structs. Fill it with data and print the
name of the second student.
#include<stdio.h>
#include<string.h>
struct student
{
int age;
char name[10];
};
int main()
{
struct student s1;
struct student s2;
s1.age=17;
strcpy(s1.name,"AKIL");
s2.age=18;
strcpy(s2.name,"AKILESH");
printf("The student2 name: %s",s2.name);
return 0;
}

42. Pointer to struct: Create a Student struct and a pointer to it. Use the pointer and the ->
operator to access and print its members.
#include<stdio.h>
struct student
{
int age;
char grade;
};
int main()
{
struct student s1={18,'A'};
struct student *ptr=&s1;
printf("The student age is: %d\n",ptr->age);
printf("The student grade is: %c",ptr->grade);
return 0;
}

43. Create an enum: Create an enum for Level with the members LOW, MEDIUM, and HIGH.

#include<stdio.h>
enum Level
{
LOW,
MEDIUM,
HIGH
};
int main()
{
enum Level mylevel=HIGH;
printf("%d",mylevel);
return 0;
}

43. Use a union: Create a union Data that can hold an int, float, or char. Store an integer in it and
print it. Then, store a float and print it to see what happens.
#include<stdio.h>
union Data
{
int a;
float b;
char c;
};
int main()
{
union Data u1;
u1.a=25;
printf("The integer is: %d\n",u1.a);
u1.b=3.14;
printf("The float is: %f",u1.b);
return 0;
}

File I/O (Questions 45-47)

44. Create and Write to File: Write a program that creates a file named greeting.txt and writes
the text "Hello, File!" into it.
#include<stdio.h>
int main()
{
FILE *fptr;
fptr=fopen("greeting.txt","w");
fprintf(fptr,"Hello File!");
fclose(fptr);
return 0;
}

45. Append to File: Write a program that opens greeting.txt in append mode and adds the line
"Goodbye!" to it.
#include<stdio.h>
int main()
{
FILE *fptr;
fptr=fopen("greeting.txt","a");
fprintf(fptr,"\nGoodbye!");
fclose(fptr);
return 0;
}
46. Read from File: Write a program that reads the entire content of greeting.txt and prints it to
the console.
#include<stdio.h>
int main()
{
FILE *fptr;
fptr=fopen("greeting.txt","r");
char mystring[100];
while(fgets(mystring,100,fptr))
{
printf("%s",mystring);
}
fclose(fptr);
return 0;

Standard Libraries and More (Questions 48-50)

48. Random Number: Use the rand() function from to generate and print a random number.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(0));
int randomnumber=rand();
printf("The random number is: %d",randomnumber);
return 0;
}
49. Character Type: Use the isdigit() function from to check if the character '7' is a digit.

#include<stdio.h>

#include<ctype.h>

int main()

char ch='7';

if(isdigit(ch))

printf("%c is a digit",ch);

else

printf("%c is not a digit",ch);

return 0;

}
50. Current Time: Use functions from to get and print the current date and time.
#include<stdio.h>
#include<time.h>
int main()
{
time_t currenttime;
time(&currenttime);
printf("The current time is: %s",ctime(&currenttime));
return 0;
}

You might also like