C SAMPLE PROGRAMS
1.Write a C program to print "Hello, World!" on the screen.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
2. Write a C program to add two numbers entered by the user.
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
3. Write a C program to find the largest number among two numbers.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if(a > b)
printf("%d is greater.", a);
else
printf("%d is greater.", b);
return 0;
}
4. Write a C program to check whether a number is positive, negative
or zero.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num > 0)
printf("Positive number");
else if(num < 0)
printf("Negative number");
else
printf("Zero");
return 0;
}
5. Program to Check Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
return 0;
}
6. Program to Swap Two Numbers (using third variable)
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d", a, b);
return 0;
}
7. Write a C program to check whether a number entered by the user
is even or odd.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
8. Write a C program to print the multiplication table of a given
number.
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
for(i = 1; i <= 10; ++i)
printf("%d x %d = %d\n", num, i, num * i);
return 0;
}
9. Write a C program to check whether a character is a vowel or
consonant.
#include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("Vowel");
else
printf("Consonant");
return 0;
}
10. Write a C program to print numbers from 1 to 10 using a loop.
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
11. Write a C program to reverse a given number.
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed number = %d", reversed);
return 0;
}