0% found this document useful (0 votes)
5 views3 pages

C Programs Part1

The document contains multiple C programs that demonstrate basic programming concepts such as checking if a number is even or odd, determining leap years, finding the greatest of two or three numbers, identifying positive or negative numbers, classifying characters as vowels or consonants, assigning grades based on marks, and using switch statements to display month names and weekdays. Each program prompts the user for input and provides output based on the conditions specified. These examples serve as fundamental exercises for beginners in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C Programs Part1

The document contains multiple C programs that demonstrate basic programming concepts such as checking if a number is even or odd, determining leap years, finding the greatest of two or three numbers, identifying positive or negative numbers, classifying characters as vowels or consonants, assigning grades based on marks, and using switch statements to display month names and weekdays. Each program prompts the user for input and provides output based on the conditions specified. These examples serve as fundamental exercises for beginners in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Even or Odd

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}

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("Leap year\n");
else
printf("Not a leap year\n");
return 0;
}

Greatest of 2 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\n", a);
else
printf("%d is greater\n", b);
return 0;
}

Greatest of 3 Numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b && a > c)
printf("%d is greatest\n", a);
else if(b > c)
printf("%d is greatest\n", b);
else
printf("%d is greatest\n", c);
return 0;
}

Positive or Negative
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num > 0)
printf("Positive\n");
else if(num < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}

Vowel or Consonant
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
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\n");
else
printf("Consonant\n");
return 0;
}

Grade of Student
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if(marks >= 90)
printf("Grade A\n");
else if(marks >= 80)
printf("Grade B\n");
else if(marks >= 70)
printf("Grade C\n");
else if(marks >= 60)
printf("Grade D\n");
else
printf("Grade F\n");
return 0;
}

Month Name using Switch


#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch(month) {
case 1: printf("January\n"); break;
case 2: printf("February\n"); break;
case 3: printf("March\n"); break;
case 4: printf("April\n"); break;
case 5: printf("May\n"); break;
case 6: printf("June\n"); break;
case 7: printf("July\n"); break;
case 8: printf("August\n"); break;
case 9: printf("September\n"); break;
case 10: printf("October\n"); break;
case 11: printf("November\n"); break;
case 12: printf("December\n"); break;
default: printf("Invalid month\n");
}
return 0;
}

Weekday using Switch


#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
case 4: printf("Wednesday\n"); break;
case 5: printf("Thursday\n"); break;
case 6: printf("Friday\n"); break;
case 7: printf("Saturday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}

You might also like